const text=value=>String(value??'').normalize('NFKC').trim() export const categoryIdentity=row=>JSON.stringify([row.name??row.materialName,row.spec??row.materialParams,row.brand??row.materialBrand].map(text)) export const categoryFingerprint=row=>JSON.stringify([row.materialName,row.materialParams,row.materialBrand,row.category,row.productId,row.unit].map(text)) export function categoryBatchSource(rows,editable,indexOf){ const eligible=[],skipped={unnamed:0,locked:0} for(const row of rows){ if(!text(row.materialName)){skipped.unnamed++;continue} if(!editable(row)){skipped.locked++;continue} eligible.push({key:String(row._key),line:indexOf(row)+1,name:row.materialName,spec:row.materialParams||'',brand:row.materialBrand||'',category:row.category||'',fingerprint:categoryFingerprint(row)}) } return {rows:eligible,skipped} } // One model task handles a packet of distinct descriptions; duplicates share its result. export async function runCategoryBatches(rows,{request,onResult,signal,packetSize=12,concurrency=3}){ const identities=new Map() for(const row of rows){const identity=categoryIdentity(row);if(!identities.has(identity))identities.set(identity,[]);identities.get(identity).push(row)} const groups=[...identities.values()],packets=[] for(let i=0;i{const row=group[0];return {key:row.key,name:row.name,spec:row.spec,brand:row.brand}}) let answers try{ const response=await request(items,signal);if(signal?.aborted)return answers=new Map();const duplicate=new Set() for(const result of response.data?.rows||[]){if(answers.has(result.key))duplicate.add(result.key);answers.set(result.key,result)} for(const key of duplicate)answers.set(key,{error:'此项结果重复,请重试'}) }catch(error){if(signal?.aborted)return;answers=new Map(items.map(item=>[item.key,{error:'本组未完成,请重试'}]))} for(const group of packet){const answer=answers.get(group[0].key)||{error:'此项未返回,请重试'};for(const row of group)onResult(row.key,answer)} } } await Promise.all(Array.from({length:Math.min(concurrency,packets.length)},worker)) return {uniqueCount:groups.length,total:rows.length} } // Check the whole selection before changing any local row. export function checkCategoryBatch(entries,currentRows,editable){ const current=new Map(currentRows.map(row=>[String(row._key),row])),seen=new Set() return entries.map(entry=>{ const row=current.get(entry.key) if(seen.has(entry.key)||!row||!editable(row)||categoryFingerprint(row)!==entry.fingerprint)throw new Error(`第 ${entry.line} 行已变化或不可编辑,请关闭预览后重新推荐`) seen.add(entry.key) if(!text(entry.value))throw new Error(`第 ${entry.line} 行请选择分类`) return {row,entry} }) }