zhou zhou
84 分钟以前 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
<template>
  <div class="warehouse-areas-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="openWarehouseAreasDialog('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"
      />
 
      <WarehouseAreasDialog
        v-model:visible="dialogVisible"
        :dialog-type="dialogType"
        :warehouse-areas-data="currentWarehouseAreasData"
        :warehouse-options="warehouseOptions"
        :shipper-options="shipperOptions"
        :supplier-options="supplierOptions"
        :type-options="typeOptions"
        @submit="handleDialogSubmit"
      />
 
      <WarehouseAreasDetailDrawer
        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 { useTableColumns } from '@/hooks/core/useTableColumns'
  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 {
    fetchCompanysList,
    fetchDeleteWarehouseAreas,
    fetchWarehouseAreasDetail,
    fetchWarehouseAreasPage,
    fetchSaveWarehouseAreas,
    fetchUpdateWarehouseAreas,
    fetchWarehouseList
  } from '@/api/warehouse-areas'
  import WarehouseAreasDialog from './modules/warehouse-areas-dialog.vue'
  import WarehouseAreasDetailDrawer from './modules/warehouse-areas-detail-drawer.vue'
  import { createWarehouseAreasTableColumns } from './warehouseAreasTable.columns'
  import {
    buildWarehouseAreasDialogModel,
    buildWarehouseAreasPageQueryParams,
    buildWarehouseAreasSavePayload,
    buildWarehouseAreasSearchParams,
    createWarehouseAreasSearchState,
    getWarehouseAreasPaginationKey,
    getWarehouseAreasStatusOptions,
    normalizeWarehouseAreasDetailRecord,
    normalizeWarehouseAreasListRow
  } from './warehouseAreasPage.helpers'
 
  defineOptions({ name: 'WarehouseAreas' })
 
  const { hasAuth } = useAuth()
 
  const searchForm = ref(createWarehouseAreasSearchState())
  const detailDrawerVisible = ref(false)
  const detailLoading = ref(false)
  const detailData = ref({})
  const warehouseOptions = ref([])
  const shipperOptions = ref([])
  const supplierOptions = ref([])
  const typeOptions = ref([])
  const companyOptionsLoaded = ref(false)
  const companyOptionsLoading = ref(null)
  let handleDeleteAction = null
 
  const searchItems = computed(() => [
    {
      label: '关键字',
      key: 'condition',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入库区名称/编码/备注'
      }
    },
    {
      label: '仓库',
      key: 'warehouseId',
      type: 'select',
      props: {
        clearable: true,
        filterable: true,
        options: warehouseOptions.value
      }
    },
    {
      label: '库区编码',
      key: 'code',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入库区编码'
      }
    },
    {
      label: '库区名称',
      key: 'name',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入库区名称'
      }
    },
    {
      label: '业务类型',
      key: 'type',
      type: 'select',
      props: {
        clearable: true,
        filterable: true,
        options: typeOptions.value
      }
    },
    {
      label: '状态',
      key: 'status',
      type: 'select',
      props: {
        clearable: true,
        options: getWarehouseAreasStatusOptions()
      }
    }
  ])
 
  async function openDetail(row) {
    detailDrawerVisible.value = true
    detailLoading.value = true
    try {
      const detail = await guardRequestWithMessage(fetchWarehouseAreasDetail(row.id), {}, {
        timeoutMessage: '库区详情加载超时,已停止等待'
      })
      detailData.value = normalizeWarehouseAreasDetailRecord(detail)
    } catch (error) {
      detailDrawerVisible.value = false
      detailData.value = {}
      ElMessage.error(error?.message || '获取库区详情失败')
    } finally {
      detailLoading.value = false
    }
  }
 
  async function openEditDialog(row) {
    try {
      await ensureCompanyOptions()
      const detail = await guardRequestWithMessage(fetchWarehouseAreasDetail(row.id), {}, {
        timeoutMessage: '库区详情加载超时,已停止等待'
      })
      showDialog('edit', detail)
    } catch (error) {
      ElMessage.error(error?.message || '获取库区详情失败')
    }
  }
 
  async function openWarehouseAreasDialog(type, record) {
    await ensureCompanyOptions()
    showDialog(type, record)
  }
 
  const { columns, columnChecks, data, loading, pagination, getData, replaceSearchParams, resetSearchParams, handleSizeChange, handleCurrentChange, refreshData, refreshCreate, refreshUpdate, refreshRemove } =
    useTable({
      core: {
        apiFn: fetchWarehouseAreasPage,
        apiParams: buildWarehouseAreasPageQueryParams(searchForm.value),
        paginationKey: getWarehouseAreasPaginationKey(),
        columnsFactory: () =>
          createWarehouseAreasTableColumns({
            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) => normalizeWarehouseAreasListRow(item))
        }
      }
    })
 
  const {
    dialogVisible,
    dialogType,
    currentRecord: currentWarehouseAreasData,
    selectedRows,
    handleSelectionChange,
    showDialog,
    handleDialogSubmit,
    handleDelete,
    handleBatchDelete
  } = useCrudPage({
    createEmptyModel: () => buildWarehouseAreasDialogModel(),
    buildEditModel: (record) => buildWarehouseAreasDialogModel(record),
    buildSavePayload: (formData) => buildWarehouseAreasSavePayload(formData),
    saveRequest: fetchSaveWarehouseAreas,
    updateRequest: fetchUpdateWarehouseAreas,
    deleteRequest: fetchDeleteWarehouseAreas,
    entityName: '库区',
    resolveRecordLabel: (record) => record?.name || record?.code || record?.id,
    refreshCreate,
    refreshUpdate,
    refreshRemove
  })
  handleDeleteAction = handleDelete
 
  function handleSearch(params) {
    replaceSearchParams(buildWarehouseAreasSearchParams(params))
    getData()
  }
 
  function handleReset() {
    Object.assign(searchForm.value, createWarehouseAreasSearchState())
    resetSearchParams()
  }
 
  async function ensureCompanyOptions() {
    if (companyOptionsLoaded.value) {
      return
    }
 
    if (companyOptionsLoading.value) {
      await companyOptionsLoading.value
      return
    }
 
    companyOptionsLoading.value = (async () => {
      const records = await guardRequestWithMessage(fetchCompanysList(), [], {
        timeoutMessage: '往来企业选项加载超时,已停止等待'
      })
      const normalizedRecords = defaultResponseAdapter(records).records
      shipperOptions.value = normalizedRecords
        .filter((item) => item.type === 'shipper')
        .map((item) => ({
          label: item.name || item.code || `货主 ${item.id}`,
          value: item.id
        }))
      supplierOptions.value = normalizedRecords
        .filter((item) => item.type === 'supplier')
        .map((item) => ({
          label: item.name || item.code || `供应商 ${item.id}`,
          value: item.id
        }))
      companyOptionsLoaded.value = true
    })()
 
    try {
      await companyOptionsLoading.value
    } finally {
      companyOptionsLoading.value = null
    }
  }
 
  async function loadWarehouseOptions() {
    const records = await guardRequestWithMessage(fetchWarehouseList(), [], {
      timeoutMessage: '仓库选项加载超时,已停止等待'
    })
    warehouseOptions.value = defaultResponseAdapter(records).records.map((item) => ({
      label: item.name || item.code || `仓库 ${item.id}`,
      value: item.id
    }))
  }
 
  async function loadTypeOptions() {
    const response = await guardRequestWithMessage(
      fetchDictDataPage({
        current: 1,
        pageSize: 200,
        dictTypeCode: 'sys_ware_areas_type',
        status: 1
      }),
      { records: [] },
      { timeoutMessage: '业务类型加载超时,已停止等待' }
    )
    typeOptions.value = defaultResponseAdapter(response).records.map((item) => ({
      label: item.label || item.name || item.value,
      value: item.value
    }))
  }
 
  onMounted(async () => {
    await Promise.all([loadWarehouseOptions(), loadTypeOptions(), getData()])
  })
</script>