zhou zhou
36 分钟以前 03167ec81343fb8bf8967da13768a1137c89b24d
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<template>
  <div class="check-diff-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>
 
    <CheckDiffDetailDrawer
      v-model:visible="detailDrawerVisible"
      :loading="detailLoading"
      :detail="detailData"
      :items="detailItemRows"
      :columns="detailColumns"
      :pagination="detailPagination"
      @size-change="handleDetailSizeChange"
      @current-change="handleDetailCurrentChange"
    />
  </div>
</template>
 
<script setup>
  import { computed, reactive, 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,
    fetchCheckDiffPage,
    fetchDeleteCheckDiff,
    fetchExportCheckDiffReport,
    fetchGetCheckDiffDetail,
    fetchGetCheckDiffMany
  } from '@/api/check-diff'
  import CheckDiffDetailDrawer from './modules/check-diff-detail-drawer.vue'
  import {
    CHECK_DIFF_REPORT_TITLE,
    buildCheckDiffDetailQueryParams,
    buildCheckDiffPageQueryParams,
    buildCheckDiffPrintRows,
    buildCheckDiffReportMeta,
    buildCheckDiffSearchParams,
    createCheckDiffSearchState,
    normalizeCheckDiffRow
  } from './checkDiffPage.helpers'
  import { createCheckDiffTableColumns } from './checkDiffTable.columns'
  import { createCheckDiffItemTableColumns } from '../check-diff-item/checkDiffItemTable.columns'
  import { normalizeCheckDiffItemRow } from '../check-diff-item/checkDiffItemPage.helpers'
 
  defineOptions({ name: 'CheckDiff' })
 
  const userStore = useUserStore()
  const reportTitle = CHECK_DIFF_REPORT_TITLE
  const searchForm = ref(createCheckDiffSearchState())
  const selectedRows = ref([])
  const detailDrawerVisible = ref(false)
  const detailLoading = ref(false)
  const detailData = ref({})
  const detailItemRows = ref([])
  const activeCheckDiffId = ref(null)
 
  const detailPagination = reactive({
    current: 1,
    size: 20,
    total: 0
  })
 
  const reportQueryParams = computed(() => buildCheckDiffSearchParams(searchForm.value))
  const detailColumns = computed(() => createCheckDiffItemTableColumns({ handleView: () => {} }))
  const searchItems = computed(() => [
    { label: '关键字', key: 'condition', type: 'input', props: { clearable: true, placeholder: '请输入盘点单号/库区名称/备注' } },
    { label: '盘点单号', key: 'orderCode', type: 'input', props: { clearable: true, placeholder: '请输入盘点单号' } },
    {
      label: '盘点类型',
      key: 'checkType',
      type: 'select',
      props: { clearable: true, options: [{ label: '明盘', value: 0 }, { label: '暗盘', value: 1 }] }
    },
    {
      label: '单据状态',
      key: 'exceStatus',
      type: 'select',
      props: {
        clearable: true,
        options: [
          { label: '未执行', value: 0 },
          { label: '执行中', value: 1 },
          { label: '已完成', value: 2 },
          { label: '已取消', value: 8 }
        ]
      }
    },
    { label: '库区ID', key: 'areaId', type: 'input', props: { clearable: true, placeholder: '请输入库区ID' } },
    { label: '库区名称', key: 'areaName', type: 'input', props: { clearable: true, placeholder: '请输入库区名称' } },
    { label: '备注', key: 'memo', type: 'input', props: { clearable: true, placeholder: '请输入备注' } }
  ])
 
  function updatePaginationState(target, response, fallbackCurrent, fallbackSize) {
    target.total = Number(response?.total || 0)
    target.current = Number(response?.current || fallbackCurrent || 1)
    target.size = Number(response?.size || fallbackSize || target.size || 20)
  }
 
  async function openDetail(row) {
    activeCheckDiffId.value = row.id
    detailPagination.current = 1
    detailDrawerVisible.value = true
    await loadDetailResources()
  }
 
  async function handleDelete(row) {
    try {
      await ElMessageBox.confirm(`确定删除盘点差异单 ${row.orderCode || ''} 吗?`, '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await fetchDeleteCheckDiff(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') {
      await openDetail(row)
      return
    }
    if (action.key === 'print') {
      await handlePrint({ ids: [row.id], pageSize: 1 })
      return
    }
    if (action.key === 'delete') {
      await handleDelete(row)
    }
  }
 
  const {
    columns,
    columnChecks,
    data,
    loading,
    pagination,
    replaceSearchParams,
    resetSearchParams,
    handleSizeChange,
    handleCurrentChange,
    refreshData,
    getData
  } = useTable({
    core: {
      apiFn: fetchCheckDiffPage,
      apiParams: buildCheckDiffPageQueryParams(searchForm.value),
      columnsFactory: () => createCheckDiffTableColumns({ handleActionClick })
    },
    transform: {
      dataTransformer: (records) => (Array.isArray(records) ? records.map((item) => normalizeCheckDiffRow(item)) : [])
    }
  })
 
  async function loadDetailResources() {
    if (!activeCheckDiffId.value) return
    detailLoading.value = true
    try {
      const [detailResponse, itemResponse] = await Promise.all([
        guardRequestWithMessage(fetchGetCheckDiffDetail(activeCheckDiffId.value), {}, { timeoutMessage: '盘点差异单详情加载超时,已停止等待' }),
        guardRequestWithMessage(
          fetchCheckDiffItemPage(
            buildCheckDiffDetailQueryParams({
              checkId: activeCheckDiffId.value,
              current: detailPagination.current,
              pageSize: detailPagination.size
            })
          ),
          { records: [], total: 0, current: detailPagination.current, size: detailPagination.size },
          { timeoutMessage: '盘点差异单明细加载超时,已停止等待' }
        )
      ])
      detailData.value = normalizeCheckDiffRow(detailResponse)
      detailItemRows.value = Array.isArray(itemResponse?.records)
        ? itemResponse.records.map((item) => normalizeCheckDiffItemRow(item))
        : []
      updatePaginationState(detailPagination, itemResponse, detailPagination.current, detailPagination.size)
    } finally {
      detailLoading.value = false
    }
  }
 
  function handleSelectionChange(rows) {
    selectedRows.value = Array.isArray(rows) ? rows : []
  }
 
  function handleSearch(params) {
    searchForm.value = { ...searchForm.value, ...params }
    replaceSearchParams(buildCheckDiffSearchParams(searchForm.value))
    getData()
  }
 
  function handleReset() {
    searchForm.value = createCheckDiffSearchState()
    resetSearchParams()
  }
 
  function handleDetailSizeChange(size) {
    detailPagination.size = size
    loadDetailResources()
  }
 
  function handleDetailCurrentChange(current) {
    detailPagination.current = current
    loadDetailResources()
  }
 
  const resolvePrintRecords = async (payload) => {
    if (Array.isArray(payload?.ids) && payload.ids.length > 0) {
      return defaultResponseAdapter(await fetchGetCheckDiffMany(payload.ids)).records
    }
    return defaultResponseAdapter(
      await fetchCheckDiffPage({
        ...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.xlsx',
      requestExport: (payload) =>
        fetchExportCheckDiffReport(payload, {
          headers: {
            Authorization: userStore.accessToken || ''
          }
        }),
      resolvePrintRecords,
      buildPreviewRows: (records) => buildCheckDiffPrintRows(records),
      buildPreviewMeta: (rows) => buildCheckDiffReportMeta(rows)
    })
</script>