56 lines
2.9 KiB
TypeScript
56 lines
2.9 KiB
TypeScript
/**
|
|
* 出货聊天:库存列映射 payload 读取与字段对齐 Markdown 生成(纯函数)。
|
|
*
|
|
* 消息 type === 'shipping-inventory-mapping' 时 payload 含 schema_preview + form。
|
|
*/
|
|
import { normalizeShippingHeaderForDisplay } from './shippingText';
|
|
import type { ChatMessage } from '../types/chat.types';
|
|
import type { ShippingInventoryMappingFormPayload } from '../types/shipping.types';
|
|
|
|
export function getShippingInventoryMappingPayload(chatMsg: ChatMessage): ShippingInventoryMappingFormPayload | null {
|
|
if (!chatMsg || chatMsg.type !== 'shipping-inventory-mapping') return null;
|
|
const payload = chatMsg.payload;
|
|
if (!payload || typeof payload !== 'object') return null;
|
|
const taskId = String((payload as ShippingInventoryMappingFormPayload).taskId || '').trim();
|
|
const schemaPreview = (payload as ShippingInventoryMappingFormPayload).schema_preview;
|
|
const form = (payload as ShippingInventoryMappingFormPayload).form;
|
|
if (!taskId || !schemaPreview || !form) return null;
|
|
return payload as ShippingInventoryMappingFormPayload;
|
|
}
|
|
|
|
export function hasShippingInventorySampleRows(chatMsg: ChatMessage): boolean {
|
|
const rows = getShippingInventoryMappingPayload(chatMsg)?.schema_preview?.sample_rows;
|
|
return Array.isArray(rows) && rows.length > 0;
|
|
}
|
|
|
|
export function getShippingInventorySampleRowText(chatMsg: ChatMessage): string {
|
|
const row = getShippingInventoryMappingPayload(chatMsg)?.schema_preview?.sample_rows?.[0];
|
|
return Array.isArray(row) ? row.map((cell) => String(cell)).join(' | ') : '';
|
|
}
|
|
|
|
export function buildShippingFieldAlignmentText(payload: { data?: { field_alignment?: Record<string, unknown> } }): string {
|
|
const alignment = payload?.data?.field_alignment;
|
|
if (!alignment || typeof alignment !== 'object') return '';
|
|
const invCols = (alignment as { inventory_columns?: { mapping?: Record<string, { source_column?: string }> } }).inventory_columns || {};
|
|
const mapping = invCols.mapping || {};
|
|
const rows: Array<[string, string]> = [
|
|
['sku', normalizeShippingHeaderForDisplay(mapping?.sku?.source_column) || '(未识别)'],
|
|
['reservation_id', normalizeShippingHeaderForDisplay(mapping?.reservation_id?.source_column) || '(未识别/可选)'],
|
|
['available_qty', normalizeShippingHeaderForDisplay(mapping?.available_qty?.source_column) || '(未识别)'],
|
|
];
|
|
const lines: string[] = [];
|
|
const matchRule = String((alignment as { match_rule?: string }).match_rule || '').trim();
|
|
if (matchRule) lines.push(`匹配规则:${matchRule}`);
|
|
lines.push('', '字段对齐表:', '', '|系统字段|库存列|', '|---|---|');
|
|
for (const [k, v] of rows) {
|
|
lines.push(`|${k}|${v}|`);
|
|
}
|
|
const warnings = Array.isArray((alignment as { warnings?: unknown[] }).warnings)
|
|
? (alignment as { warnings: unknown[] }).warnings.filter(Boolean)
|
|
: [];
|
|
if (warnings.length) {
|
|
lines.push('', '提示:', ...warnings.map((w) => `- ${String(w)}`));
|
|
}
|
|
return lines.join('\n').trim();
|
|
}
|