1
2 天以前 3a4a8c78098f41d3a7ce41f272cdefc35b572681
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<template>
  <div class="preparation-item-page art-full-height">
    <ElCard v-if="activeSourceSummary" class="mb-3">
      <div class="flex items-center justify-between gap-3">
        <div class="flex items-center gap-2 text-sm text-[var(--art-text-gray-600)]">
          <span class="font-medium text-[var(--art-text-gray-900)]">当前来源</span>
          <span>备料单ID:{{ activeSourceSummary.orderId }}</span>
        </div>
        <ElButton link type="primary" @click="handleClearSourceFilter">查看全部</ElButton>
      </div>
    </ElCard>
 
    <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
            class="inline-flex"
            :preview-visible="previewVisible"
            @update:previewVisible="handlePreviewVisibleChange"
            :report-title="reportTitle"
            :selected-rows="selectedRows"
            :query-params="reportQueryParams"
            :columns="reportColumns"
            :preview-rows="previewRows"
            :preview-meta="resolvedPreviewMeta"
            :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>
 
    <OutStockItemDetailDrawer
      v-model:visible="detailDrawerVisible"
      :loading="detailLoading"
      :detail="detailData"
    />
  </div>
</template>
 
<script setup>
  import { computed, onMounted, ref, watch } from 'vue'
  import { useRoute, useRouter } from 'vue-router'
  import { ElButton, ElMessage } from 'element-plus'
  import { useUserStore } from '@/store/modules/user'
  import { useTable } from '@/hooks/core/useTable'
  import { defaultResponseAdapter } from '@/utils/table/tableUtils'
  import { guardRequestWithMessage } from '@/utils/sys/requestGuard'
  import { usePrintExportPage } from '@/views/system/common/usePrintExportPage'
  import ListExportPrint from '@/components/biz/list-export-print/index.vue'
  import {
    fetchExportPreparationItemReport,
    fetchGetPreparationItemDetail,
    fetchGetPreparationItemMany,
    fetchPreparationItemPage
  } from '@/api/preparation-item'
  import { createOutStockItemTableColumns } from '../out-stock-item/outStockItemTable.columns.js'
  import OutStockItemDetailDrawer from '../out-stock-item/modules/out-stock-item-detail-drawer.vue'
  import {
    PREPARATION_ITEM_REPORT_STYLE,
    PREPARATION_ITEM_REPORT_TITLE,
    buildPreparationItemPageQueryParams,
    buildPreparationItemPrintRows,
    buildPreparationItemReportMeta,
    buildPreparationItemSearchParams,
    createPreparationItemSearchState,
    getPreparationItemPaginationKey,
    getPreparationItemReportColumns,
    normalizePreparationItemRow
  } from './preparationItemPage.helpers.js'
 
  defineOptions({ name: 'PreparationItem' })
 
  const route = useRoute()
  const router = useRouter()
  const userStore = useUserStore()
  const initialOrderId = route.query.orderId || route.query.id
  const searchForm = ref(
    createPreparationItemSearchState({
      orderId: initialOrderId !== undefined ? Number(initialOrderId) || '' : ''
    })
  )
  const detailDrawerVisible = ref(false)
  const detailLoading = ref(false)
  const detailData = ref({})
  const selectedRows = ref([])
  const reportTitle = PREPARATION_ITEM_REPORT_TITLE
  const reportColumns = getPreparationItemReportColumns()
  const reportQueryParams = computed(() => buildPreparationItemSearchParams(searchForm.value))
  const activeSourceSummary = computed(() => {
    if (
      searchForm.value.orderId === '' ||
      searchForm.value.orderId === undefined ||
      searchForm.value.orderId === null
    ) {
      return null
    }
    return {
      orderId: searchForm.value.orderId
    }
  })
 
  const searchItems = computed(() => [
    {
      label: '关键字',
      key: 'condition',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入备料单号/物料编码/物料名称'
      }
    },
    {
      label: '备料单ID',
      key: 'orderId',
      type: 'inputNumber',
      props: {
        clearable: true,
        controlsPosition: 'right',
        placeholder: '请输入备料单ID'
      }
    },
    {
      label: '备料单号',
      key: 'orderCode',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入备料单号'
      }
    },
    {
      label: 'PO单号',
      key: 'poCode',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入PO单号'
      }
    },
    {
      label: '物料编码',
      key: 'matnrCode',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入物料编码'
      }
    },
    {
      label: '物料名称',
      key: 'maktx',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入物料名称'
      }
    },
    {
      label: '批次',
      key: 'batch',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入批次'
      }
    },
    {
      label: '供应商批次',
      key: 'splrBatch',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入供应商批次'
      }
    },
    {
      label: '字段索引',
      key: 'fieldsIndex',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入字段索引'
      }
    }
  ])
 
  async function openDetail(row) {
    detailDrawerVisible.value = true
    detailLoading.value = true
    try {
      const detail = await guardRequestWithMessage(fetchGetPreparationItemDetail(row.id), {}, {
        timeoutMessage: '备料单明细详情加载超时,已停止等待'
      })
      detailData.value = normalizePreparationItemRow({
        ...row,
        ...(detail || {})
      })
    } catch (error) {
      detailDrawerVisible.value = false
      detailData.value = {}
      ElMessage.error(error?.message || '获取备料单明细详情失败')
    } finally {
      detailLoading.value = false
    }
  }
 
  const {
    columns,
    columnChecks,
    data,
    loading,
    pagination,
    getData,
    replaceSearchParams,
    resetSearchParams,
    handleSizeChange,
    handleCurrentChange,
    refreshData
  } = useTable({
    core: {
      apiFn: fetchPreparationItemPage,
      apiParams: buildPreparationItemPageQueryParams(searchForm.value),
      paginationKey: getPreparationItemPaginationKey(),
      columnsFactory: () => createOutStockItemTableColumns({ handleActionClick: openDetail })
    },
    transform: {
      dataTransformer: (records) =>
        Array.isArray(records) ? records.map((item) => normalizePreparationItemRow(item)) : []
    }
  })
 
  function handleSelectionChange(rows) {
    selectedRows.value = Array.isArray(rows) ? rows : []
  }
 
  function handleSearch(params) {
    searchForm.value = {
      ...searchForm.value,
      ...params
    }
    replaceSearchParams(buildPreparationItemPageQueryParams(searchForm.value))
    getData()
  }
 
  function handleReset() {
    const resetSeed =
      initialOrderId !== undefined ? { orderId: Number(initialOrderId) || '' } : {}
    Object.assign(searchForm.value, createPreparationItemSearchState(resetSeed))
    resetSearchParams(buildPreparationItemPageQueryParams(createPreparationItemSearchState(resetSeed)))
  }
 
  function applyRouteSearch() {
    const orderId = route.query.orderId || route.query.id
    if (orderId === undefined || orderId === null || orderId === '') {
      return
    }
    searchForm.value.orderId = Number.isFinite(Number(orderId))
      ? Number(orderId)
      : searchForm.value.orderId
  }
 
  function handleClearSourceFilter() {
    searchForm.value.orderId = ''
    router.replace({
      path: route.path,
      query: {
        ...route.query,
        orderId: undefined,
        id: undefined
      }
    })
    replaceSearchParams(buildPreparationItemPageQueryParams(searchForm.value))
    getData()
  }
 
  watch(
    () => [route.query.orderId, route.query.id],
    ([orderId, id]) => {
      if (
        (orderId === undefined || orderId === null || orderId === '') &&
        (id === undefined || id === null || id === '')
      ) {
        return
      }
      applyRouteSearch()
      replaceSearchParams(buildPreparationItemPageQueryParams(searchForm.value))
      getData()
    }
  )
 
  const resolvePrintRecords = async (payload) => {
    if (Array.isArray(payload?.ids) && payload.ids.length > 0) {
      return defaultResponseAdapter(await fetchGetPreparationItemMany(payload.ids)).records
    }
    return defaultResponseAdapter(
      await fetchPreparationItemPage({
        ...reportQueryParams.value,
        current: 1,
        pageSize: Number(pagination.total) > 0 ? Number(pagination.total) : 20
      })
    ).records
  }
 
  const {
    previewVisible,
    previewRows,
    previewMeta,
    handlePreviewVisibleChange,
    handleExport,
    handlePrint
  } = usePrintExportPage({
    downloadFileName: 'preparation-item.xlsx',
    requestExport: (payload) =>
      fetchExportPreparationItemReport(payload, {
        headers: {
          Authorization: userStore.accessToken || ''
        }
      }),
    resolvePrintRecords,
    buildPreviewRows: (records) => buildPreparationItemPrintRows(records),
    buildPreviewMeta: (rows) => ({
      reportTitle,
      reportDate: new Date().toLocaleDateString('zh-CN'),
      printedAt: new Date().toLocaleString('zh-CN', { hour12: false }),
      operator: userStore.getUserInfo?.name || userStore.getUserInfo?.username || '',
      count: rows.length,
      reportStyle: {
        ...PREPARATION_ITEM_REPORT_STYLE
      }
    })
  })
 
  const resolvedPreviewMeta = computed(() =>
    buildPreparationItemReportMeta({
      previewMeta: previewMeta.value,
      count: previewRows.value.length,
      orientation:
        previewMeta.value?.reportStyle?.orientation || PREPARATION_ITEM_REPORT_STYLE.orientation
    })
  )
 
  onMounted(() => {
    applyRouteSearch()
    replaceSearchParams(buildPreparationItemPageQueryParams(searchForm.value))
    getData()
  })
</script>