Files
SupplierSystem/docs/audit/scripts/20260915-inventory-audit/final-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

50 lines
4.0 KiB
JavaScript

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();