/** * 修复仍停在「待分配(20)」但已实际完成分配/备货的订单。 * 用法(在 server 目录): * node scripts/repair-stuck-allocation-status.cjs * node scripts/repair-stuck-allocation-status.cjs --execute */ require('dotenv').config(); const { Pool } = require('pg'); async function getPending(pool, orderCode) { const preps = await pool.query( `SELECT COUNT(*)::int AS cnt FROM supplier_preparations sp WHERE sp.order_code = $1 AND COALESCE(sp.is_stockout, false) = false AND COALESCE(sp.transferred_to_purchase, false) = false AND NOT EXISTS ( SELECT 1 FROM inspections i WHERE i.order_code = sp.order_code AND i.sku_code IS NOT DISTINCT FROM sp.sku_code AND COALESCE(i.supplier_code, '') = COALESCE(sp.supplier_code, '') )`, [orderCode] ); const purchases = await pool.query( `SELECT COUNT(*)::int AS cnt FROM purchase_orders WHERE order_code = $1 AND COALESCE(purchase_status, 10) IN (10, 32)`, [orderCode] ); return { pendingPreps: Number(preps.rows[0]?.cnt || 0), pendingPurchases: Number(purchases.rows[0]?.cnt || 0), }; } async function advanceOne(pool, orderCode) { const orderRes = await pool.query( `SELECT order_status, branch_code FROM orders WHERE order_code = $1`, [orderCode] ); const order = orderRes.rows[0]; if (!order) return { order_code: orderCode, error: 'not found' }; let status = Number(order.order_status); let changed = false; if (status === 20) { const bump = await pool.query( `UPDATE orders SET order_status = 30, updated_at = NOW() WHERE order_code = $1 AND order_status = 20 RETURNING order_code`, [orderCode] ); if (!bump.rowCount) return { order_code: orderCode, status, note: 'race skip' }; status = 30; changed = true; } if (status !== 30) { return { order_code: orderCode, status, note: 'not 30' }; } const pending = await getPending(pool, orderCode); if (pending.pendingPreps > 0 || pending.pendingPurchases > 0) { return { order_code: orderCode, status, advanced_to: changed ? 30 : status, pending, note: changed ? 'corrected to 待备货' : 'still pending fulfillment', }; } const insp = await pool.query( `SELECT COUNT(*)::int AS cnt FROM inspections WHERE order_code = $1`, [orderCode] ); if (Number(insp.rows[0]?.cnt || 0) === 0) { return { order_code: orderCode, status, advanced_to: changed ? 30 : status, note: changed ? 'corrected to 待备货 (no inspections yet)' : 'no inspections', }; } const to40 = await pool.query( `UPDATE orders SET order_status = 40, updated_at = NOW() WHERE order_code = $1 AND order_status = 30 RETURNING order_code`, [orderCode] ); return { order_code: orderCode, advanced_to: to40.rowCount ? 40 : 30, note: to40.rowCount ? '→ 待验收' : 'stay 待备货', }; } async function main() { const execute = process.argv.includes('--execute'); const pool = new Pool({ connectionString: process.env.DATABASE_URL }); const stuck = await pool.query(` SELECT o.order_code, o.order_status, o.updated_at FROM orders o WHERE o.order_status = 20 AND EXISTS (SELECT 1 FROM order_items oi WHERE oi.order_code = o.order_code) AND NOT EXISTS ( SELECT 1 FROM order_items oi WHERE oi.order_code = o.order_code AND NOT ( ( oi.supplier_code IS NOT NULL AND TRIM(oi.supplier_code) <> '' AND oi.supplier_code NOT IN ('UNASSIGNED', 'INTERNAL') ) OR EXISTS ( SELECT 1 FROM purchase_orders po WHERE po.order_code = oi.order_code AND po.source_id = oi.id::text AND po.purchase_status < 80 ) ) ) ORDER BY o.updated_at DESC NULLS LAST LIMIT 200 `); console.log(`found ${stuck.rows.length} stuck status=20 (allocation fulfilled)`); for (const row of stuck.rows) { console.log(`- ${row.order_code}`); } if (!execute) { console.log('Dry run. Re-run with --execute to apply.'); await pool.end(); return; } const results = []; for (const row of stuck.rows) { results.push(await advanceOne(pool, row.order_code)); } console.log(JSON.stringify(results, null, 2)); await pool.end(); } main().catch((e) => { console.error(e); process.exit(1); });