import type { ParsedExcel } from './excelImportExport'; /** 系统字段词典项 */ export interface ImportFieldDef { /** 系统字段 key(映射值,也是 remap 后行的键) */ key: string; /** 界面显示名 */ label: string; /** 是否必填(必填未映射则禁止导入) */ required?: boolean; /** 精确归一化匹配词(小写、去空格、去括号注释、去尾*) */ aliases: string[]; /** 弱匹配正则:命中且唯一 → medium 置信 */ patterns?: RegExp[]; /** 系统标准模板表头(remap 后写入的键,兼容现有 getCell 别名链) */ templateHeader: string; } export type MappingConfidence = 'high' | 'medium' | 'none'; export interface ColumnMapping { /** 源表头 → 系统字段 key(空字符串 = 忽略此列) */ [sourceHeader: string]: string; } /** 归一化表头用于匹配 */ function normalizeHeader(h: string): string { return String(h || '') .trim() .toLowerCase() .replace(/\s+/g, '') .replace(/(/g, '(') .replace(/)/g, ')') .replace(/\([^)]*\)/g, '') .replace(/\*$/g, ''); } /** 表头签名:归一化后排序拼接的简易 hash(用于记忆键) */ export function headerSignature(headers: string[]): string { const norm = headers.map(normalizeHeader).filter(Boolean).sort().join('|'); let h1 = 0xdeadbeef; let h2 = 0x41c6ce57; for (let i = 0; i < norm.length; i += 1) { const ch = norm.charCodeAt(i); h1 = Math.imul(h1 ^ ch, 2654435761); h2 = Math.imul(h2 ^ ch, 1597334677); } h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909); h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909); return (h2 >>> 0).toString(16) + (h1 >>> 0).toString(16); } /** * 自动映射:为每个源表头在词典里找系统字段。 * 返回 { mapping, confidence, matchedBy }。confidence: 该源列命中置信度。 */ export function autoMapColumns( headers: string[], fields: ImportFieldDef[] ): { mapping: ColumnMapping; confidence: Record } { const mapping: ColumnMapping = {}; const confidence: Record = {}; const usedFieldKeys = new Set(); // 第一轮:别名精确命中 → high for (const header of headers) { if (!header) continue; const norm = normalizeHeader(header); if (!norm) continue; for (const field of fields) { if (usedFieldKeys.has(field.key)) continue; if (field.aliases.map(normalizeHeader).includes(norm)) { mapping[header] = field.key; confidence[header] = 'high'; usedFieldKeys.add(field.key); break; } } } // 第二轮:pattern 弱命中(唯一候选)→ medium for (const header of headers) { if (!header || mapping[header]) continue; const norm = normalizeHeader(header); if (!norm) continue; const candidates = fields.filter( (f) => !usedFieldKeys.has(f.key) && (f.patterns || []).some((p) => p.test(norm)) ); if (candidates.length === 1) { mapping[header] = candidates[0].key; confidence[header] = 'medium'; usedFieldKeys.add(candidates[0].key); } } // 其余 → 忽略('') for (const header of headers) { if (header && mapping[header] === undefined) { mapping[header] = ''; confidence[header] = 'none'; } } return { mapping, confidence }; } /** * 用映射把行重命名为「系统标准模板表头」键,兼容现有 getCell 别名链。 * mapping 里 value='' 的列被忽略。 */ export function remapRows( rows: Record[], mapping: ColumnMapping, fields: ImportFieldDef[] ): Record[] { const keyToTemplate = new Map(fields.map((f) => [f.key, f.templateHeader])); return rows.map((row) => { const out: Record = { __row: row.__row || '' }; for (const [sourceHeader, fieldKey] of Object.entries(mapping)) { if (!fieldKey) continue; const templateHeader = keyToTemplate.get(fieldKey); if (!templateHeader) continue; if (row[sourceHeader] !== undefined) { out[templateHeader] = row[sourceHeader]; } } return out; }); } /** 校验必填字段是否都已映射到某源列 */ export function missingRequiredFields( mapping: ColumnMapping, fields: ImportFieldDef[] ): ImportFieldDef[] { const mappedKeys = new Set(Object.values(mapping).filter(Boolean)); return fields.filter((f) => f.required && !mappedKeys.has(f.key)); } export type { ParsedExcel };