#!/usr/bin/env node const fs = require('fs'); const pg = require('pg'); const env = fs.readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8'); const url = env.match(/^DATABASE_URL=(.+)$/m)[1].trim().replace(/^["']|["']$/g, ''); const pool = new pg.Pool({ connectionString: url }); (async () => { const r = await pool.query(` SELECT id, order_code, sku_code, supplier_code, source_type, inspection_time IS NOT NULL AS done, inbound_batch_no, compliance_status, processing_details->>'purchase_code' AS purchase_code FROM inspections WHERE order_code IN ('ORD1784019177126', 'ORD1784019319794') AND sku_code IN ('SKU78668735222', 'SKU78665585978', 'SKU78666228313') ORDER BY created_at `); console.log(JSON.stringify(r.rows, null, 2)); // Simulate hasSuccessfulInboundInspection for purchase (supplier_code null) for (const row of [ { order: 'ORD1784019177126', sku: 'SKU78668735222', pc: 'PUR1784107301297477d847' }, { order: 'ORD1784019177126', sku: 'SKU78665585978', pc: 'PUR1784107301334779b173' }, { order: 'ORD1784019319794', sku: 'SKU78666228313', pc: 'PUR1784107300948e412163' }, ]) { const hit = await pool.query( `SELECT id, source_type, supplier_code FROM inspections WHERE order_code IS NOT DISTINCT FROM $1 AND COALESCE(supplier_code, '') = COALESCE($3, '') AND sku_code IS NOT DISTINCT FROM $2 AND inspection_time IS NOT NULL AND NULLIF(TRIM(inbound_batch_no), '') IS NOT NULL AND COALESCE(quality_rating, '') NOT IN ('D', '不合格') AND COALESCE(compliance_status, 0) <> 30 AND ( $4 = '' OR NULLIF(TRIM(processing_details->>'purchase_code'), '') IS NOT DISTINCT FROM NULLIF($4, '') OR NULLIF(TRIM(processing_details->>'purchase_code'), '') IS NULL ) LIMIT 1`, [row.order, row.sku, null, row.pc] ); console.log('wouldMatchSuccess', row.pc, hit.rows); } await pool.end(); })().catch((e) => { console.error(e); process.exit(1); });