zhou zhou
1 天以前 aaf8a50511d77dbc209ca93bbba308c21179a8bc
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
import {
  normalizeLocAreaMatGroupTreeOptions,
  resolveLocAreaMatAreaOptions,
  resolveLocAreaMatLocOptions,
  resolveLocAreaMatLocTypeOptions,
  resolveLocAreaMatMatnrOptions
} from '../loc-area-mat/locAreaMatPage.helpers.js'
 
const STATUS_META = {
  1: { text: '正常', type: 'success', bool: true },
  0: { text: '冻结', type: 'danger', bool: false }
}
 
export const LOC_AREA_MAT_RELA_REPORT_TITLE = '库区物料关系报表'
export const LOC_AREA_MAT_RELA_REPORT_STYLE = {
  titleAlign: 'center',
  titleLevel: 'strong',
  orientation: 'landscape',
  density: 'compact',
  showSequence: true
}
 
function normalizeText(value) {
  return String(value ?? '').trim()
}
 
function normalizeNumber(value, fallback = void 0) {
  if (value === '' || value === null || value === undefined) {
    return fallback
  }
  const numberValue = Number(value)
  return Number.isNaN(numberValue) ? fallback : numberValue
}
 
function normalizeOptionId(item = {}) {
  const value = item.id ?? item.value
  if (value === void 0 || value === null || value === '') {
    return void 0
  }
  return normalizeNumber(value)
}
 
function normalizeOptionText(item = {}, fallbackPrefix = '') {
  return (
    normalizeText(
      item.displayLabel ||
        item.name ||
        item.code ||
        item.label ||
        item.codeText ||
        `${fallbackPrefix}${item.id ?? item.value ?? ''}`
    ) || '--'
  )
}
 
function filterParams(params = {}, ignoredKeys = []) {
  return Object.fromEntries(
    Object.entries(params)
      .filter(([key, value]) => {
        if (ignoredKeys.includes(key)) return false
        if (value === undefined || value === null) return false
        if (typeof value === 'string' && value.trim() === '') return false
        return true
      })
      .map(([key, value]) => [key, normalizeText(value)])
  )
}
 
function createLookupMap(options = [], childrenKey = 'children') {
  const map = {}
  const walk = (items = []) => {
    items.forEach((item) => {
      if (!item || typeof item !== 'object') return
      const value = normalizeOptionId(item)
      if (value !== void 0) {
        map[String(value)] = normalizeOptionText(item)
      }
      const children = item[childrenKey]
      if (Array.isArray(children) && children.length > 0) {
        walk(children)
      }
    })
  }
  walk(Array.isArray(options) ? options : [])
  return map
}
 
function resolveMapLabel(record, field, map, fallbackKeys = []) {
  const rawValue = record?.[field]
  const candidates = [
    record?.[`${field}$`],
    record?.[`${field}Text`],
    ...fallbackKeys.map((key) => record?.[key])
  ]
  const mapped = rawValue !== void 0 && rawValue !== null && rawValue !== '' ? map[String(rawValue)] : void 0
  return normalizeText(candidates.find((item) => normalizeText(item)) || mapped || '')
}
 
function resolveRelationTypeText(record = {}) {
  const hasMatnr = record.matnrId !== void 0 || record.matnrId$ !== void 0 || record.matnrIdText !== void 0
  const hasLoc = record.locId !== void 0 || record.locId$ !== void 0 || record.locIdText !== void 0
  if (hasMatnr && hasLoc) return '混合关系'
  if (hasMatnr) return '物料关系'
  if (hasLoc) return '库位关系'
  return '未分类'
}
 
export function createLocAreaMatRelaSearchState() {
  return {
    condition: '',
    areaMatId: '',
    areaId: '',
    code: '',
    matnrId: '',
    groupId: '',
    locTypeId: '',
    locId: '',
    status: '',
    memo: ''
  }
}
 
export function createLocAreaMatRelaFormState() {
  return {
    id: void 0,
    areaMatId: '',
    areaId: '',
    code: '',
    matnrId: '',
    groupId: '',
    locTypeId: '',
    locId: '',
    status: 1,
    memo: ''
  }
}
 
export function getLocAreaMatRelaStatusOptions() {
  return [
    { label: '正常', value: 1 },
    { label: '冻结', value: 0 }
  ]
}
 
export function getLocAreaMatRelaStatusMeta(status) {
  if (status === true || Number(status) === 1) {
    return STATUS_META[1]
  }
  if (status === false || Number(status) === 0) {
    return STATUS_META[0]
  }
  return { text: '未知', type: 'info', bool: false }
}
 
export function getLocAreaMatRelaPaginationKey() {
  return {
    current: 'current',
    size: 'pageSize'
  }
}
 
