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('、') : '' }