zhou zhou
昨天 c579731e0c206d6062f8442ec9df70ca781b26f6
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
const STATUS_META = {
  1: { text: '正常', type: 'success', bool: true },
  0: { text: '冻结', type: 'danger', bool: false }
}
 
export const BAS_CONTAINER_REPORT_TITLE = '容器规则报表'
export const BAS_CONTAINER_REPORT_STYLE = {
  titleAlign: 'center',
  titleLevel: 'strong',
  orientation: 'portrait',
  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
}
 
export function createBasContainerSearchState() {
  return {
    condition: '',
    code: '',
    containerType: '',
    codeType: '',
    areas: '',
    status: '',
    memo: '',
    timeStart: '',
    timeEnd: ''
  }
}
 
export function createBasContainerFormState() {
  return {
    id: void 0,
    code: '',
    containerType: void 0,
    codeType: '',
    areas: [],
    status: 1,
    memo: ''
  }
}
 
export function getBasContainerStatusOptions() {
  return [
    { label: '正常', value: 1 },
    { label: '冻结', value: 0 }
  ]
}
 
export function getBasContainerPaginationKey() {
  return {
    current: 'current',
    size: 'pageSize'
  }
}
 
export function getBasContainerStatusMeta(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 buildBasContainerSearchParams(params = {}) {
  const result = {}
 
  ;['condition', 'code', 'codeType', 'areas', 'memo', 'timeStart', 'timeEnd'].forEach((key) => {
    const value = normalizeText(params[key])
    if (value) {
      result[key] = value
    }
  })
 
  if (params.containerType !== '' && params.containerType !== null && params.containerType !== undefined) {
    result.containerType = Number(params.containerType)
  }
 
  if (params.status !== '' && params.status !== null && params.status !== undefined) {
    result.status = Number(params.status)
  }
 
  return result
}
 
export function buildBasContainerPageQueryParams(params = {}) {
  return {
    current: params.current || 1,
    pageSize: params.pageSize || params.size || 20,
    ...buildBasContainerSearchParams(params)
  }
}
 
export function normalizeBasContainerAreas(areas = []) {
  if (!Array.isArray(areas)) {
    return []
  }
 
  return areas
    .map((item, index) => normalizeBasContainerAreaRecord(item, index))
    .filter(Boolean)
    .sort((left, right) => {
      const leftSort = normalizeNumber(left.sort, Number.MAX_SAFE_INTEGER)
      const rightSort = normalizeNumber(right.sort, Number.MAX_SAFE_INTEGER)
      return leftSort - rightSort
    })
    .map((item, index) => ({
      id: item.id,
      sort: index + 1
    }))
}
 
export function buildBasContainerSavePayload(formData = {}) {
  const areas = normalizeBasContainerAreas(formData.areas)
  const payload = {
    ...(formData.id !== void 0 && formData.id !== null && formData.id !== ''
      ? { id: Number(formData.id) }
      : {}),
    code: normalizeText(formData.code),
    ...(formData.containerType !== void 0 && formData.containerType !== null && formData.containerType !== ''
      ? { containerType: Number(formData.containerType) }
      : {}),
    codeType: normalizeText(formData.codeType),
    areas,
    status: formData.status !== void 0 && formData.status !== null && formData.status !== ''
      ? Number(formData.status)
      : 1,
    memo: normalizeText(formData.memo)
  }
 
  if (!payload.areas.length) {
    delete payload.areas
  }
 
  return payload
}
 
export function buildBasContainerDialogModel(record = {}) {
  return {
    ...createBasContainerFormState(),
    ...(record.id !== void 0 && record.id !== null && record.id !== '' ? { id: Number(record.id) } : {}),
    code: normalizeText(record.code || ''),
    containerType:
      record.containerType !== void 0 && record.containerType !== null && record.containerType !== ''
        ? Number(record.containerType)
        : record.containerType === 0
          ? 0
          : void 0,
    codeType: normalizeText(record.codeType || ''),
    areas: normalizeBasContainerAreas(record.areas),
    status: record.status !== void 0 && record.status !== null ? Number(record.status) : 1,
    memo: normalizeText(record.memo || '')
  }
}
 
export function normalizeBasContainerDetailRecord(record = {}, resolveAreaLabel) {
  const areas = normalizeBasContainerAreas(record.areas)
  const statusMeta = getBasContainerStatusMeta(record.statusBool ?? record.status)
  return {
    ...record,
    areas,
    containerTypeText: normalizeText(record.containerType$ || record.containerTypeText || ''),
    areasText: buildBasContainerAreasText(areas, record.areas, resolveAreaLabel),
    statusText: statusMeta.text,
    statusType: statusMeta.type,
    statusBool: record.statusBool !== void 0 ? Boolean(record.statusBool) : statusMeta.bool,
    createTimeText: normalizeText(record.createTime$ || record.createTime || ''),
    updateTimeText: normalizeText(record.updateTime$ || record.updateTime || ''),
    areasIds: areas.map((item) => item.id)
  }
}
 
export function normalizeBasContainerListRow(record = {}, resolveAreaLabel) {
  return normalizeBasContainerDetailRecord(record, resolveAreaLabel)
}
 
export function buildBasContainerPrintRows(records = [], resolveAreaLabel) {
  if (!Array.isArray(records)) {
    return []
  }
  return records.map((record) => normalizeBasContainerListRow(record, resolveAreaLabel))
}
 
export function buildBasContainerReportMeta({
  previewMeta = {},
  count = 0,
  orientation = BAS_CONTAINER_REPORT_STYLE.orientation
} = {}) {
  return {
    reportTitle: BAS_CONTAINER_REPORT_TITLE,
    reportDate: previewMeta.reportDate,
    printedAt: previewMeta.printedAt,
    operator: previewMeta.operator,
    count,
    reportStyle: {
      ...BAS_CONTAINER_REPORT_STYLE,
      orientation
    }
  }
}
 
export function buildBasContainerContainerTypeOptions(records = []) {
  if (!Array.isArray(records)) {
    return []
  }
 
  return records
    .map((item) => {
      if (!item || typeof item !== 'object') {
        return null
      }
      const value = item.value ?? item.id ?? item.dictValue
      if (value === void 0 || value === null || value === '') {
        return null
      }
      return {
        value: Number(value),
        label: normalizeText(item.label || item.name || item.dictLabel || item.value || '')
      }
    })
    .filter(Boolean)
}
 
export function resolveBasContainerAreaOptions(records = []) {
  if (!Array.isArray(records)) {
    return []
  }
 
  return records
    .map((item) => {
      if (!item || typeof item !== 'object') {
        return null
      }
 
      const value = item.id ?? item.areaId ?? item.value
      if (value === void 0 || value === null || value === '') {
        return null
      }
 
      return {
        value: Number(value),
        label: normalizeText(item.name || item.areaName || item.code || item.areaCode || `库区 ${value}`)
      }
    })
    .filter(Boolean)
}
 
function normalizeBasContainerAreaRecord(item, index) {
  if (item === null || item === undefined) {
    return null
  }
 
  if (typeof item === 'object') {
    const id = normalizeNumber(item.id ?? item.areaId ?? item.value, void 0)
    if (id === void 0) {
      return null
    }
    return {
      id,
      sort: normalizeNumber(item.sort, index + 1),
      name: normalizeText(item.name || item.areaName || item.label || ''),
      code: normalizeText(item.code || item.areaCode || ''),
      memo: normalizeText(item.memo || '')
    }
  }
 
  const id = normalizeNumber(item, void 0)
  if (id === void 0) {
    return null
  }
  return {
    id,
    sort: index + 1,
    name: '',
    code: '',
    memo: ''
  }
}
 
function buildBasContainerAreasText(areas = [], fallbackAreas = [], resolveAreaLabel) {
  const fallbackLabelMap = new Map(
    (Array.isArray(fallbackAreas) ? fallbackAreas : [])
      .map((item) => {
        if (!item || typeof item !== 'object') {
          return null
        }
        const id = normalizeNumber(item.id ?? item.areaId ?? item.value, void 0)
        if (id === void 0) {
          return null
        }
        return [id, normalizeText(item.name || item.areaName || item.code || item.areaCode || '')]
      })
      .filter(Boolean)
  )
 
  const orderedLabels = (Array.isArray(areas) ? areas : [])
    .map((item) => {
      if (!item || typeof item !== 'object') {
        return ''
      }
      if (typeof resolveAreaLabel === 'function') {
        const resolvedLabel = normalizeText(resolveAreaLabel(item.id))
        if (resolvedLabel) {
          return resolvedLabel
        }
      }
      const fallbackLabel = fallbackLabelMap.get(item.id)
      if (fallbackLabel) {
        return fallbackLabel
      }
      return normalizeText(item.name || item.areaName || item.code || item.areaCode || item.id)
    })
    .filter(Boolean)
 
  if (orderedLabels.length) {
    return orderedLabels.join('、')
  }
 
  const fallbackLabels = (Array.isArray(fallbackAreas) ? fallbackAreas : [])
    .map((item) => {
      if (!item || typeof item !== 'object') {
        return ''
      }
      return normalizeText(item.name || item.areaName || item.code || item.areaCode || item.id)
    })
    .filter(Boolean)
 
  const labels = fallbackLabels
  return labels.length ? labels.join('、') : ''
}