zhou zhou
6 小时以前 e12fb4e6e8e0a408e81ce05a269a15cc535d8c78
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<template>
  <div class="check-diff-item-page art-full-height">
    <ArtSearchBar
      v-model="searchForm"
      :items="searchItems"
      :showExpand="true"
      @search="handleSearch"
      @reset="handleReset"
    />
 
    <ElCard class="art-table-card">
      <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
        <template #left>
          <ListExportPrint
            :preview-visible="previewVisible"
            @update:previewVisible="handlePreviewVisibleChange"
            :report-title="reportTitle"
            :selected-rows="selectedRows"
            :query-params="reportQueryParams"
            :columns="columns"
            :preview-rows="previewRows"
            :preview-meta="previewMeta"
            :total="pagination.total"
            :disabled="loading"
            @export="handleExport"
            @print="handlePrint"
          />
        </template>
      </ArtTableHeader>
 
      <ArtTable
        :loading="loading"
        :data="data"
        :columns="columns"
        :pagination="pagination"
        @selection-change="handleSelectionChange"
        @pagination:size-change="handleSizeChange"
        @pagination:current-change="handleCurrentChange"
      />
    </ElCard>
 
    <CheckDiffItemDetailDrawer v-model:visible="detailDrawerVisible" :detail="detailData" />
  </div>
</template>
 
