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; console.log('===== br002 清零分界(7-29 ~ 7-31 建行的批次)====='); for (const r of await q(`SELECT created_at::date d, COUNT(*)::int batches, COUNT(*) FILTER (WHERE COALESCE(quantity,0)=0)::int zeroed, MIN(created_at)::timestamp(0) first_ts, MAX(created_at)::timestamp(0) last_ts FROM tenant_br002.inventory WHERE created_at::date BETWEEN '2026-07-28' AND '2026-08-01' GROUP BY 1 ORDER BY 1`)) console.log(` ${String(r.d).slice(0,10)} 批次 ${r.batches} 其中被清零 ${r.zeroed} 建行时间 ${r.first_ts} ~ ${r.last_ts}`); const [z] = await q(`SELECT COUNT(*)::int batches, COUNT(DISTINCT sku_code)::int skus, ROUND(SUM(quantity)::numeric,2) qty_kept_zero FROM tenant_br002.inventory WHERE COALESCE(quantity,0)=0 AND available_qty=0`); const [z2] = await q(`SELECT COUNT(*)::int batches, COUNT(DISTINCT i.sku_code)::int skus, ROUND(SUM(l.q)::numeric,2) inbound_qty FROM tenant_br002.inventory i JOIN (SELECT batch_no, SUM(quantity)::numeric q FROM tenant_br002.inventory_logs WHERE operation_type IN ('inspection_in','stock_in','processing_return') GROUP BY 1) l ON l.batch_no = i.batch_no WHERE COALESCE(i.quantity,0)=0 AND i.available_qty=0 AND l.q > 0`); console.log(` 全零批次 ${z.batches} 个(${z.skus} SKU);其中有入库流水但台账量=0 的 ${z2.batches} 个批次(${z2.skus} SKU),这些批次的入库流水合计 ${z2.inbound_qty}`); console.log('\n===== br002 差异 SKU:差额是否等于「被清零批次的入库流水量」 ====='); const rows = await q(` WITH diff AS ( SELECT COALESCE(i.sku_code, g.sku_code) sku_code, ROUND((COALESCE(i.q,0) - COALESCE(g.q,0))::numeric,2) diff FROM (SELECT sku_code, SUM(quantity)::numeric q FROM tenant_br002.inventory GROUP BY 1) i FULL OUTER JOIN (SELECT sku_code, SUM(CASE WHEN operation_type IN ('inspection_in','stock_in','processing_return') THEN quantity ELSE 0 END)::numeric q FROM tenant_br002.inventory_logs GROUP BY 1) g ON g.sku_code = i.sku_code WHERE ABS(COALESCE(i.q,0) - COALESCE(g.q,0)) > 0.01), zeroed AS ( SELECT i.sku_code, ROUND(SUM(l.q)::numeric,2) zq FROM tenant_br002.inventory i JOIN (SELECT batch_no, SUM(quantity)::numeric q FROM tenant_br002.inventory_logs WHERE operation_type IN ('inspection_in','stock_in','processing_return') GROUP BY 1) l ON l.batch_no = i.batch_no WHERE COALESCE(i.quantity,0)=0 GROUP BY 1) SELECT COUNT(*)::int skus, COUNT(*) FILTER (WHERE ABS(diff.diff + COALESCE(z.zq,0)) < 0.01)::int explained_by_zeroing FROM diff LEFT JOIN zeroed z ON z.sku_code = diff.sku_code`); console.log(' ', JSON.stringify(rows[0])); console.log('\n===== br003 差异 SKU:差额是否等于 adjust 流水净额 ====='); const b3 = await q(` WITH diff AS ( SELECT COALESCE(i.sku_code, g.sku_code) sku_code, ROUND((COALESCE(i.q,0)-COALESCE(g.q,0))::numeric,2) diff FROM (SELECT sku_code, SUM(quantity)::numeric q FROM tenant_br003.inventory GROUP BY 1) i FULL OUTER JOIN (SELECT sku_code, SUM(CASE WHEN operation_type IN ('inspection_in','stock_in','processing_return') THEN quantity ELSE 0 END)::numeric q FROM tenant_br003.inventory_logs GROUP BY 1) g ON g.sku_code=i.sku_code WHERE ABS(COALESCE(i.q,0)-COALESCE(g.q,0)) > 0.01), adj AS (SELECT sku_code, ROUND(SUM(CASE WHEN after_qty > before_qty THEN quantity ELSE -quantity END)::numeric,2) adj_net FROM tenant_br003.inventory_logs WHERE operation_type='adjust' GROUP BY 1) SELECT d.sku_code, d.diff, COALESCE(a.adj_net,0) adjust_net FROM diff d LEFT JOIN adj a ON a.sku_code=d.sku_code ORDER BY ABS(d.diff) DESC LIMIT 6`); for (const r of b3) console.log(` ${r.sku_code} 差额 ${r.diff} adjust 净额 ${r.adjust_net}`); await pool.end();