zhou zhou
3 小时以前 89847f0c5a5d37e5a720afd32cdd7e4d9ead664b
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
<template>
  <div class="check-out-bound-page art-full-height">
    <ArtSearchBar
      v-model="searchForm"
      :items="searchItems"
      :showExpand="true"
      @search="handleSearch"
      @reset="handleReset"
    />
 
    <ElCard class="art-table-card mb-4">
      <div class="flex flex-wrap items-center gap-3">
        <div class="text-sm text-[var(--art-text-gray-600)]">
          已选择 <span class="font-medium text-[var(--art-text-gray-800)]">{{ selectedRows.length }}</span> 条库位物料
        </div>
        <ElSelect
          v-model="siteNo"
          class="min-w-56"
          filterable
          clearable
          placeholder="请选择站点"
          :loading="siteLoading"
          :disabled="siteLoading"
        >
          <ElOption
            v-for="item in siteOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </ElSelect>
        <ElButton
          type="primary"
          :loading="generateLoading"
          :disabled="!canGenerateTask"
          @click="handleGenerateTask"
        >
          生成盘点出库任务
        </ElButton>
        <ElButton :disabled="selectedRows.length === 0" @click="clearSelection">清空选择</ElButton>
      </div>
    </ElCard>
 
    <ElCard class="art-table-card">
      <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData" />
 
      <ArtTable
        :loading="loading"
        :data="data"
        :columns="columns"
        :pagination="pagination"
        @selection-change="handleSelectionChange"
        @pagination:size-change="handleSizeChange"
        @pagination:current-change="handleCurrentChange"
      />
    </ElCard>
 
    <CheckOutBoundDetailDrawer
      v-model:visible="detailDrawerVisible"
      :loading="detailLoading"
      :detail="detailData"
    />
  </div>
</template>
 
<script setup>
  import { computed, onMounted, ref, watch } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import { useTable } from '@/hooks/core/useTable'
  import { guardRequestWithMessage } from '@/utils/sys/requestGuard'
  import {
    buildCheckOutBoundPageQueryParams,
    buildCheckOutBoundTaskPayload,
    createCheckOutBoundSearchState,
    normalizeCheckOutBoundRow,
    resolveCheckOutBoundSiteOptions
  } from './checkOutBoundPage.helpers'
  import {
    fetchCheckOutBoundPage,
    fetchCheckOutBoundSites,
    fetchGenerateCheckOutBoundTask,
    fetchGetCheckOutBoundDetail
  } from '@/api/check-out-bound'
  import CheckOutBoundDetailDrawer from './modules/check-out-bound-detail-drawer.vue'
  import { createCheckOutBoundTableColumns } from './checkOutBoundTable.columns'
 
  defineOptions({ name: 'CheckOutBound' })
 
  const searchForm = ref(createCheckOutBoundSearchState())
  const selectedRows = ref([])
  const siteOptions = ref([])
  const siteLoading = ref(false)
  const siteNo = ref('')
  const memo = ref('')
  const detailDrawerVisible = ref(false)
  const detailLoading = ref(false)
  const detailData = ref({})
  const generateLoading = ref(false)
 
  const searchItems = computed(() => [
    {
      label: '关键字',
      key: 'condition',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入库位编码/物料编码/物料名称/批次'
      }
    },
    {
      label: '库位编码',
      key: 'locCode',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入库位编码'
      }
    },
    {
      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: 'trackCode',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入追踪码'
      }
    }
  ])
 
  const {
    columns,
    columnChecks,
    data,
    loading,
    pagination,
    replaceSearchParams,
    resetSearchParams,
    handleSizeChange: handleTableSizeChange,
    handleCurrentChange: handleTableCurrentChange,
    refreshData
  } = useTable({
    core: {
      apiFn: fetchCheckOutBoundPage,
      apiParams: buildCheckOutBoundPageQueryParams(searchForm.value),
      columnsFactory: () =>
        createCheckOutBoundTableColumns({
          handleActionClick
        })
    },
    transform: {
      dataTransformer: (records) => (Array.isArray(records) ? records.map((item) => normalizeCheckOutBoundRow(item)) : [])
    }
  })
 
  const canGenerateTask = computed(() => selectedRows.value.length > 0 && Boolean(siteNo.value) && !generateLoading.value)
 
  watch(
    data,
    () => {
      selectedRows.value = []
    },
    { deep: false }
  )
 
  onMounted(loadSiteOptions)
 
  async function loadSiteOptions() {
    siteLoading.value = true
    try {
      const response = await guardRequestWithMessage(fetchCheckOutBoundSites(), [], {
        timeoutMessage: '站点选项加载超时,已停止等待'
      })
      siteOptions.value = resolveCheckOutBoundSiteOptions(response)
      if (siteOptions.value.length > 0 && !siteOptions.value.some((item) => item.value === siteNo.value)) {
        siteNo.value = ''
      }
    } catch (error) {
      siteOptions.value = []
      ElMessage.error(error?.message || '获取站点选项失败')
    } finally {
      siteLoading.value = false
    }
  }
 
  async function openDetailDrawer(row) {
    detailDrawerVisible.value = true
    detailLoading.value = true
    try {
      const detail = await guardRequestWithMessage(fetchGetCheckOutBoundDetail(row.id), {}, {
        timeoutMessage: '盘点出库明细加载超时,已停止等待'
      })
      detailData.value = normalizeCheckOutBoundRow(detail)
    } catch (error) {
      detailDrawerVisible.value = false
      detailData.value = {}
      ElMessage.error(error?.message || '获取盘点出库明细失败')
    } finally {
      detailLoading.value = false
    }
  }
 
  async function handleActionClick(action, row) {
    if (action?.disabled) return
    if (action.key === 'view') {
      await openDetailDrawer(row)
    }
  }
 
  function handleSelectionChange(rows) {
    selectedRows.value = Array.isArray(rows) ? rows : []
  }
 
  function clearSelection() {
    selectedRows.value = []
  }
 
  function handleSearch(params) {
    searchForm.value = {
      ...searchForm.value,
      ...params
    }
    selectedRows.value = []
    replaceSearchParams(buildCheckOutBoundPageQueryParams(searchForm.value))
    refreshData()
  }
 
  function handleReset() {
    searchForm.value = createCheckOutBoundSearchState()
    selectedRows.value = []
    siteNo.value = ''
    memo.value = ''
    resetSearchParams()
  }
 
  function handleSizeChange(size) {
    selectedRows.value = []
    handleTableSizeChange(size)
  }
 
  function handleCurrentChange(current) {
    selectedRows.value = []
    handleTableCurrentChange(current)
  }
 
  async function handleGenerateTask() {
    if (!siteNo.value) {
      ElMessage.warning('请选择站点')
      return
    }
    if (selectedRows.value.length === 0) {
      ElMessage.warning('请选择库位物料')
      return
    }
 
    try {
      await ElMessageBox.confirm(
        `确定基于当前所选 ${selectedRows.value.length} 条库位物料生成盘点出库任务吗?`,
        '生成确认',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
 
      generateLoading.value = true
      const payload = buildCheckOutBoundTaskPayload({
        siteNo: siteNo.value,
        memo: memo.value,
        items: selectedRows.value
      })
      await fetchGenerateCheckOutBoundTask(payload)
      ElMessage.success('任务生成成功')
      selectedRows.value = []
      refreshData()
    } catch (error) {
      if (error === 'cancel' || error?.message === 'cancel') return
      ElMessage.error(error?.message || '任务生成失败')
    } finally {
      generateLoading.value = false
    }
  }
</script>