zhou zhou
10 小时以前 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<template>
  <div class="bas-station-area-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>
          <ElSpace wrap>
            <ElButton v-auth="'add'" @click="handleShowDialog('add')" v-ripple
              >新增站点区域</ElButton
            >
            <ElButton
              v-auth="'delete'"
              type="danger"
              :disabled="selectedRows.length === 0"
              @click="handleBatchDelete"
              v-ripple
            >
              批量删除
            </ElButton>
          </ElSpace>
        </template>
      </ArtTableHeader>
 
      <ArtTable
        :loading="loading"
        :data="data"
        :columns="columns"
        :pagination="pagination"
        @selection-change="handleSelectionChange"
        @pagination:size-change="handleSizeChange"
        @pagination:current-change="handleCurrentChange"
      />
 
      <BasStationAreaDialog
        v-model:visible="dialogVisible"
        :dialog-type="dialogType"
        :bas-station-area-data="currentBasStationAreaData"
        :area-options="areaOptions"
        :cross-zone-area-options="crossZoneAreaOptions"
        :container-type-options="containerTypeOptions"
        :station-options="stationOptions"
        :use-status-options="useStatusOptions"
        @submit="handleDialogSubmit"
      />
 
      <BasStationAreaDetailDrawer
        v-model:visible="detailDrawerVisible"
        :loading="detailLoading"
        :detail="detailData"
      />
    </ElCard>
  </div>
</template>
 
