zhou zhou
6 小时以前 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
<template>
  <div class="wh-mat-page-root">
    <div class="wh-mat-page art-full-height">
      <div class="wh-mat-page__sidebar">
        <ElCard class="wh-mat-page__sidebar-card">
          <div class="mb-3 flex items-center justify-between gap-3">
            <div>
              <div class="text-base font-medium text-[var(--art-text-primary)]">物料分组</div>
              <div class="text-xs text-[var(--art-text-secondary)]">
                {{ selectedGroupLabel }}
              </div>
            </div>
            <ElButton text @click="handleResetGroup">全部</ElButton>
          </div>
 
          <div class="mb-3 flex items-center gap-2">
            <ElInput
              v-model.trim="groupSearch"
              clearable
              placeholder="搜索物料分组"
              @clear="handleGroupSearch"
              @keyup.enter="handleGroupSearch"
            />
            <ElButton @click="handleGroupSearch">搜索</ElButton>
          </div>
 
          <ElScrollbar class="wh-mat-page__tree-scroll pr-1">
            <div v-if="groupTreeLoading" class="py-6">
              <ElSkeleton :rows="10" animated />
            </div>
            <ElEmpty v-else-if="!groupTreeData.length" description="暂无物料分组" />
            <ElTree
              v-else
              :data="groupTreeData"
              :props="treeProps"
              node-key="id"
              highlight-current
              default-expand-all
              :current-node-key="selectedGroupId"
              @node-click="handleGroupNodeClick"
            >
              <template #default="{ data }">
                <div class="flex items-center gap-2">
                  <span class="font-medium">{{ data.name || '--' }}</span>
                  <span class="text-xs text-[var(--art-text-secondary)]">{{ data.code || '--' }}</span>
                </div>
              </template>
            </ElTree>
          </ElScrollbar>
        </ElCard>
      </div>
 
      <div class="wh-mat-page__content">
        <ArtSearchBar
          v-model="searchForm"
          :items="searchItems"
          :showExpand="true"
          @search="handleSearch"
          @reset="handleReset"
        />
 
        <ElCard class="art-table-card">
          <ArtTableHeader :loading="loading" v-model:columns="columnChecks" @refresh="loadMatnrList" />
 
          <ArtTable
            :loading="loading"
            :data="tableData"
            :columns="columns"
            :pagination="pagination"
            @pagination:size-change="handleSizeChange"
            @pagination:current-change="handleCurrentChange"
          >
            <template #action="{ row }">
              <ArtButtonTable icon="ri:eye-line" @click="openDetailDrawer(row)" />
            </template>
          </ArtTable>
        </ElCard>
      </div>
    </div>
 
    <WhMatDetailDrawer
      v-model:visible="detailDrawerVisible"
      :loading="detailLoading"
      :detail="detailData"
    />
  </div>
</template>
 
