import { pool } from './src/db'; import { runWithTenantDataAccess } from './src/services/tenantSchemaService'; async function main() { const branch = process.argv[2] || 'BR002'; await runWithTenantDataAccess(branch, async () => { // 零价订单的创建时间 vs 报价生效时间:是否录单时报价还没生效? const timing = await pool.query( `SELECT o.order_code, o.customer_code, oi.sku_code, oi.unit AS item_unit, o.created_at AS order_created, (SELECT MIN(q.effective_at) FROM customer_quote_versions q WHERE q.customer_code=o.customer_code AND q.sku_code=oi.sku_code AND q.status=1 ) AS first_quote_effective, (SELECT MAX(q.effective_at) FROM customer_quote_versions q WHERE q.customer_code=o.customer_code AND q.sku_code=oi.sku_code AND q.status=1 ) AS latest_quote_effective FROM order_items oi JOIN orders o ON o.order_code=oi.order_code AND o.deleted_at IS NULL WHERE o.order_status >= 90 AND COALESCE(NULLIF(oi.locked_price,0), NULLIF(oi.price,0), CASE WHEN COALESCE(oi.quantity,0)>0 THEN COALESCE(oi.amount,0)/oi.quantity ELSE 0 END, 0) = 0 ORDER BY o.created_at` ); console.log('=== 零价明细:录单时间 vs 报价生效时间 ==='); for (const r of timing.rows) { const oc = r.order_created ? new Date(r.order_created).toISOString().slice(0,16) : '-'; const fq = r.first_quote_effective ? new Date(r.first_quote_effective).toISOString().slice(0,16) : '(无)'; const lq = r.latest_quote_effective ? new Date(r.latest_quote_effective).toISOString().slice(0,16) : '(无)'; const quoteAfterOrder = r.first_quote_effective && new Date(r.first_quote_effective) > new Date(r.order_created); console.log(` ${r.order_code.slice(-8)} | ${r.sku_code.slice(-6)} unit=${r.item_unit} | 录单 ${oc} | 报价生效 ${fq}~${lq} ${quoteAfterOrder?'⚠️报价晚于录单':''}`); } // 单位不匹配统计:明细unit vs 报价unit const unitMiss = await pool.query( `SELECT oi.unit AS item_unit, COUNT(*)::int AS cnt FROM order_items oi JOIN orders o ON o.order_code=oi.order_code AND o.deleted_at IS NULL WHERE o.order_status >= 90 AND COALESCE(NULLIF(oi.locked_price,0), NULLIF(oi.price,0), CASE WHEN COALESCE(oi.quantity,0)>0 THEN COALESCE(oi.amount,0)/oi.quantity ELSE 0 END, 0) = 0 GROUP BY oi.unit ORDER BY cnt DESC` ); console.log('\n=== 零价明细的单位分布 ==='); for (const r of unitMiss.rows) console.log(` unit='${r.item_unit}': ${r.cnt} 条`); // 报价表的单位分布(这些客户-SKU) const quoteUnits = await pool.query( `SELECT DISTINCT oi.unit AS item_unit, q.unit AS quote_unit, COUNT(*)::int AS cnt FROM order_items oi JOIN orders o ON o.order_code=oi.order_code AND o.deleted_at IS NULL LEFT JOIN customer_quote_versions q ON q.customer_code=o.customer_code AND q.sku_code=oi.sku_code AND q.status=1 WHERE o.order_status >= 90 AND COALESCE(NULLIF(oi.locked_price,0), NULLIF(oi.price,0), CASE WHEN COALESCE(oi.quantity,0)>0 THEN COALESCE(oi.amount,0)/oi.quantity ELSE 0 END, 0) = 0 GROUP BY oi.unit, q.unit ORDER BY cnt DESC LIMIT 20` ); console.log('\n=== 明细单位 vs 报价单位 ==='); for (const r of quoteUnits.rows) { const qu = r.quote_unit == null ? '(无报价行)' : r.quote_unit; console.log(` 明细unit='${r.item_unit}' 报价unit='${qu}': ${r.cnt} 条`); } }); await pool.end(); } main().catch((e) => { console.error(e); process.exit(1); });