45 lines
3.1 KiB
JavaScript
45 lines
3.1 KiB
JavaScript
'use strict';
|
||
|
||
// Fixed field boundaries keep extraction deterministic; raw title stays on the event.
|
||
const summaryKeys={'机芯号':'coreNo','产品型号':'productModel','客户简称':'customerShort','出货方式':'shippingMethod','总数量':'totalQuantity','订单类别':'orderCategory'};
|
||
const retained=new Set(['订单号',...Object.keys(summaryKeys)]);
|
||
function extractTitle(title){
|
||
const raw=typeof title==='string'?title:'';
|
||
const text=raw.replace(/<br\s*\/?\s*>/gi,'\n').replace(/ | | /gi,' ');
|
||
const matches=[...text.matchAll(/(客户等级|毛利|订单号|机芯号|产品型号|客户简称|出货方式|总数量|订单类别|订单类型|业务模式)\s*[::]/gu)];
|
||
const orders=[],factoryHints=[],issues=[];let current=null,pendingMode=null,pendingAmbiguous=false;
|
||
for(let i=0;i<matches.length;i++){
|
||
const match=matches[i],label=match[1],value=text.slice(match.index+match[0].length,matches[i+1]?.index??text.length).trim();
|
||
if(['业务模式','客户简称'].includes(label)&&value.includes('惠州'))factoryHints.push({source:label,value,status:'待核实额外通知'});
|
||
if(label==='订单号'&&!/(?:原|参考)$/.test(text.slice(0,match.index))){
|
||
// Unknown labels and pipe-separated titles still delimit the identifier.
|
||
// Keep their original content in the notification paragraph below.
|
||
const identifier=/^([A-Za-z0-9][A-Za-z0-9._/-]*)(?=$|\s|[\p{Script=Han}|,,;;])/u.exec(value)?.[1]||value;
|
||
current={orderNo:identifier,text:''};if(pendingMode){current.businessMode=pendingMode;if(pendingAmbiguous)current.businessModeAmbiguous=true;pendingMode=null;pendingAmbiguous=false;}orders.push(current);
|
||
if(!value)issues.push('标题中的订单号为空,不能遗漏该订单段落');
|
||
}
|
||
if(label==='业务模式'){
|
||
const nextOrder=matches.findIndex((m,index)=>index>i&&m[1]==='订单号');
|
||
const between=nextOrder<0?[]:matches.slice(i+1,nextOrder).map(m=>m[1]);
|
||
// Summary fields after a mode still belong to the current order, irrespective
|
||
// of line wrapping. Only a header with no intervening summary binds forward.
|
||
const startsParagraph=/[\n|]\s*$/.test(text.slice(0,match.index));
|
||
const hasOwnSummary=between.some(label=>Object.hasOwn(summaryKeys,label));
|
||
const forward=nextOrder>=0&&!hasOwnSummary&&(between.includes('毛利')||startsParagraph);
|
||
if(current&&nextOrder>=0&&!hasOwnSummary&&!forward){
|
||
// An unmarked boundary cannot establish which adjacent order owns the mode.
|
||
current.businessModeAmbiguous=true;pendingMode=value;pendingAmbiguous=true;
|
||
}
|
||
else if(!current||forward){
|
||
if(pendingMode&&pendingMode!==value)pendingAmbiguous=true;
|
||
pendingMode=value;
|
||
}
|
||
else if(current){if(current.businessMode&¤t.businessMode!==value)current.businessModeAmbiguous=true;current.businessMode=value;}
|
||
}
|
||
if(current&&retained.has(label)){current.text+=`${label}:${value}`;if(summaryKeys[label])current[summaryKeys[label]]=value;}
|
||
}
|
||
if(!orders.length)issues.push('标题中没有可识别的“订单号:”段落');
|
||
return {rawTitle:raw,orders,brief:orders.map(order=>order.text).join('\n'),factoryHints,issues};
|
||
}
|
||
module.exports={extractTitle};
|