import { pool } from './src/db'; import { runWithTenantDataAccess } from './src/services/tenantSchemaService'; async function main() { const branch = process.argv[2] || 'BR002'; await runWithTenantDataAccess(branch, async () => { // 1) 零价明细的 price_source / needs_customer_price 分布 const dist = await pool.query( `SELECT COALESCE(oi.price_source,'(null)') AS price_source, COALESCE(oi.needs_customer_price, false) AS needs_customer_price, COUNT(DISTINCT oi.order_code)::int AS order_cnt, COUNT(*)::int AS item_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 1,2 ORDER BY item_cnt DESC` ); console.log('=== 零价明细 price_source / needs_customer_price 分布 ==='); for (const r of dist.rows) { console.log(` price_source=${r.price_source} needs_customer_price=${r.needs_customer_price} -> ${r.order_cnt} 单 / ${r.item_cnt} 条`); } // 2) 这些客户-SKU 是否有生效报价单售价 const quoteCheck = await pool.query( `WITH zero_items AS ( SELECT DISTINCT o.customer_code, oi.sku_code 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 AND oi.sku_code IS NOT NULL ) SELECT zi.customer_code, zi.sku_code, s.sku_name, (SELECT COUNT(*) FROM customer_quote_sheet_items i JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code WHERE qsc.customer_code = zi.customer_code AND i.sku_code = zi.sku_code AND qs.approval_status = 30 AND qs.status = 1 AND COALESCE(NULLIF(i.sale_price,0), NULLIF(i.price,0), 0) > 0 )::int AS active_quote_cnt, (SELECT MAX(COALESCE(NULLIF(i.sale_price,0), NULLIF(i.price,0), 0)) FROM customer_quote_sheet_items i JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code WHERE qsc.customer_code = zi.customer_code AND i.sku_code = zi.sku_code AND qs.approval_status = 30 AND qs.status = 1 ) AS max_quote_price FROM zero_items zi LEFT JOIN skus s ON s.sku_code = zi.sku_code ORDER BY active_quote_cnt DESC, zi.customer_code` ); const hasQuote = quoteCheck.rows.filter((r) => Number(r.active_quote_cnt) > 0); const noQuote = quoteCheck.rows.filter((r) => Number(r.active_quote_cnt) === 0); console.log(`\n=== 零价 SKU 报价单覆盖 ===`); console.log(`有生效报价但订单价=0(录单未取到价): ${hasQuote.length} 个客户-SKU`); console.log(`无生效报价单(系统里根本没报价): ${noQuote.length} 个客户-SKU`); if (hasQuote.length) { console.log('\n--- 有报价单但订单价=0(应回填)---'); for (const r of hasQuote) { console.log(` ${r.customer_code} | ${r.sku_name||''} (${r.sku_code}) | 报价 ¥${r.max_quote_price}`); } } if (noQuote.length) { console.log('\n--- 无生效报价单(需补报价)---'); for (const r of noQuote) { console.log(` ${r.customer_code} | ${r.sku_name||''} (${r.sku_code})`); } } // 3) 创建时间分布(看是否集中在某次导入) const timeline = await pool.query( `SELECT to_char(date_trunc('day', o.created_at),'YYYY-MM-DD') AS create_day, COUNT(DISTINCT o.order_code)::int AS order_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 1 ORDER BY 1` ); console.log(`\n=== 零价订单创建日分布 ===`); for (const r of timeline.rows) console.log(` ${r.create_day}: ${r.order_cnt} 单`); // 4) 录单人分布 const byUser = await pool.query( `SELECT COALESCE(o.created_by,'(null)') AS created_by, COUNT(DISTINCT o.order_code)::int AS order_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 1 ORDER BY order_cnt DESC` ); console.log(`\n=== 零价订单录单人分布 ===`); for (const r of byUser.rows) console.log(` ${r.created_by}: ${r.order_cnt} 单`); }); await pool.end(); } main().catch((e) => { console.error(e); process.exit(1); });