Files
SupplierSystem/src/utils/smartColumnMapping.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

141 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<string, MappingConfidence> } {
const mapping: ColumnMapping = {};
const confidence: Record<string, MappingConfidence> = {};
const usedFieldKeys = new Set<string>();
// 第一轮:别名精确命中 → 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<string, string>[],
mapping: ColumnMapping,
fields: ImportFieldDef[]
): Record<string, string>[] {
const keyToTemplate = new Map(fields.map((f) => [f.key, f.templateHeader]));
return rows.map((row) => {
const out: Record<string, string> = { __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 };