export function buildLocAreaMatRelaSearchParams(params = {}) {
  const searchParams = {
    condition: normalizeText(params.condition),
    areaMatId:
      params.areaMatId !== undefined && params.areaMatId !== null && params.areaMatId !== ''
        ? normalizeNumber(params.areaMatId)
        : void 0,
    areaId:
      params.areaId !== undefined && params.areaId !== null && params.areaId !== ''
        ? normalizeNumber(params.areaId)
        : void 0,
    code: normalizeText(params.code),
    matnrId:
      params.matnrId !== undefined && params.matnrId !== null && params.matnrId !== ''
        ? normalizeNumber(params.matnrId)
        : void 0,
    groupId:
      params.groupId !== undefined && params.groupId !== null && params.groupId !== ''
        ? normalizeNumber(params.groupId)
        : void 0,
    locTypeId:
      params.locTypeId !== undefined && params.locTypeId !== null && params.locTypeId !== ''
        ? normalizeNumber(params.locTypeId)
        : void 0,
    locId:
      params.locId !== undefined && params.locId !== null && params.locId !== ''
        ? normalizeNumber(params.locId)
        : void 0,
    status:
      params.status !== undefined && params.status !== null && params.status !== ''
        ? normalizeNumber(params.status)
        : void 0,
    memo: normalizeText(params.memo)
  }
 
  return Object.fromEntries(
    Object.entries(searchParams).filter(([, value]) => value !== '' && value !== void 0 && value !== null)
  )
}
 
export function buildLocAreaMatRelaPageQueryParams(params = {}) {
  return {
    current: params.current || 1,
    pageSize: params.pageSize || params.size || 20,
    ...buildLocAreaMatRelaSearchParams(params)
  }
}
 
export function buildLocAreaMatRelaSavePayload(formData = {}) {
  return {
    ...(formData.id !== void 0 && formData.id !== null && formData.id !== ''
      ? { id: normalizeNumber(formData.id) }
      : {}),
    ...(formData.areaMatId !== void 0 && formData.areaMatId !== null && formData.areaMatId !== ''
      ? { areaMatId: normalizeNumber(formData.areaMatId) }
      : {}),
    ...(formData.areaId !== void 0 && formData.areaId !== null && formData.areaId !== ''
      ? { areaId: normalizeNumber(formData.areaId) }
      : {}),
    code: normalizeText(formData.code) || '',
    ...(formData.matnrId !== void 0 && formData.matnrId !== null && formData.matnrId !== ''
      ? { matnrId: normalizeNumber(formData.matnrId) }
      : {}),
    ...(formData.groupId !== void 0 && formData.groupId !== null && formData.groupId !== ''
      ? { groupId: normalizeNumber(formData.groupId) }
      : {}),
    ...(formData.locTypeId !== void 0 && formData.locTypeId !== null && formData.locTypeId !== ''
      ? { locTypeId: normalizeNumber(formData.locTypeId) }
      : {}),
    ...(formData.locId !== void 0 && formData.locId !== null && formData.locId !== ''
      ? { locId: normalizeNumber(formData.locId) }
      : {}),
    status:
      formData.status !== undefined && formData.status !== null && formData.status !== ''
        ? normalizeNumber(formData.status)
        : 1,
    memo: normalizeText(formData.memo) || ''
  }
}
 
export function buildLocAreaMatRelaDialogModel(record = {}) {
  return {
    ...createLocAreaMatRelaFormState(),
    ...(record.id !== void 0 && record.id !== null && record.id !== '' ? { id: normalizeNumber(record.id) } : {}),
    areaMatId:
      record.areaMatId !== undefined && record.areaMatId !== null && record.areaMatId !== ''
        ? normalizeNumber(record.areaMatId)
        : '',
    areaId:
      record.areaId !== undefined && record.areaId !== null && record.areaId !== ''
        ? normalizeNumber(record.areaId)
        : '',
    code: normalizeText(record.code || ''),
    matnrId:
      record.matnrId !== undefined && record.matnrId !== null && record.matnrId !== ''
        ? normalizeNumber(record.matnrId)
        : '',
    groupId:
      record.groupId !== undefined && record.groupId !== null && record.groupId !== ''
        ? normalizeNumber(record.groupId)
        : '',
    locTypeId:
      record.locTypeId !== undefined && record.locTypeId !== null && record.locTypeId !== ''
        ? normalizeNumber(record.locTypeId)
        : '',
    locId:
      record.locId !== undefined && record.locId !== null && record.locId !== ''
        ? normalizeNumber(record.locId)
        : '',
    status: record.status !== undefined && record.status !== null ? normalizeNumber(record.status, 1) : 1,
    memo: normalizeText(record.memo || '')
  }
}
 