<script setup>
  import { ElMessage } from 'element-plus'
  import { computed, onMounted, reactive, ref } from 'vue'
  import ArtButtonTable from '@/components/core/forms/art-button-table/index.vue'
  import { useTableColumns } from '@/hooks/core/useTableColumns'
  import { guardRequestWithMessage } from '@/utils/sys/requestGuard'
  import { fetchMatnrDetail, fetchMatnrGroupTree, fetchMatnrPage } from '@/api/wh-mat'
  import WhMatDetailDrawer from './modules/wh-mat-detail-drawer.vue'
  import { createWhMatTableColumns } from './whMatTable.columns'
  import {
    buildMatnrGroupTreeQueryParams,
    buildMatnrPageQueryParams,
    createWhMatSearchState,
    getWhMatTreeNodeLabel,
    normalizeMatnrDetail,
    normalizeMatnrGroupTreeRows,
    normalizeMatnrRow
  } from './whMatPage.helpers'
 
  defineOptions({ name: 'WhMat' })
 
  const loading = ref(false)
  const groupTreeLoading = ref(false)
  const detailDrawerVisible = ref(false)
  const detailLoading = ref(false)
  const tableData = ref([])
  const groupTreeData = ref([])
  const detailData = ref({})
  const selectedGroupId = ref(null)
  const groupSearch = ref('')
  const searchForm = ref(createWhMatSearchState())
 
  const pagination = reactive({
    current: 1,
    size: 20,
    total: 0
  })
 
  const treeProps = {
    label: 'name',
    children: 'children'
  }
 
  const searchItems = computed(() => [
    {
      label: '关键字',
      key: 'condition',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入物料编码/物料名称'
      }
    },
    {
      label: '物料编码',
      key: 'code',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入物料编码'
      }
    },
    {
      label: '物料名称',
      key: 'name',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入物料名称'
      }
    },
    {
      label: '规格',
      key: 'spec',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入规格'
      }
    },
    {
      label: '条码',
      key: 'barcode',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入条码'
      }
    }
  ])
 
  const { columnChecks, columns } = useTableColumns(() =>
    createWhMatTableColumns({
      handleViewDetail: openDetailDrawer
    })
  )
 
  const selectedGroupLabel = computed(() => {
    if (!selectedGroupId.value) {
      return '全部物料'
    }
    const found = findGroupNode(groupTreeData.value, selectedGroupId.value)
    return found ? getWhMatTreeNodeLabel(found) : '全部物料'
  })
 
  function findGroupNode(nodes, targetId) {
    const normalizedTarget = String(targetId || '')
    for (const node of nodes || []) {
      if (String(node.id) === normalizedTarget) {
        return node
      }
      if (node.children?.length) {
        const child = findGroupNode(node.children, normalizedTarget)
        if (child) {
          return child
        }
      }
    }
    return null
  }
 
  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 loadGroupTree() {
    groupTreeLoading.value = true
    try {
      const records = await guardRequestWithMessage(
        fetchMatnrGroupTree(buildMatnrGroupTreeQueryParams({ condition: groupSearch.value })),
        [],
        { timeoutMessage: '物料分组加载超时,已停止等待' }
      )
      const normalizedTree = normalizeMatnrGroupTreeRows(Array.isArray(records) ? records : [])
      groupTreeData.value = normalizedTree
      if (selectedGroupId.value && !findGroupNode(normalizedTree, selectedGroupId.value)) {
        selectedGroupId.value = null
      }
    } catch (error) {
      groupTreeData.value = []
      ElMessage.error(error?.message || '物料分组加载失败')
    } finally {
      groupTreeLoading.value = false
    }
  }
 
  async function loadMatnrList() {
    loading.value = true
    try {
      const response = await guardRequestWithMessage(
        fetchMatnrPage(
          buildMatnrPageQueryParams({
            ...searchForm.value,
            groupId: selectedGroupId.value,
            current: pagination.current,
            pageSize: pagination.size
          })
        ),
        {
          records: [],
          total: 0,
          current: pagination.current,
          size: pagination.size
        },
        { timeoutMessage: '物料列表加载超时,已停止等待' }
      )
      tableData.value = Array.isArray(response?.records)
        ? response.records.map((record) => normalizeMatnrRow(record))
        : []
      updatePaginationState(pagination, response, pagination.current, pagination.size)
    } catch (error) {
      tableData.value = []
      ElMessage.error(error?.message || '物料列表加载失败')
    } finally {
      loading.value = false
    }
  }
 
  async function openDetailDrawer(row) {
    detailDrawerVisible.value = true
    detailLoading.value = true
    try {
      detailData.value = normalizeMatnrDetail(
        await guardRequestWithMessage(fetchMatnrDetail(row.id), {}, {
          timeoutMessage: '物料详情加载超时,已停止等待'
        })
      )
    } catch (error) {
      detailDrawerVisible.value = false
      detailData.value = {}
      ElMessage.error(error?.message || '获取物料详情失败')
    } finally {
      detailLoading.value = false
    }
  }
 
  function handleSearch(params) {
    searchForm.value = {
      ...searchForm.value,
      ...params
    }
    pagination.current = 1
    loadMatnrList()
  }
 
  async function handleReset() {
    searchForm.value = createWhMatSearchState()
    pagination.current = 1
    selectedGroupId.value = null
    groupSearch.value = ''
    await Promise.all([loadGroupTree(), loadMatnrList()])
  }
 
  function handleSizeChange(size) {
    pagination.size = size
    pagination.current = 1
    loadMatnrList()
  }
 
  function handleCurrentChange(current) {
    pagination.current = current
    loadMatnrList()
  }
 
  function handleGroupNodeClick(data) {
    selectedGroupId.value = data?.id ?? null
    pagination.current = 1
    loadMatnrList()
  }
 
  async function handleResetGroup() {
    selectedGroupId.value = null
    pagination.current = 1
    groupSearch.value = ''
    await Promise.all([loadGroupTree(), loadMatnrList()])
  }
 
  async function handleGroupSearch() {
    selectedGroupId.value = null
    pagination.current = 1
    await Promise.all([loadGroupTree(), loadMatnrList()])
  }
 
  onMounted(async () => {
    await Promise.all([loadGroupTree(), loadMatnrList()])
  })
</script>
 
<style scoped>
  .wh-mat-page {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: 16px;
  }
 
  .wh-mat-page__sidebar {
    width: 320px;
    flex: 0 0 320px;
  }
 
  .wh-mat-page__sidebar-card {
    position: sticky;
    top: 16px;
  }
 
  .wh-mat-page__tree-scroll {
    height: calc(100vh - 320px);
    min-height: 420px;
  }
 
  .wh-mat-page__content {
    min-width: 0;
    flex: 1 1 auto;
  }
 
  .wh-mat-page__content > * + * {
    margin-top: 16px;
  }
 
  @media (max-width: 1024px) {
    .wh-mat-page {
      flex-direction: column;
    }
 
    .wh-mat-page__sidebar {
      width: 100%;
      flex-basis: auto;
    }
 
    .wh-mat-page__sidebar-card {
      position: static;
    }
 
    .wh-mat-page__tree-scroll {
      height: 320px;
      min-height: 320px;
    }
  }
</style>