<script setup>
  import { computed, ref } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import { useUserStore } from '@/store/modules/user'
  import { useTable } from '@/hooks/core/useTable'
  import { usePrintExportPage } from '@/views/system/common/usePrintExportPage'
  import ListExportPrint from '@/components/biz/list-export-print/index.vue'
  import { defaultResponseAdapter } from '@/utils/table/tableUtils'
  import { guardRequestWithMessage } from '@/utils/sys/requestGuard'
  import {
    fetchCheckDiffItemPage,
    fetchDeleteCheckDiffItem,
    fetchExportCheckDiffItemReport,
    fetchGetCheckDiffItemDetail,
    fetchGetCheckDiffItemMany,
    fetchUpdateCheckDiffItem
  } from '@/api/check-diff'
  import CheckDiffItemDetailDrawer from './modules/check-diff-item-detail-drawer.vue'
  import {
    CHECK_DIFF_ITEM_REPORT_TITLE,
    buildCheckDiffItemPageQueryParams,
    buildCheckDiffItemPrintRows,
    buildCheckDiffItemReportMeta,
    buildCheckDiffItemSearchParams,
    createCheckDiffItemSearchState,
    normalizeCheckDiffItemRow
  } from './checkDiffItemPage.helpers'
  import { createCheckDiffItemTableColumns } from './checkDiffItemTable.columns'
 
  defineOptions({ name: 'CheckDiffItem' })
 
  const userStore = useUserStore()
  const reportTitle = CHECK_DIFF_ITEM_REPORT_TITLE
  const searchForm = ref(createCheckDiffItemSearchState())
  const selectedRows = ref([])
  const detailDrawerVisible = ref(false)
  const detailData = ref({})
 
  const reportQueryParams = computed(() => buildCheckDiffItemSearchParams(searchForm.value))
 
  const searchItems = computed(() => [
    { label: '关键字', key: 'condition', type: 'input', props: { clearable: true, placeholder: '请输入盘点单号/物料编码/托盘码' } },
    { label: '盘点单ID', key: 'checkId', type: 'input', props: { clearable: true, placeholder: '请输入盘点单ID' } },
    { label: '盘点单号', key: 'orderCode', type: 'input', props: { clearable: true, placeholder: '请输入盘点单号' } },
    { label: '物料编码', key: 'matnrCode', type: 'input', props: { clearable: true, placeholder: '请输入物料编码' } },
    { label: '托盘码', key: 'barcode', type: 'input', props: { clearable: true, placeholder: '请输入托盘码' } },
    { label: '差异原因', key: 'reason', type: 'input', props: { clearable: true, placeholder: '请输入差异原因' } },
    {
      label: '盘点状态',
      key: 'exceStatus',
      type: 'select',
      props: {
        clearable: true,
        options: [
          { label: '待处理', value: 0 },
          { label: '处理中', value: 1 },
          { label: '已完成', value: 2 }
        ]
      }
    }
  ])
 
  function openDetail(row) {
    detailDrawerVisible.value = true
    detailData.value = normalizeCheckDiffItemRow(row)
  }
 
  async function handleApprove(row) {
    try {
      await ElMessageBox.confirm(`确定审批通过 ${row.orderCode || ''} / ${row.matnrCode || ''} 吗?`, '审批确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await fetchUpdateCheckDiffItem({ ...row, exceStatus: 2 })
      ElMessage.success('审批成功')
      await refreshData()
    } catch (error) {
      if (error === 'cancel' || error?.message === 'cancel') return
      ElMessage.error(error?.message || '审批失败')
    }
  }
 
  function handleSelectionChange(rows) {
    selectedRows.value = Array.isArray(rows) ? rows : []
  }
 
  async function handleDelete(row) {
    try {
      await ElMessageBox.confirm(`确定删除差异明细 ${row.orderCode || ''} / ${row.matnrCode || ''} 吗?`, '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await fetchDeleteCheckDiffItem(row.id)
      ElMessage.success('删除成功')
      await refreshData()
    } catch (error) {
      if (error === 'cancel' || error?.message === 'cancel') return
      ElMessage.error(error?.message || '删除失败')
    }
  }
 
  async function handleActionClick(action, row) {
    if (action?.disabled) return
    if (action.key === 'view') {
      openDetail(row)
      return
    }
    if (action.key === 'approve') {
      await handleApprove(row)
      return
    }
    if (action.key === 'delete') {
      await handleDelete(row)
    }
  }
 
  const {
    columns,
    columnChecks,
    data,
    loading,
    pagination,
    replaceSearchParams,
    resetSearchParams,
    handleSizeChange,
    handleCurrentChange,
    refreshData,
    getData
  } = useTable({
    core: {
      apiFn: fetchCheckDiffItemPage,
      apiParams: buildCheckDiffItemPageQueryParams(searchForm.value),
      columnsFactory: () => createCheckDiffItemTableColumns({ handleView: openDetail, handleApprove })
    },
    transform: {
      dataTransformer: (records) => (Array.isArray(records) ? records.map((item) => normalizeCheckDiffItemRow(item)) : [])
    }
  })
 
  const resolvePrintRecords = async (payload) => {
    if (Array.isArray(payload?.ids) && payload.ids.length > 0) {
      return defaultResponseAdapter(await fetchGetCheckDiffItemMany(payload.ids)).records
    }
    return defaultResponseAdapter(
      await fetchCheckDiffItemPage({
        ...reportQueryParams.value,
        current: 1,
        pageSize: Number(pagination.total) > 0 ? Number(pagination.total) : 20
      })
    ).records
  }
 
  const { previewVisible, previewRows, previewMeta, handlePreviewVisibleChange, handleExport, handlePrint } =
    usePrintExportPage({
      downloadFileName: 'check-diff-item.xlsx',
      requestExport: (payload) =>
        fetchExportCheckDiffItemReport(payload, {
          headers: {
            Authorization: userStore.accessToken || ''
          }
        }),
      resolvePrintRecords,
      buildPreviewRows: (records) => buildCheckDiffItemPrintRows(records),
      buildPreviewMeta: (rows) => buildCheckDiffItemReportMeta(rows)
    })
 
  function handleSearch(params) {
    searchForm.value = { ...searchForm.value, ...params }
    replaceSearchParams(buildCheckDiffItemSearchParams(searchForm.value))
    getData()
  }
 
  function handleReset() {
    searchForm.value = createCheckDiffItemSearchState()
    resetSearchParams()
  }
</script>