Files
SupplierSystem/docs/audit/scripts/20260915-inventory-audit/attribution.mjs
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

41 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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();