/** * 只读巡检:**标签码同码撞车**(2026-10-13)。 * * 背景(真库复现,见 docs/design/20261013-扫码按标签值定位行(消除跨系统SKU同码错行).md): * 分拣标签印的是"该行的主系统 SKU";若这个字符串**恰好也是我方另一个商品的编码/条码**, * 旧解析(先把码翻译成我方 SKU 再按 SKU 找行)会**静默把数量记到另一个商品**上。 * 新解析按"纸上的码"优先定位到行、歧义直接报错,不会错行;但**撞车本身**要能提前看见。 * * 本脚本只读、不改任何数据,检查三类: * A 危险撞车:台账 `main_sku_code` 命中我方**另一个** SKU(撞车 + 对应不同商品); * B 歧义标签:**同一张分拣单内**两个不同 SKU 的行会印出同一个码(扫码时无法区分,会报错要人工确认); * C 印不出码:未完成分拣单里标签值为空的行。 * * 用法:`npx tsx scripts/audit-label-barcode-collisions.ts [BR002 BR003 ...]` * 不带分公司参数 ⇒ 巡检 `public.branches` 里全部已登记分公司。退出码恒为 0(只报告)。 */ import { pool } from '../src/db'; import { runWithTenantDbContext } from '../src/services/tenantDbContext'; import { ORDER_ITEM_LABEL_BARCODE_SQL, ORDER_ITEM_LABEL_LEDGER_JOIN_SQL, ORDER_ITEM_OWN_SKU_CODE_SQL, } from '../src/services/orderItemLabelBarcodeSql'; const ONLY = process.argv.slice(2).filter((a) => /^BR\d+$/i.test(a)).map((a) => a.toUpperCase()); async function resolveBranches(): Promise { if (ONLY.length) return ONLY; const r = await pool.query<{ branch_code: string }>( `SELECT branch_code FROM public.branches WHERE status IS DISTINCT FROM 'inactive' ORDER BY branch_code` ); return r.rows.map((row) => String(row.branch_code)); } /** 分拣行的标签值 / 我方 SKU(与打印、扫码解析同一份取值链) */ const PICK_ROWS_SQL = ` SELECT pi.pick_code, ${ORDER_ITEM_LABEL_BARCODE_SQL} AS label_value, ${ORDER_ITEM_OWN_SKU_CODE_SQL} AS sku_code, pi.item_name FROM picking_items pi LEFT JOIN order_items oi ON oi.id = pi.order_item_id LEFT JOIN product_catalog catalog ON catalog.item_type = 'product' AND ( catalog.code = COALESCE(NULLIF(TRIM(pi.sku_code), ''), NULLIF(TRIM(oi.sku_code), '')) OR ( COALESCE(NULLIF(TRIM(pi.sku_code), ''), NULLIF(TRIM(oi.sku_code), '')) IS NULL AND TRIM(catalog.name) = TRIM(pi.item_name) ) ) LEFT JOIN skus s ON s.sku_code = COALESCE( NULLIF(TRIM(pi.sku_code), ''), NULLIF(TRIM(oi.sku_code), ''), catalog.code ) ${ORDER_ITEM_LABEL_LEDGER_JOIN_SQL} WHERE pi.pick_code IN (SELECT pick_code FROM picking_tasks WHERE pick_status < 40)`; async function auditBranch(branch: string): Promise { return runWithTenantDbContext(branch, async () => { console.log(`\n════════ ${branch} ════════`); const dangerous = await pool.query<{ main_sku_code: string; colliding_sku: string; colliding_name: string | null; line_sku: string | null; line_name: string | null; our_order_code: string | null; }>( `SELECT msi.main_sku_code, s.sku_code AS colliding_sku, s.sku_name AS colliding_name, oi.sku_code AS line_sku, oi.item_name AS line_name, msi.our_order_code FROM public.main_system_order_import_items msi JOIN order_items oi ON oi.id = msi.our_order_item_id JOIN skus s ON (s.sku_code = msi.main_sku_code OR s.barcode = msi.main_sku_code) WHERE msi.branch_code = $1 AND msi.main_sku_code IS NOT NULL AND s.sku_code IS DISTINCT FROM oi.sku_code ORDER BY msi.our_order_code DESC`, [branch] ); console.log(`A 危险撞车(台账SKU = 我方另一个商品):${dangerous.rowCount} 条`); for (const row of dangerous.rows) { console.log( ` ✗ ${row.main_sku_code} ⇒ 撞到我方 ${row.colliding_sku}(${row.colliding_name || '?'})` + `,该行本应是 ${row.line_sku || '—'}(${row.line_name || '—'}) ${row.our_order_code || ''}` ); } const ambiguous = await pool.query<{ pick_code: string; label_value: string; skus: string; }>( `SELECT pick_code, MIN(label_value) AS label_value, string_agg(DISTINCT sku_code, ' / ') AS skus FROM (${PICK_ROWS_SQL}) t WHERE label_value IS NOT NULL GROUP BY pick_code, UPPER(TRIM(label_value)) HAVING COUNT(DISTINCT sku_code) > 1 ORDER BY pick_code`, [] ); console.log(`B 歧义标签(同一分拣单里不同 SKU 印同一个码):${ambiguous.rowCount} 条`); for (const row of ambiguous.rows) { console.log(` ✗ ${row.pick_code} 「${row.label_value}」⇒ ${row.skus}`); } const unprintable = await pool.query<{ n: string }>( `SELECT COUNT(*)::text AS n FROM (${PICK_ROWS_SQL}) t WHERE label_value IS NULL`, [] ); console.log(`C 印不出码(未完成分拣单里标签值为空的行):${unprintable.rows[0]?.n || 0} 行`); return (dangerous.rowCount || 0) + (ambiguous.rowCount || 0); }); } async function main() { const branches = await resolveBranches(); let problems = 0; let failures = 0; for (const branch of branches) { try { problems += await auditBranch(branch); } catch (error) { failures += 1; console.error(`\n════════ ${branch} ════════\n ⚠️ 巡检失败:${(error as Error)?.message || error}`); } } console.log( (failures > 0 ? `\n⚠️ 有 ${failures} 个分公司巡检失败 —— 上面的"0 条"不代表干净。` : '') + (problems === 0 ? '\n✅ 无标签码撞车(A/B 均为 0)。' : `\n⚠️ 发现 ${problems} 处标签码撞车风险 —— 扫码时会按"纸上的码"定位到行,歧义(B)会拒绝并提示人工确认;` + `危险撞车(A)若两行同处一张分拣单,请改 SKU 编码或按设计文档 §4 处理。`) ); await pool.end(); } main().catch(async (error) => { console.error('[audit-label-barcode-collisions] 失败:', error); await pool.end(); process.exit(1); });