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 }