<script setup>
  import { computed, onMounted, ref } from 'vue'
  import { ElMessage } from 'element-plus'
  import { useAuth } from '@/hooks/core/useAuth'
  import { useTable } from '@/hooks/core/useTable'
  import { useCrudPage } from '@/views/system/common/useCrudPage'
  import { defaultResponseAdapter } from '@/utils/table/tableUtils'
  import { guardRequestWithMessage } from '@/utils/sys/requestGuard'
  import { fetchDictDataPage } from '@/api/system-manage'
  import { fetchBasStationOptionPage } from '@/api/bas-station'
  import { fetchWarehouseAreasList } from '@/api/warehouse-areas'
  import {
    fetchBasStationAreaDetail,
    fetchBasStationAreaPage,
    fetchDeleteBasStationArea,
    fetchSaveBasStationArea,
    fetchUpdateBasStationArea
  } from '@/api/bas-station-area'
  import BasStationAreaDialog from './modules/bas-station-area-dialog.vue'
  import BasStationAreaDetailDrawer from './modules/bas-station-area-detail-drawer.vue'
  import { createBasStationAreaTableColumns } from './basStationAreaTable.columns'
  import {
    buildBasStationAreaDialogModel,
    buildBasStationAreaPageQueryParams,
    buildBasStationAreaSavePayload,
    buildBasStationAreaSearchParams,
    createBasStationAreaSearchState,
    getBasStationAreaBinaryOptions,
    getBasStationAreaPaginationKey,
    getBasStationAreaStatusOptions,
    getBasStationAreaTypeOptions,
    normalizeBasStationAreaDetailRecord,
    normalizeBasStationAreaListRow,
    resolveBasStationAreaContainerTypeOptions,
    resolveBasStationAreaStationOptions,
    resolveBasStationAreaUseStatusOptions,
    resolveBasStationAreaWarehouseAreaOptions
  } from './basStationAreaPage.helpers'
 
  defineOptions({ name: 'BasStationArea' })
 
  const { hasAuth } = useAuth()
 
  const searchForm = ref(createBasStationAreaSearchState())
  const detailDrawerVisible = ref(false)
  const detailLoading = ref(false)
  const detailData = ref({})
  const areaOptions = ref([])
  const crossZoneAreaOptions = ref([])
  const containerTypeOptions = ref([])
  const stationOptions = ref([])
  const useStatusOptions = ref([])
  const stationOptionsLoaded = ref(false)
  const stationOptionsLoading = ref(null)
  let handleDeleteAction = null
 
  const areaLabelMap = computed(
    () =>
      new Map(
        areaOptions.value
          .map((item) => [String(item.value), item.label])
          .filter(([value, label]) => value && label)
      )
  )
  const containerTypeLabelMap = computed(
    () =>
      new Map(
        containerTypeOptions.value
          .map((item) => [String(item.value), item.label])
          .filter(([value, label]) => value && label)
      )
  )
  const typeLabelMap = computed(
    () =>
      new Map(
        getBasStationAreaTypeOptions()
          .map((item) => [String(item.value), item.label])
          .filter(([value, label]) => value && label)
      )
  )
  const stationLabelMap = computed(
    () =>
      new Map(
        stationOptions.value
          .map((item) => [String(item.value), item.label])
          .filter(([value, label]) => value && label)
      )
  )
  const useStatusLabelMap = computed(
    () =>
      new Map(
        useStatusOptions.value
          .map((item) => [String(item.value), item.label])
          .filter(([value, label]) => value && label)
      )
  )
 
  const resolveAreaLabel = (id) => areaLabelMap.value.get(String(id)) || ''
  const resolveCrossZoneAreaLabel = (id) => areaLabelMap.value.get(String(id)) || ''
  const resolveContainerTypeLabel = (id) => containerTypeLabelMap.value.get(String(id)) || ''
  const resolveTypeLabel = (value) => typeLabelMap.value.get(String(value)) || ''
  const resolveStationAliasLabel = (id) => stationLabelMap.value.get(String(id)) || ''
  const resolveUseStatusLabel = (value) => useStatusLabelMap.value.get(String(value)) || ''
 
  const searchItems = computed(() => [
    {
      label: '关键字',
      key: 'condition',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入站点区域名称/编号/备注'
      }
    },
    {
      label: '站点区域名称',
      key: 'stationAreaName',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入站点区域名称'
      }
    },
    {
      label: '站点区域编号',
      key: 'stationAreaId',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入站点区域编号'
      }
    },
    {
      label: '站点类型',
      key: 'type',
      type: 'select',
      props: {
        clearable: true,
        options: getBasStationAreaTypeOptions()
      }
    },
    {
      label: '所属库区',
      key: 'area',
      type: 'select',
      props: {
        clearable: true,
        filterable: true,
        options: areaOptions.value
      }
    },
    {
      label: '使用状态',
      key: 'useStatus',
      type: 'select',
      props: {
        clearable: true,
        filterable: true,
        options: useStatusOptions.value
      }
    },
    {
      label: '可入',
      key: 'inAble',
      type: 'select',
      props: {
        clearable: true,
        options: getBasStationAreaBinaryOptions()
      }
    },
    {
      label: '可出',
      key: 'outAble',
      type: 'select',
      props: {
        clearable: true,
        options: getBasStationAreaBinaryOptions()
      }
    },
    {
      label: '是否跨区',
      key: 'isCrossZone',
      type: 'select',
      props: {
        clearable: true,
        options: getBasStationAreaBinaryOptions()
      }
    },
    {
      label: '是否WCS',
      key: 'isWcs',
      type: 'select',
      props: {
        clearable: true,
        options: getBasStationAreaBinaryOptions()
      }
    },
    {
      label: '自动调拨',
      key: 'autoTransfer',
      type: 'select',
      props: {
        clearable: true,
        options: getBasStationAreaBinaryOptions()
      }
    },
    {
      label: '条码',
      key: 'barcode',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入条码'
      }
    },
    {
      label: '状态',
      key: 'status',
      type: 'select',
      props: {
        clearable: true,
        options: getBasStationAreaStatusOptions()
      }
    },
    {
      label: '备注',
      key: 'memo',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入备注'
      }
    }
  ])
 
  async function openDetail(row) {
    detailDrawerVisible.value = true
    detailLoading.value = true
    try {
      const detail = await guardRequestWithMessage(
        fetchBasStationAreaDetail(row.id),
        {},
        {
          timeoutMessage: '站点区域详情加载超时,已停止等待'
        }
      )
      detailData.value = normalizeBasStationAreaDetailRecord(detail, {
        resolveAreaLabel,
        resolveCrossZoneAreaLabel,
        resolveContainerTypeLabel,
        resolveTypeLabel,
        resolveStationAliasLabel,
        resolveUseStatusLabel
      })
    } catch (error) {
      detailDrawerVisible.value = false
      detailData.value = {}
      ElMessage.error(error?.message || '获取站点区域详情失败')
    } finally {
      detailLoading.value = false
    }
  }
 
  async function ensureStationOptions() {
    if (stationOptionsLoaded.value) {
      return
    }
 
    if (stationOptionsLoading.value) {
      await stationOptionsLoading.value
      return
    }
 
    stationOptionsLoading.value = (async () => {
      const response = await guardRequestWithMessage(
        fetchBasStationOptionPage(
          {
            current: 1,
            pageSize: 100
          },
          {
            showErrorMessage: false
          }
        ),
        { records: [] },
        {
          timeoutMessage: '站点别名选项加载超时,已停止等待'
        }
      )
      stationOptions.value = resolveBasStationAreaStationOptions(
        defaultResponseAdapter(response).records
      )
      stationOptionsLoaded.value = true
    })()
 
    try {
      await stationOptionsLoading.value
    } finally {
      stationOptionsLoading.value = null
    }
  }
 
  async function handleShowDialog(type, record) {
    await ensureStationOptions()
    showDialog(type, record)
  }
 
  async function openEditDialog(row) {
    try {
      const detail = await guardRequestWithMessage(
        fetchBasStationAreaDetail(row.id),
        {},
        {
          timeoutMessage: '站点区域详情加载超时,已停止等待'
        }
      )
      await handleShowDialog('edit', detail)
    } catch (error) {
      ElMessage.error(error?.message || '获取站点区域详情失败')
    }
  }
 
  const {
    columns,
    columnChecks,
    data,
    loading,
    pagination,
    getData,
    replaceSearchParams,
    resetSearchParams,
    handleSizeChange,
    handleCurrentChange,
    refreshData,
    refreshCreate,
    refreshUpdate,
    refreshRemove
  } = useTable({
    core: {
      apiFn: fetchBasStationAreaPage,
      apiParams: buildBasStationAreaPageQueryParams(searchForm.value),
      paginationKey: getBasStationAreaPaginationKey(),
      columnsFactory: () =>
        createBasStationAreaTableColumns({
          handleView: openDetail,
          handleEdit: hasAuth('update') ? openEditDialog : null,
          handleDelete: hasAuth('delete') ? (row) => handleDeleteAction?.(row) : null,
          canEdit: hasAuth('update'),
          canDelete: hasAuth('delete')
        })
    },
    transform: {
      dataTransformer: (records) => {
        if (!Array.isArray(records)) {
          return []
        }
        return records.map((item) =>
          normalizeBasStationAreaListRow(item, {
            resolveAreaLabel,
            resolveCrossZoneAreaLabel,
            resolveContainerTypeLabel,
            resolveTypeLabel,
            resolveStationAliasLabel,
            resolveUseStatusLabel
          })
        )
      }
    }
  })
 
  const {
    dialogVisible,
    dialogType,
    currentRecord: currentBasStationAreaData,
    selectedRows,
    handleSelectionChange,
    showDialog,
    handleDialogSubmit,
    handleDelete,
    handleBatchDelete
  } = useCrudPage({
    createEmptyModel: () => buildBasStationAreaDialogModel(),
    buildEditModel: (record) => buildBasStationAreaDialogModel(record),
    buildSavePayload: (formData) => buildBasStationAreaSavePayload(formData),
    saveRequest: fetchSaveBasStationArea,
    updateRequest: fetchUpdateBasStationArea,
    deleteRequest: fetchDeleteBasStationArea,
    entityName: '站点区域',
    resolveRecordLabel: (record) => record?.stationAreaName || record?.stationAreaId || record?.id,
    refreshCreate,
    refreshUpdate,
    refreshRemove
  })
  handleDeleteAction = handleDelete
 
  function handleSearch(params) {
    replaceSearchParams(buildBasStationAreaSearchParams(params))
    getData()
  }
 
  function handleReset() {
    Object.assign(searchForm.value, createBasStationAreaSearchState())
    resetSearchParams()
  }
 
  async function loadAreaOptions() {
    const response = await guardRequestWithMessage(fetchWarehouseAreasList(), [], {
      timeoutMessage: '库区选项加载超时,已停止等待'
    })
    const options = resolveBasStationAreaWarehouseAreaOptions(
      defaultResponseAdapter(response).records
    )
    areaOptions.value = options
    crossZoneAreaOptions.value = options
  }
 
  async function loadContainerTypeOptions() {
    const response = await guardRequestWithMessage(
      fetchDictDataPage({
        current: 1,
        pageSize: 200,
        dictTypeCode: 'sys_container_type',
        status: 1
      }),
      { records: [] },
      { timeoutMessage: '容器类型选项加载超时,已停止等待' }
    )
    containerTypeOptions.value = resolveBasStationAreaContainerTypeOptions(
      defaultResponseAdapter(response).records
    )
  }
 
  async function loadUseStatusOptions() {
    const response = await guardRequestWithMessage(
      fetchDictDataPage({
        current: 1,
        pageSize: 200,
        dictTypeCode: 'sys_sta_use_stas',
        status: 1
      }),
      { records: [] },
      { timeoutMessage: '使用状态选项加载超时,已停止等待' }
    )
    useStatusOptions.value = resolveBasStationAreaUseStatusOptions(
      defaultResponseAdapter(response).records
    )
  }
 
  onMounted(async () => {
    await Promise.allSettled([
      loadAreaOptions(),
      loadContainerTypeOptions(),
      loadUseStatusOptions()
    ])
    await getData()
  })
</script>