import { pool } from './src/db'; import { runWithTenantDataAccess } from './src/services/tenantSchemaService'; import { resolveSkuPricesForCustomer, resolveSkuPriceForCustomer, normalizeUnitKey } from './src/services/orderPricingService'; async function main() { const branch = process.argv[2] || 'BR002'; await runWithTenantDataAccess(branch, async () => { // 取一个零价样本:CUS34931115429 西瓜 SKU78666937433(报价 ¥2.50) const samples = [ { customer: 'CUS34931115429', sku: 'SKU78666937433', name: '西瓜' }, { customer: 'CUS34952066486', sku: 'SKU78668520975', name: '鲫鱼' }, { customer: 'CUS34964973228', sku: 'SKU78668735222', name: '鸡蛋' }, ]; for (const s of samples) { const batchPrice = await resolveSkuPricesForCustomer(s.customer, [s.sku]); console.log(`\n=== ${s.name} (${s.customer}/${s.sku}) ===`); console.log(`resolveSkuPricesForCustomer -> ${batchPrice.get(s.sku) ?? '(未命中)'}`); // 直接查报价表(不过滤分公司/单位) const raw = await pool.query( `SELECT customer_code, sku_code, price, unit, branch_code, status, approval_status, effective_at, expires_at FROM customer_quote_versions WHERE customer_code=$1 AND sku_code=$2 ORDER BY effective_at DESC NULLS LAST LIMIT 5`, [s.customer, s.sku] ); console.log('customer_quote_versions 原始行:'); for (const r of raw.rows) console.log(' ', r); const cust = await pool.query(`SELECT branch_code FROM customers WHERE customer_code=$1`, [s.customer]); console.log(`客户 branch_code = ${cust.rows[0]?.branch_code}`); } // 顺便看订单明细里的 unit 是什么 const itemUnit = await pool.query( `SELECT oi.order_code, oi.unit, oi.sku_code, o.customer_code FROM order_items oi JOIN orders o ON o.order_code=oi.order_code WHERE o.customer_code='CUS34931115429' AND oi.sku_code='SKU78666937433' ORDER BY oi.created_at DESC LIMIT 3` ); console.log('\n订单明细里的 unit:', itemUnit.rows); }); await pool.end(); } main().catch((e) => { console.error(e); process.exit(1); });