/** * 知识库 ↔ 特征建模 跨层映射。 * * 知识库切片里的「所属特征树」是形如 `SRV-2U-620.焊接.激光焊焊瘤` 的语义码; * 特征建模侧(DS-01/08/09/12)的实例 id 是料号 / 结构特征维度 / 试验号等。 * 二者无法直接字符串匹配,因此这里维护一份精选映射表,并反向构建索引, * 供两侧页面做双向跳转。 */ import { RoleKey, roleDocs } from './knowledge'; export interface FeatureRef { /** 特征建模中的对象 key,例如 'DS-09' */ objKey: string; /** 特征建模中的实例 id;省略表示映射到对象层 */ instanceId?: string; /** 为什么这两个东西相关(一句话,展示在跳转入口里) */ why: string; } export interface KbRef { role: RoleKey; chunkId: string; /** 一句话摘要,用于特征建模侧「知识引用」列表 */ label: string; /** 对应的特征码 */ code: string; } /** * 精选映射:每个知识库特征码 → 一个或多个特征建模落点。 * 映射以「工序 + 缺陷/特征」语义为准,优先选择最能支撑该切片结论的实例。 */ export const FEATURE_LINKS: Record = { 'SRV-2U-620.NCT.手工倒角': [ { objKey: 'DS-01', instanceId: 'SRV2U-BASE-01', why: '机箱底座(双侧折弯 + R 角)是去毛刺 / 倒角高发场景' }, ], 'SRV-2U-620.NCT.孔位': [ { objKey: 'DS-01', instanceId: 'SRV2U-FANW-01', why: '风扇墙含大量网孔阵列,是同机型冲压 / 孔位特征' }, ], 'SRV-2U-620.折弯.回弹补偿': [ { objKey: 'DS-01', instanceId: 'SRV2U-TOP-01', why: '机箱上盖为四边折弯结构,是回弹补偿关键件' }, ], 'SRV-2U-620.焊接.激光焊焊瘤': [ { objKey: 'DS-09', instanceId: '密集焊点(≥12 点)', why: 'DS-09 中焊瘤率最高的结构特征维度(0.141)' }, { objKey: 'DS-12', instanceId: 'T5', why: 'DOE 推荐组合(1200W / 气 15 / 焊速 38)可抑制焊瘤' }, ], 'SRV-2U-620.焊接.焊接变形': [ { objKey: 'DS-09', instanceId: '长条形(>400mm)', why: 'DS-09 中焊接变形率最高的结构特征维度(0.087)' }, ], 'SRV-2U-620.表面处理.色差': [ { objKey: 'DS-09', instanceId: '镀层板(SGCC)', why: '镀层板表面处理对色差最敏感' }, { objKey: 'DS-01', instanceId: 'SRV2U-HDDCAGE-01', why: '同机型 SGCC 材质件' }, ], }; export const featureRefsOf = (code?: string): FeatureRef[] => FEATURE_LINKS[code ?? ''] ?? []; const INDEX_KEY = (objKey: string, instanceId?: string) => `${objKey}#${instanceId ?? ''}`; const shortLabel = (text: string, max = 22) => { if (text.length <= max) return text; return text.slice(0, max) + '…'; }; const buildIndex = () => { const idx = new Map(); for (const doc of roleDocs) { const treeCol = doc.chunkCols.find(c => c.includes('特征树')); if (!treeCol) continue; for (const ch of doc.chunks) { const code = ch.cells[treeCol]; if (!code) continue; const refs = FEATURE_LINKS[code]; if (!refs) continue; for (const ref of refs) { const k = INDEX_KEY(ref.objKey, ref.instanceId); const arr = idx.get(k) ?? []; if (!arr.some(r => r.role === doc.role && r.chunkId === ch.id && r.code === code)) { arr.push({ role: doc.role, chunkId: ch.id, label: shortLabel(ch.text), code }); idx.set(k, arr); } } } } return idx; }; const KB_INDEX = buildIndex(); /** * 查询特征建模某对象(或某实例)被哪些知识库切片引用。 * instanceId 省略时,返回该对象下所有实例的引用。 */ export const kbRefsFor = (objKey: string, instanceId?: string): KbRef[] => { const exactKey = INDEX_KEY(objKey, instanceId); const out = KB_INDEX.get(exactKey) ? [...KB_INDEX.get(exactKey)!] : []; if (instanceId) return out; // 对象层:再合并该对象所有实例的引用 const prefix = `${objKey}#`; for (const [k, refs] of KB_INDEX) { if (!k.startsWith(prefix) || k === exactKey) continue; for (const r of refs) { if (!out.some(x => x.role === r.role && x.chunkId === r.chunkId && x.code === r.code)) { out.push(r); } } } return out; };