import { createRequire } from 'node:module'; import { writeFileSync } from 'node:fs'; 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')"; const out = '/home/ubuntu/harness/ai-fresh-delivery/docs/audit/attachments'; const summary = []; for (const T of ['tenant_br002','tenant_br003','tenant_br004']) { const rows = await q(` WITH inv AS (SELECT sku_code, COUNT(*)::int b, SUM(quantity)::numeric iq, SUM(available_qty)::numeric aq, ROUND(SUM(COALESCE(cost_price,0)*quantity)::numeric,2) val FROM ${T}.inventory GROUP BY 1), lg AS (SELECT sku_code, SUM(CASE WHEN operation_type IN ${UP} THEN quantity ELSE 0 END)::numeric lq, COUNT(*)::int n FROM ${T}.inventory_logs GROUP BY 1), zeroed AS (SELECT i.sku_code, ROUND(SUM(l.q)::numeric,2) zq, COUNT(*)::int zb FROM ${T}.inventory i JOIN (SELECT batch_no, SUM(quantity)::numeric q FROM ${T}.inventory_logs WHERE operation_type IN ${UP} GROUP BY 1) l ON l.batch_no=i.batch_no WHERE COALESCE(i.quantity,0)=0 GROUP BY 1), skus AS (SELECT sku_code, sku_name FROM ${T}.skus) SELECT COALESCE(i.sku_code,g.sku_code) sku_code, s.sku_name, COALESCE(i.b,0)::int batches, ROUND(COALESCE(i.iq,0)::numeric,2) ledger_qty, COALESCE(i.aq,0)::numeric avail, ROUND(COALESCE(g.lq,0)::numeric,2) inbound_log_qty, ROUND((COALESCE(i.iq,0)-COALESCE(g.lq,0))::numeric,2) diff, COALESCE(z.zb,0)::int zeroed_batches, ROUND(COALESCE(z.zq,0)::numeric,2) zeroed_batches_inbound_qty, COALESCE(i.val,0) ledger_value FROM inv i FULL OUTER JOIN lg g ON g.sku_code=i.sku_code LEFT JOIN zeroed z ON z.sku_code=COALESCE(i.sku_code,g.sku_code) LEFT JOIN skus s ON s.sku_code=COALESCE(i.sku_code,g.sku_code) WHERE ABS(COALESCE(i.iq,0)-COALESCE(g.lq,0)) > 0.01 ORDER BY ABS(COALESCE(i.iq,0)-COALESCE(g.lq,0)) DESC`, []); const csv = ['sku_code,sku_name,batches,ledger_qty,available_qty,inbound_log_qty,diff,zeroed_batches,zeroed_batches_inbound_qty,ledger_value', ...rows.map(r=>[r.sku_code, (r.sku_name||'').replace(/,/g,' '), r.batches, r.ledger_qty, r.avail, r.inbound_log_qty, r.diff, r.zeroed_batches, r.zeroed_batches_inbound_qty, r.ledger_value].join(','))].join('\n'); writeFileSync(`${out}/20260915-台账流水差异-${T.replace('tenant_','')}.csv`, csv); const explained = rows.filter(r => Math.abs(Number(r.diff) + Number(r.zeroed_batches_inbound_qty)) < 0.01).length; summary.push({ T, skus: rows.length, absDiff: rows.reduce((s,r)=>s+Math.abs(Number(r.diff)),0).toFixed(2), explained }); } console.log(JSON.stringify(summary, null, 1)); // 幽灵预留 const ghosts = []; for (const T of ['tenant_br002','tenant_br003','tenant_br004']) { const rows = await q(`SELECT r.order_code, r.sku_code, s.sku_name, r.qty::numeric qty, o.order_status, o.delivery_date::date delivery, r.created_at::date created, c.company_name, (SELECT ROUND(SUM(available_qty)::numeric,2) FROM ${T}.inventory i WHERE i.sku_code=r.sku_code) avail, (SELECT COUNT(*) FROM ${T}.inventory_logs l WHERE l.sku_code=r.sku_code AND l.related_order_code=r.order_code) logs FROM ${T}.inventory_reservations r LEFT JOIN ${T}.orders o ON o.order_code=r.order_code LEFT JOIN ${T}.customers c ON c.customer_code=o.customer_code LEFT JOIN ${T}.skus s ON s.sku_code=r.sku_code WHERE r.status=10 ORDER BY r.order_code, r.sku_code`, []); for (const r of rows) ghosts.push({ tenant: T.replace('tenant_',''), ...r }); } writeFileSync(`${out}/20260915-活跃预留清单.csv`, ['tenant,order_code,sku_code,sku_name,qty,order_status,delivery_date,reserved_at,customer,sku_available,order_outbound_logs,is_ghost', ...ghosts.map(g=>[g.tenant,g.order_code,g.sku_code,(g.sku_name||'').replace(/,/g,' '),g.qty,g.order_status,g.delivery?new Date(g.delivery).toISOString().slice(0,10):'',new Date(g.created).toISOString().slice(0,10),(g.company_name||'').replace(/,/g,' '),g.avail,g.logs,Number(g.order_status)>=45?'是':'否'].join(','))].join('\n')); console.log('活跃预留', ghosts.length, '条,其中幽灵(订单已送达/结案)', ghosts.filter(g=>Number(g.order_status)>=45).length, '条'); await pool.end();