import { createRequire } from 'node:module'; const require = createRequire('/home/ubuntu/harness/ai-fresh-delivery/server/index.js'); const { Pool } = require('pg'); const pool = new Pool({ connectionString: 'postgresql://hermes:Hermes_2026_DB@1.13.62.2:20538/ai_fresh_delivery', connectionTimeoutMillis: 20000, max: 2 }); const q = async (s,p)=> (await pool.query(s,p)).rows; const UP = "('inspection_in','stock_in','processing_return')"; for (const T of ['tenant_br002','tenant_br003']) { console.log(`\n=========== ${T} 批次级归因 ===========`); const [a] = await q(`SELECT COUNT(*)::int batches_with_logs, COUNT(*) FILTER (WHERE i.sku_code IS DISTINCT FROM l.log_sku)::int sku_rewritten FROM ${T}.inventory i JOIN (SELECT batch_no, MIN(sku_code) log_sku FROM ${T}.inventory_logs WHERE operation_type IN ${UP} GROUP BY 1) l ON l.batch_no = i.batch_no`); console.log('① 批次 sku_code 被改写(台账 sku ≠ 该批次入库流水的 sku):', a.sku_rewritten, '/', a.batches_with_logs); const [b] = await q(`SELECT COUNT(*)::int orphan_batches, COUNT(*) FILTER (WHERE operation_type IN ${UP})::int orphan_inbound, ROUND(COALESCE(SUM(quantity) FILTER (WHERE operation_type IN ${UP}),0)::numeric,2) orphan_qty FROM ${T}.inventory_logs l WHERE NOT EXISTS (SELECT 1 FROM ${T}.inventory i WHERE i.batch_no = l.batch_no)`); console.log('② 台账中已无该批次的流水(批次行被删/不存在):', b.orphan_batches, '条,其中入库类', b.orphan_inbound, '条=', b.orphan_qty); const [c] = await q(`SELECT COUNT(*)::int batches, COUNT(*) FILTER (WHERE cnt > 1)::int multi_inbound, ROUND(COALESCE(SUM(q),0)::numeric,2) qty FROM (SELECT batch_no, COUNT(*) cnt, SUM(quantity) q FROM ${T}.inventory_logs WHERE operation_type IN ${UP} GROUP BY 1) x`); console.log('③ 同一批次多条入库流水:', c.multi_inbound, '/', c.batches, '个批次(合计入库量', c.qty, ')'); // 逐 SKU:把「sku 被改写」造成的差值单独算出来 const rows = await q(`WITH inv AS (SELECT sku_code, SUM(quantity)::numeric q FROM ${T}.inventory GROUP BY 1), lg AS (SELECT sku_code, SUM(CASE WHEN operation_type IN ${UP} THEN quantity ELSE 0 END)::numeric q FROM ${T}.inventory_logs GROUP BY 1), rewritten AS (SELECT l.sku_code, SUM(l.quantity)::numeric lost FROM ${T}.inventory_logs l JOIN ${T}.inventory i ON i.batch_no = l.batch_no WHERE l.operation_type IN ${UP} AND l.sku_code IS DISTINCT FROM i.sku_code GROUP BY 1) SELECT COALESCE(i.sku_code,g.sku_code) sku, ROUND((COALESCE(i.q,0)-COALESCE(g.q,0))::numeric,2) diff, ROUND(COALESCE(r.lost,0)::numeric,2) rewritten_inbound FROM inv i FULL OUTER JOIN lg g ON g.sku_code=i.sku_code LEFT JOIN rewritten r ON r.sku_code=COALESCE(i.sku_code,g.sku_code) WHERE ABS(COALESCE(i.q,0)-COALESCE(g.q,0)) > 0.01`); const explained = rows.filter(r => Math.abs(Number(r.diff) + Number(r.rewritten_inbound)) < 0.01); const unexplained = rows.filter(r => !(Math.abs(Number(r.diff) + Number(r.rewritten_inbound)) < 0.01)); console.log('④ 差异 SKU 总数:', rows.length, '|可用「批次 sku 改写」解释:', explained.length, '(差值合计', explained.reduce((s,r)=>s+Math.abs(Number(r.diff)),0).toFixed(2), ')|仍未解释:', unexplained.length, '(绝对差', unexplained.reduce((s,r)=>s+Math.abs(Number(r.diff)),0).toFixed(2), ')'); for (const r of unexplained.sort((x,y)=>Math.abs(Number(y.diff))-Math.abs(Number(x.diff))).slice(0,5)) console.log(' 未解释:', r.sku, 'diff=', r.diff, '改写量=', r.rewritten_inbound); } await pool.end();