import dayjs from 'dayjs' import type { StockReportListItem } from '@/api/stock.report/stock.report' export const reportFieldLabels: Record = { store_id: '货主ID', store_name: '货主', stock_id: 'SKU', item_code: '商品编码', barcode: '条码', item_name: '商品名称', begin_total: '期初数量', inbound_total: '区间内入库', outbound_total: '区间内出库', end_total: '期末数量', start_time: '统计区间开始', end_time: '统计区间结束', warehouse_id: '仓库ID', warehouse_name: '仓库名称', space_id: '库位ID', space_name: '库位' } const DETAIL_KEY_ORDER = [ 'store_id', 'store_name', 'warehouse_id', 'warehouse_name', 'space_id', 'space_name', 'stock_id', 'item_code', 'barcode', 'item_name', 'begin_total', 'inbound_total', 'outbound_total', 'end_total', 'start_time', 'end_time' ] export function sortReportDetailKeys(keys: string[]) { const head = DETAIL_KEY_ORDER.filter((k) => keys.includes(k)) const tail = keys.filter((k) => !DETAIL_KEY_ORDER.includes(k)).sort() return [...head, ...tail] } /** * 报表明细分区(对齐 stock/external_stock「基本信息」新表单设计) * 未列出的字段统一归入「其他信息」,保持 sortReportDetailKeys 的兜底顺序 */ export const REPORT_DETAIL_SECTIONS: { title: string; keys: string[] }[] = [ { title: '货主与仓库', keys: ['store_name', 'store_id', 'warehouse_name', 'warehouse_id'] }, { title: '库位信息', keys: ['space_name', 'space_id'] }, { title: '商品信息', keys: ['stock_id', 'item_code', 'barcode', 'item_name'] }, { title: '库存数量', keys: ['begin_total', 'inbound_total', 'outbound_total', 'end_total'] }, { title: '统计区间', keys: ['start_time', 'end_time'] } ] /** 明细字段 -> 分区列表(含「其他信息」兜底;空分区不返回) */ export function groupReportDetailKeys(keys: string[]): { title: string; keys: string[] }[] { const ordered = sortReportDetailKeys(keys) const used = new Set() const sections: { title: string; keys: string[] }[] = [] for (const section of REPORT_DETAIL_SECTIONS) { const hit = section.keys.filter((k) => ordered.includes(k)) if (!hit.length) continue sections.push({ title: section.title, keys: hit }) hit.forEach((k) => used.add(k)) } const rest = ordered.filter((k) => !used.has(k)) if (rest.length) sections.push({ title: '其他信息', keys: rest }) return sections } export function formatReportNumber(val: unknown): string { if (val === null || val === undefined || val === '') return '--' const n = Number(val) if (Number.isNaN(n)) return '--' return String(Math.trunc(n) === n ? n.toLocaleString('zh-CN') : n.toLocaleString('zh-CN', { maximumFractionDigits: 4 })) } export function formatReportDateTime(val: unknown): string { if (val === null || val === undefined || val === '') return '--' const d = dayjs(val as string | number | Date) return d.isValid() ? d.format('YYYY-MM-DD HH:mm:ss') : String(val) } export function formatReportCell(val: unknown): string { if (val === null || val === undefined || val === '') return '--' if (typeof val === 'object') return JSON.stringify(val) return String(val) } export type ReportColumn = { prop: string label: string width?: string | number minWidth?: string | number align?: 'left' | 'center' | 'right' sortable?: boolean | 'custom' sortOrders?: string[] copyAction?: 'copy' | 'copyQr' formatter?: (row: StockReportListItem) => string } /** 主表格固定列(与真实接口字段一致);按库位时后端可额外返回 space_*,由 buildReportTableColumns 追加 */ export const fixedReportColumns: ReportColumn[] = [ { prop: 'store_name', label: '货主', minWidth: 120, formatter: (row) => row.store_name || '--' }, { prop: 'item_name', label: '商品名称', minWidth: 140, formatter: (row) => row.item_name || '--' }, { prop: 'item_code', label: '商品编码', minWidth: 120, copyAction: 'copy' as const, formatter: (row) => row.item_code || '--' }, { prop: 'barcode', label: '条码', minWidth: 140, copyAction: 'copy' as const, formatter: (row) => row.barcode || '--' }, { prop: 'stock_id', label: 'SKU', width: 96, copyAction: 'copy' as const, formatter: (row) => formatReportCell(row.stock_id) }, { prop: 'begin_total', label: '期初数量', width: 104, sortable: 'custom', formatter: (row) => formatReportNumber(row.begin_total) }, { prop: 'inbound_total', label: '区间内入库', width: 112, sortable: 'custom', formatter: (row) => formatReportNumber(row.inbound_total) }, { prop: 'outbound_total', label: '区间内出库', width: 112, sortable: 'custom', formatter: (row) => formatReportNumber(row.outbound_total) }, { prop: 'end_total', label: '期末数量', width: 104, sortable: 'custom', formatter: (row) => formatReportNumber(row.end_total) }, { prop: 'start_time', label: '区间开始', width: 168, formatter: (row) => formatReportDateTime(row.start_time) }, { prop: 'end_time', label: '区间结束', width: 168, formatter: (row) => formatReportDateTime(row.end_time) }, { prop: 'warehouse_name', label: '仓库', minWidth: 120, formatter: (row) => (row as any).warehouse_name || '--' } ] const FIXED_PROPS = new Set(fixedReportColumns.map((c) => c.prop)) export function buildReportTableColumns(rows: StockReportListItem[]) { const cols = fixedReportColumns.map((c) => ({ ...c })) const extra = new Set() for (const row of rows) { if (!row || typeof row !== 'object') continue for (const k of Object.keys(row)) { if (k === '__rid' || k === 'store_id' || k === 'warehouse_id' || FIXED_PROPS.has(k)) continue extra.add(k) } } for (const prop of [...extra].sort()) { cols.push({ prop, label: reportFieldLabels[prop] || prop, minWidth: 110, formatter: (row) => formatReportCell((row as any)[prop]) }) } return cols } function normalizeReportDateParam(value: unknown): string | undefined { if (value == null || value === '') return undefined const d = dayjs(value as string | number | Date) return d.isValid() ? d.format('YYYY-MM-DD HH:mm:ss') : String(value) } function normalizeReportStartDateParam(value: unknown): string | undefined { if (value == null || value === '') return undefined const d = dayjs(value as string | number | Date) return d.isValid() ? d.startOf('day').format('YYYY-MM-DD HH:mm:ss') : String(value) } function normalizeReportEndDateParam(value: unknown): string | undefined { if (value == null || value === '') return undefined const d = dayjs(value as string | number | Date) return d.isValid() ? d.endOf('day').format('YYYY-MM-DD HH:mm:ss') : String(value) } /** ScanBox 筛选字段分组(库存报表模块专用) */ export const REPORT_SCAN_FIELD_GROUPS = { basic: '基础信息', time: '时间筛选', } as const type ReportScanFieldGroupKey = keyof typeof REPORT_SCAN_FIELD_GROUPS export function reportScanField( group: G, field: Record ) { return { ...field, group: REPORT_SCAN_FIELD_GROUPS[group] } } /** ScanBox 默认 props(与波次模块 scan-box 侧栏一致) */ export const reportScanBoxConfig = { width: '220px', treeProps: { children: 'children', label: 'label' } as const, autoSubmit: false } /** ScanBox 侧栏可用字段(与 storage.report.search 的 where 对应) */ export const reportScanBoxAvailableFields = [ reportScanField('basic', { value: 'keyword', label: '关键字', type: 'text', placeholder: '搜索商品名称/编码/条码' }), reportScanField('basic', { value: 'stock_id', label: 'SKU', type: 'text', placeholder: '多个 SKU ID 用逗号/空格分隔(传 where.stock_ids 数组)' }), reportScanField('basic', { value: 'store_id', label: '货主', type: 'select', multiple: true, filterable: true, placeholder: '请选择货主', options: [] as { label: string; value: number }[] }), reportScanField('basic', { value: 'by_space', label: '按库位统计', type: 'boolean', placeholder: '开启后按商品+库位+货主返回;关闭则按商品+货主聚合' }), reportScanField('time', { value: 'report_period', label: '统计时间', type: 'daterange', startPlaceholder: '开始日期', endPlaceholder: '结束日期' }), ] /** 将 ScanBox 条件转为接口扁平 where;无有效条件时返回 {} */ export function normalizeScanConditionToReportWhere(condition: any): Record { const where: Record = {} const conds = condition?.conditions && typeof condition.conditions === 'object' ? condition.conditions : condition && typeof condition === 'object' ? condition : null if (!conds) { return where } const period = conds.report_period?.value ?? conds.report_period const startTime = conds.start_time?.value ?? conds.start_time const endTime = conds.end_time?.value ?? conds.end_time const dateRangeValue = Array.isArray(period) ? period : Array.isArray(startTime) && startTime.length >= 2 ? startTime : Array.isArray(endTime) && endTime.length >= 2 ? endTime : null if (dateRangeValue && dateRangeValue.length >= 2) { const a = normalizeReportStartDateParam(dateRangeValue[0]) const b = normalizeReportEndDateParam(dateRangeValue[1]) if (a) where.start_time = a if (b) where.end_time = b } else { const a = normalizeReportStartDateParam(startTime) const b = normalizeReportEndDateParam(endTime) if (a) where.start_time = a if (b) where.end_time = b } const sid = conds.store_id?.value ?? conds.store_id if (sid != null && sid !== '') { const raw = sid const arr = (Array.isArray(raw) ? raw : [raw]) .map((x: unknown) => Number(x)) .filter((n) => Number.isFinite(n)) if (arr.length) where.store_ids = arr } if (Object.prototype.hasOwnProperty.call(conds, 'by_space')) { const bySpaceRaw = conds.by_space?.value ?? conds.by_space if (bySpaceRaw === true || bySpaceRaw === 'true' || bySpaceRaw === 1 || bySpaceRaw === '1') { where.by_space = true } else if ( bySpaceRaw === false || bySpaceRaw === 'false' || bySpaceRaw === 0 || bySpaceRaw === '0' ) { where.by_space = false } } // 关键字搜索 const keyword = conds.keyword?.value ?? conds.keyword if (keyword != null && String(keyword).trim() !== '') { where.keyword = String(keyword).trim() } // SKU 筛选(最新接口 where.stock_ids,恒传数组): // 支持单个或多个 SKU(逗号/空格/中文逗号分隔),空值忽略 const skuRaw = conds.stock_id?.value ?? conds.stock_id if (skuRaw != null && skuRaw !== '' && skuRaw !== undefined) { const skuParts = (Array.isArray(skuRaw) ? skuRaw : String(skuRaw).split(/[\s,,]+/)) .map((x: unknown) => Number(x)) .filter((n: number) => Number.isFinite(n) && n > 0) if (skuParts.length) { where.stock_ids = skuParts } } // 仓库筛选 const warehouseId = conds.warehouse_id?.value ?? conds.warehouse_id if (warehouseId != null && warehouseId !== '' && warehouseId !== undefined) { where.warehouse_id = warehouseId } return where }