import type { ClientSortDir } from '../hooks/useClientTableSort'; import type { TableColumnDef } from '../hooks/useTableColumnOrder'; import type { DemandForecastRowDto } from './workflowApi'; export function formatForecastQty(value: number): string { const n = Number(value); if (!Number.isFinite(n)) return '0'; if (Number.isInteger(n)) return String(n); return n.toFixed(2).replace(/\.?0+$/, ''); } const CONFIDENCE_RANK: Record = { low: 0, medium: 1, high: 2, }; function minConfidence( a: DemandForecastRowDto['confidence'], b: DemandForecastRowDto['confidence'] ): DemandForecastRowDto['confidence'] { return CONFIDENCE_RANK[a] <= CONFIDENCE_RANK[b] ? a : b; } /** 跨客户合并后的商品行 */ export type ForecastMergedSkuRow = { sku_code: string; sku_name: string; specification: string; unit: string; avg_daily_qty: number; suggested_qty: number; available_qty: number; stockout_risk: boolean; confidence: DemandForecastRowDto['confidence']; customer_count: number; order_lines: number; }; export type ForecastColumnId = | 'seq' | 'product' | 'product_desc' | 'unit' | 'avg_daily' | 'suggested' | 'available' | 'customers' | 'confidence' | 'action'; export type ForecastSortKey = 'product' | 'suggested'; export const FORECAST_COLUMNS: Array> = [ { id: 'seq', label: '序号', fixedStart: true }, { id: 'product', label: '商品名称' }, { id: 'product_desc', label: '商品描述' }, { id: 'unit', label: '单位' }, { id: 'avg_daily', label: '日均需求' }, { id: 'suggested', label: '预测需求' }, { id: 'available', label: '可用库存' }, { id: 'customers', label: '涉及客户' }, { id: 'confidence', label: '置信度' }, { id: 'action', label: '建议操作' }, ]; export const FORECAST_COLUMN_ORDER_KEY = 'table_column_order:dashboard-demand-forecast:v2'; export const FORECAST_COLUMN_WIDTH_KEY = 'table_column_widths:dashboard-demand-forecast:v2'; export const FORECAST_DEFAULT_WIDTHS: Record = { seq: 48, product: 140, product_desc: 120, unit: 48, avg_daily: 72, suggested: 88, available: 72, customers: 64, confidence: 56, action: 110, }; export const FORECAST_WIDTH_OPTIONS = { cellPadding: 24, defaultMaxContentWidth: 280, maxContentWidth: { seq: 56, product: 240, product_desc: 220, unit: 56, avg_daily: 96, suggested: 120, available: 96, customers: 80, confidence: 72, action: 140, } satisfies Partial>, }; /** 按 SKU 合并各客户预测量;可用库存取该 SKU 值(各客户行相同) */ export function mergeForecastBySku(rows: DemandForecastRowDto[]): ForecastMergedSkuRow[] { const map = new Map }>(); for (const row of rows) { const code = String(row.sku_code || '').trim(); if (!code) continue; let item = map.get(code); if (!item) { item = { sku_code: code, sku_name: String(row.sku_name || code), specification: String(row.specification || '').trim(), unit: String(row.unit || '').trim() || '个', avg_daily_qty: 0, suggested_qty: 0, available_qty: Number(row.available_qty) || 0, stockout_risk: false, confidence: row.confidence, customer_count: 0, order_lines: 0, _customers: new Set(), }; map.set(code, item); } item.avg_daily_qty += Number(row.avg_daily_qty) || 0; item.suggested_qty += Number(row.suggested_qty) || 0; item.available_qty = Number(row.available_qty) || item.available_qty; item.confidence = minConfidence(item.confidence, row.confidence); item.order_lines += Number(row.order_lines) || 0; if (!item.specification) { item.specification = String(row.specification || '').trim(); } if (!item.unit || item.unit === '个') { const u = String(row.unit || '').trim(); if (u) item.unit = u; } if (row.sku_name) item.sku_name = String(row.sku_name); const customer = String(row.customer_code || '').trim(); if (customer) item._customers.add(customer); } return [...map.values()] .map(({ _customers, ...rest }) => { const customer_count = _customers.size; const stockout_risk = rest.suggested_qty > rest.available_qty; return { ...rest, customer_count, stockout_risk }; }) .sort((a, b) => a.sku_name.localeCompare(b.sku_name, 'zh-CN')); } /** 商品名:同名聚在一起;预测需求:按数量 */ export function sortForecastMergedRows( rows: ForecastMergedSkuRow[], sortKey: ForecastSortKey | null, sortDir: ClientSortDir ): ForecastMergedSkuRow[] { if (!sortKey || !rows.length) return rows; const dir = sortDir === 'asc' ? 1 : -1; return [...rows].sort((a, b) => { if (sortKey === 'product') { const nameCmp = a.sku_name.localeCompare(b.sku_name, 'zh-CN'); if (nameCmp !== 0) return nameCmp * dir; const specCmp = a.specification.localeCompare(b.specification, 'zh-CN'); if (specCmp !== 0) return specCmp * dir; return a.sku_code.localeCompare(b.sku_code) * dir; } if (a.suggested_qty === b.suggested_qty) { return a.sku_name.localeCompare(b.sku_name, 'zh-CN'); } return a.suggested_qty < b.suggested_qty ? -dir : dir; }); } export function formatForecastMergedCopyText( rows: ForecastMergedSkuRow[], options?: { targetLabel?: string; modelLabel?: string; copyTitle?: string; copyRange?: string; copyModel?: string; unknownSku?: string; unitFallback?: string; } ): string { if (!rows.length) return ''; const unitFallback = options?.unitFallback ?? '个'; const unknownSku = options?.unknownSku ?? '未知商品'; const lines = rows.map((row, index) => { const name = row.sku_name || row.sku_code || unknownSku; const spec = row.specification ? `(${row.specification})` : ''; const unit = row.unit || unitFallback; return `${index + 1}. ${name}${spec} ${formatForecastQty(row.suggested_qty)}${unit}`; }); const header = [ options?.copyTitle ?? '【需求预测】合计', ...(options?.targetLabel ? [options.copyRange ?? `预测区间:${options.targetLabel}`] : []), ...(options?.modelLabel ? [options.copyModel ?? `模型:${options.modelLabel}`] : []), ]; return [...header, '', ...lines].join('\n'); }