import pg from 'pg'; // 二次开发版(供应商系统):删除原先硬编码的主系统库连接串(含明文口令 + canteen_delivery)。 // 现在必须显式给 DATABASE_URL,否则拒绝执行,避免误连主系统库。 const databaseUrl = (process.env.DATABASE_URL || '').trim(); if (!databaseUrl) { console.error( '拒绝执行:未设置 DATABASE_URL。\n' + '本脚本不再提供任何兜底连接串(原兜底含明文口令并指向主系统库 canteen_delivery)。\n' + '请显式指定,例如:DATABASE_URL=postgresql://supplier:<密码>@127.0.0.1:5432/supplier_delivery' ); process.exit(2); } const pool = new pg.Pool({ connectionString: databaseUrl }); async function main() { await pool.query(`SET search_path TO tenant_br002, public`); // broader name search const names = await pool.query( `SELECT DISTINCT item_name, sku_code, purchase_price, prepare_status, order_code, created_at FROM supplier_preparations WHERE item_name ILIKE '%耳%' OR item_name ILIKE '%猪%' ORDER BY created_at DESC NULLS LAST LIMIT 40` ); console.log('prep by name', names.rows); const oi = await pool.query( `SELECT oi.order_code, oi.sku_code, oi.item_name, oi.price, oi.quantity, oi.unit, o.customer_code, o.branch_code, o.created_at FROM order_items oi JOIN orders o ON o.order_code = oi.order_code WHERE oi.item_name ILIKE '%耳%' OR oi.sku_code = 'SKU78664119567' ORDER BY o.created_at DESC NULLS LAST LIMIT 30` ); console.log('order items', oi.rows); // recent zero-price preps for this sku across any name const zero = await pool.query( `SELECT sp.id, sp.order_code, sp.sku_code, sp.item_name, sp.purchase_price, sp.branch_code, o.customer_code, o.branch_code AS order_branch, sp.created_at FROM supplier_preparations sp LEFT JOIN orders o ON o.order_code = sp.order_code WHERE sp.sku_code = 'SKU78664119567' ORDER BY sp.created_at DESC LIMIT 20` ); console.log('prep by sku', zero.rows); // Test resolve for a customer on the quote const customer = 'CUS34931115429'; const branch = 'BR002'; const hit = await pool.query( `SELECT COALESCE(i.cost_price, 0)::float AS cost_price, qs.quote_sheet_code, qs.branch_code AS qs_branch FROM customer_quote_sheet_items i INNER JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code INNER JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code WHERE qsc.customer_code = $1 AND i.sku_code = $2 AND qs.approval_status = 30 AND qs.status = 1 AND (qs.expires_at IS NULL OR qs.expires_at > NOW()) AND ($3::text IS NULL OR qs.branch_code IS NOT DISTINCT FROM $3) ORDER BY qs.updated_at DESC NULLS LAST, qs.created_at DESC LIMIT 1`, [customer, 'SKU78664119567', branch] ); console.log('resolve hit BR002', hit.rows); // What if branch is wrong / null? for (const b of [null, 'BR001', 'BR002', '']) { const r = await pool.query( `SELECT COALESCE(i.cost_price, 0)::float AS cost_price FROM customer_quote_sheet_items i INNER JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code INNER JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code WHERE qsc.customer_code = $1 AND i.sku_code = $2 AND qs.approval_status = 30 AND qs.status = 1 AND (qs.expires_at IS NULL OR qs.expires_at > NOW()) AND ($3::text IS NULL OR qs.branch_code IS NOT DISTINCT FROM $3) ORDER BY qs.updated_at DESC NULLS LAST LIMIT 1`, [customer, 'SKU78664119567', b === '' ? null : b] ); console.log('branch=', b, '->', r.rows[0] || 'MISS'); } // customers NOT on this quote who ordered this sku const missCust = await pool.query( `SELECT DISTINCT o.customer_code, o.branch_code, oi.order_code, oi.item_name, oi.sku_code FROM order_items oi JOIN orders o ON o.order_code = oi.order_code WHERE oi.sku_code = 'SKU78664119567' AND o.created_at > NOW() - INTERVAL '14 days' LIMIT 30` ); console.log('recent orders with sku', missCust.rows); for (const row of missCust.rows) { const r = await pool.query( `SELECT COALESCE(i.cost_price, 0)::float AS cost_price FROM customer_quote_sheet_items i INNER JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code INNER JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code WHERE qsc.customer_code = $1 AND i.sku_code = $2 AND qs.approval_status = 30 AND qs.status = 1 AND (qs.expires_at IS NULL OR qs.expires_at > NOW()) AND ($3::text IS NULL OR qs.branch_code IS NOT DISTINCT FROM $3) LIMIT 1`, [row.customer_code, row.sku_code, row.branch_code] ); console.log( 'order', row.order_code, 'cust', row.customer_code, 'hit', r.rows[0] || 'MISS' ); } await pool.end(); } main().catch((e) => { console.error(e); process.exit(1); });