export function normalizeLocAreaMatRelaListRow(record = {}, lookups = {}) {
  const areaMatMap = lookups.areaMatMap || createLookupMap(lookups.areaMatOptions || [])
  const areaMap = lookups.areaMap || createLookupMap(lookups.areaOptions || [])
  const matnrMap = lookups.matnrMap || createLookupMap(lookups.matnrOptions || [])
  const groupMap = lookups.groupMap || createLookupMap(lookups.groupOptions || [])
  const locTypeMap = lookups.locTypeMap || createLookupMap(lookups.locTypeOptions || [])
  const locMap = lookups.locMap || createLookupMap(lookups.locOptions || [])
 
  const statusMeta = getLocAreaMatRelaStatusMeta(record.statusBool ?? record.status)
 
  return {
    ...record,
    areaMatIdText: resolveMapLabel(record, 'areaMatId', areaMatMap, ['areaMatCode', 'areaMatName']),
    areaIdText: resolveMapLabel(record, 'areaId', areaMap, ['areaName']),
    code: normalizeText(record.code || ''),
    matnrIdText: resolveMapLabel(record, 'matnrId', matnrMap, ['matnrCode', 'matnrName']),
    groupIdText: resolveMapLabel(record, 'groupId', groupMap, ['groupName']),
    locTypeIdText: resolveMapLabel(record, 'locTypeId', locTypeMap, ['locTypeName', 'typeName']),
    locIdText: resolveMapLabel(record, 'locId', locMap, ['locCode', 'locName']),
    relationTypeText: resolveRelationTypeText(record),
    statusText: statusMeta.text,
    statusType: statusMeta.type,
    statusBool: record.statusBool !== void 0 ? Boolean(record.statusBool) : statusMeta.bool,
    memo: normalizeText(record.memo || ''),
    createByText: normalizeText(record.createBy$ || record.createByText || ''),
    updateByText: normalizeText(record.updateBy$ || record.updateByText || ''),
    createTimeText: normalizeText(record.createTime$ || record.createTime || ''),
    updateTimeText: normalizeText(record.updateTime$ || record.updateTime || '')
  }
}
 
export function normalizeLocAreaMatRelaDetailRecord(record = {}, lookups = {}) {
  return normalizeLocAreaMatRelaListRow(record, lookups)
}
 
export function buildLocAreaMatRelaPrintRows(records = [], lookups = {}) {
  if (!Array.isArray(records)) {
    return []
  }
  return records.map((record) => normalizeLocAreaMatRelaListRow(record, lookups))
}
 
export function buildLocAreaMatRelaReportMeta({
  previewMeta = {},
  count = 0,
  orientation = LOC_AREA_MAT_RELA_REPORT_STYLE.orientation
} = {}) {
  return {
    reportTitle: LOC_AREA_MAT_RELA_REPORT_TITLE,
    reportDate: previewMeta.reportDate,
    printedAt: previewMeta.printedAt,
    operator: previewMeta.operator,
    count,
    reportStyle: {
      ...LOC_AREA_MAT_RELA_REPORT_STYLE,
      orientation
    }
  }
}
 
export function createLocAreaMatRelaLookupMaps({
  areaMatOptions = [],
  areaOptions = [],
  matnrOptions = [],
  groupOptions = [],
  locTypeOptions = [],
  locOptions = []
} = {}) {
  return {
    areaMatMap: createLookupMap(areaMatOptions),
    areaMap: createLookupMap(areaOptions),
    matnrMap: createLookupMap(matnrOptions),
    groupMap: createLookupMap(groupOptions),
    locTypeMap: createLookupMap(locTypeOptions),
    locMap: createLookupMap(locOptions)
  }
}
 
export function resolveLocAreaMatRelaAreaMatOptions(records = []) {
  if (!Array.isArray(records)) {
    return []
  }
  return records
    .map((item) => {
      const value = normalizeOptionId(item)
      if (value === void 0) return null
      const label =
        [
          normalizeText(item.code || item.areaMatCode || ''),
          normalizeText(item.depict || item.areaMatName || ''),
          normalizeText(item.warehouseName || item.warehouseId$ || ''),
          normalizeText(item.areaName || item.areaId$ || '')
        ]
          .filter(Boolean)
          .join(' · ') || `主单 ${value}`
      return {
        value,
        label,
        areaId:
          item.areaId !== undefined && item.areaId !== null && item.areaId !== ''
            ? normalizeNumber(item.areaId)
            : void 0
      }
    })
    .filter(Boolean)
}
 
export {
  normalizeLocAreaMatGroupTreeOptions as resolveLocAreaMatRelaGroupOptions,
  resolveLocAreaMatAreaOptions as resolveLocAreaMatRelaAreaOptions,
  resolveLocAreaMatLocOptions as resolveLocAreaMatRelaLocOptions,
  resolveLocAreaMatLocTypeOptions as resolveLocAreaMatRelaLocTypeOptions,
  resolveLocAreaMatMatnrOptions as resolveLocAreaMatRelaMatnrOptions
}