const { Pool } = require('pg'); const fs = require('fs'); (async () => { const env = fs.readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8'); const m = env.match(/^DATABASE_URL=(.+)$/m); const pool = new Pool({ connectionString: m[1].trim().replace(/^["']|["']$/g, ''), }); await pool.query(`SELECT set_config('search_path', 'tenant_br002, public', false)`); const orderCode = 'ORD_MP_1785227388534RMSP'; const cols = await pool.query( `SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'purchase_orders' ORDER BY 1` ); console.log( 'PO_COLS', cols.rows.map((r) => r.column_name) ); const pos = await pool.query( `SELECT * FROM purchase_orders WHERE order_code = $1 ORDER BY created_at`, [orderCode] ); console.log( 'POS', pos.rows.map((r) => ({ purchase_code: r.purchase_code, sku_code: r.sku_code, purchase_status: r.purchase_status, purchase_qty: r.purchase_qty, purchase_price: r.purchase_price, created_at: r.created_at, remark: r.remark, })) ); const insps = await pool.query( `SELECT id, sku_code, item_name, supplier_code, prepare_qty, purchase_price, source_type, inspection_time, inbound_batch_no, compliance_status, created_at FROM inspections WHERE order_code = $1 ORDER BY created_at`, [orderCode] ); console.log( 'INSPS', insps.rows.map((r) => ({ item: r.item_name, sku: r.sku_code, supplier: r.supplier_code, source: r.source_type, qty: r.prepare_qty, price: r.purchase_price, insp_time: r.inspection_time, batch: r.inbound_batch_no, status: r.compliance_status, created: r.created_at, })) ); // focus on the 4 pending names const names = ['丝瓜', '青条椒', '小葱', '香菜']; for (const name of names) { const oi = await pool.query( `SELECT id, sku_code, supplier_code, quantity, stock_cover_qty FROM order_items WHERE order_code = $1 AND item_name = $2`, [orderCode, name] ); const po = await pool.query( `SELECT purchase_code, sku_code, purchase_status, created_at, submitted_at, reviewed_at FROM purchase_orders WHERE order_code = $1 AND sku_code = $2`, [orderCode, oi.rows[0]?.sku_code] ); const sp = await pool.query( `SELECT id, prepare_status, prepare_time FROM supplier_preparations WHERE order_code = $1 AND sku_code = $2`, [orderCode, oi.rows[0]?.sku_code] ); console.log( 'LINE', name, JSON.stringify({ item: oi.rows[0], purchase: po.rows, prep: sp.rows, }) ); } await pool.end(); })().catch((e) => { console.error(e); process.exit(1); });