const { Pool } = require('pg'); const fs = require('fs'); async function loadDbUrl() { const env = fs.readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8'); const m = env.match(/^DATABASE_URL=(.+)$/m); if (!m) throw new Error('no DATABASE_URL'); return m[1].trim().replace(/^["']|["']$/g, ''); } (async () => { const pool = new Pool({ connectionString: await loadDbUrl() }); await pool.query(`SELECT set_config('search_path', 'tenant_br002, public', false)`); const skus = await pool.query( `SELECT sku_code, sku_name, cost_price, sale_price FROM skus WHERE sku_name ILIKE '%猪耳%' LIMIT 20` ); console.log('SKUS', JSON.stringify(skus.rows, null, 2)); const codes = skus.rows.map((r) => r.sku_code); if (!codes.length) { await pool.end(); return; } const quotes = await pool.query( `SELECT qs.quote_sheet_code, qs.branch_code, qs.approval_status, qs.status, qs.expires_at, i.sku_code, i.cost_price, i.sale_price, i.price, qsc.customer_code FROM customer_quote_sheet_items i JOIN customer_quote_sheets qs ON qs.quote_sheet_code = i.quote_sheet_code LEFT JOIN customer_quote_sheet_customers qsc ON qsc.quote_sheet_code = qs.quote_sheet_code WHERE i.sku_code = ANY($1) ORDER BY qs.updated_at DESC NULLS LAST LIMIT 40`, [codes] ); console.log('QUOTES_COUNT', quotes.rows.length); console.log('QUOTE_SAMPLE', JSON.stringify(quotes.rows.slice(0, 5), null, 2)); const preps = await pool.query( `SELECT sp.id, sp.order_code, sp.sku_code, sp.item_name, sp.purchase_price, sp.prepare_status, sp.branch_code, sp.created_at, o.customer_code, o.branch_code AS order_branch FROM supplier_preparations sp LEFT JOIN orders o ON o.order_code = sp.order_code WHERE sp.sku_code = ANY($1) OR sp.item_name ILIKE '%猪耳%' ORDER BY sp.created_at DESC NULLS LAST LIMIT 30`, [codes] ); console.log('PREPS', JSON.stringify(preps.rows, null, 2)); for (const p of preps.rows.slice(0, 20)) { const customerCode = String(p.customer_code || '').trim(); const branchCode = String(p.order_branch || p.branch_code || '').trim() || null; if (!customerCode || !p.sku_code) { console.log('SKIP_NO_CUST', p.order_code, p.item_name, p.purchase_price); continue; } const hit = await pool.query( `SELECT COALESCE(i.cost_price, 0)::float AS cost_price, qs.quote_sheet_code 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`, [customerCode, p.sku_code, branchCode] ); console.log( 'RESOLVE', JSON.stringify({ order: p.order_code, customer: customerCode, branch: branchCode, sku: p.sku_code, item: p.item_name, prep_price: p.purchase_price, hit: hit.rows[0] || null, }) ); } await pool.end(); })().catch((e) => { console.error(e); process.exit(1); });