#!/usr/bin/env node /** * 定点关单:BR002 9/15 泸州邮政 ORD_WEB_20260915_BR002_0005 * 粗大葱 / 去皮芋儿 采购单停在 20(待验收),订单已 90。 * 只把这两张 purchase_orders 置 30。不写库存、不写 cost_details、不改验收行。 * * 默认 dry-run: * node --env-file=.env scripts/close-stuck-self-purchase-cong-yu.cjs * 本地写入: * node --env-file=.env scripts/close-stuck-self-purchase-cong-yu.cjs --execute --allow-local * 生产(API 机、该机 server/.env): * node --env-file=.env scripts/close-stuck-self-purchase-cong-yu.cjs --execute --prod */ const { Pool } = require('pg'); const BRANCH = 'BR002'; const TARGETS = [ { name: '粗大葱', purchaseCode: 'PUR17894574180286dcc621', orderCode: 'ORD_WEB_20260915_BR002_0005', skuCode: 'SKU86683316915', }, { name: '去皮芋儿', purchaseCode: 'PUR1789457417752a322677', orderCode: 'ORD_WEB_20260915_BR002_0005', skuCode: 'SKU66099324731', }, ]; function parseArgs(argv) { return { execute: argv.includes('--execute'), prod: argv.includes('--prod'), allowLocal: argv.includes('--allow-local'), help: argv.includes('--help') || argv.includes('-h'), }; } function describeUrl(url) { const u = new URL(url); const host = u.hostname || 'unknown'; const database = decodeURIComponent(u.pathname.replace(/^\//, '') || ''); const local = host === '127.0.0.1' || host === 'localhost' || host === '::1'; return { host, database, local }; } async function inspect(client, t) { const po = await client.query( `SELECT purchase_code, purchase_status, purchase_qty, actual_qty, sku_code FROM purchase_orders WHERE purchase_code = $1`, [t.purchaseCode] ); const o = await client.query( `SELECT order_code, order_status FROM orders WHERE order_code = $1`, [t.orderCode] ); return { po: po.rows[0] || null, order: o.rows[0] || null }; } async function main() { const args = parseArgs(process.argv.slice(2)); if (args.help) { console.log('close-stuck-self-purchase-cong-yu.cjs [--execute --allow-local|--prod]'); return; } const url = String(process.env.DATABASE_URL || '').trim(); if (!url) throw new Error('缺少 DATABASE_URL'); const dest = describeUrl(url); console.log(`目标 ${dest.host}/${dest.database} local=${dest.local} execute=${args.execute}`); if (args.execute) { if (dest.local && !args.allowLocal) { throw new Error('当前 DATABASE_URL 是本机库。本地写入请加 --allow-local'); } if (!dest.local && !args.prod) { throw new Error('当前 DATABASE_URL 不是本机。写入生产必须加 --prod'); } if (args.prod && dest.local) { throw new Error('--prod 不能对着本机库'); } } const pool = new Pool({ connectionString: url }); const client = await pool.connect(); try { await client.query(`SELECT set_config('app.branch_code', $1, false)`, [BRANCH]); await client.query(`SET search_path TO tenant_br002, public`); const before = []; for (const t of TARGETS) { const snap = await inspect(client, t); before.push({ ...t, ...snap }); console.log( `[看] ${t.name} ${t.purchaseCode} po=${snap.po?.purchase_status} qty=${snap.po?.actual_qty} order=${snap.order?.order_status}` ); } if (!args.execute) { console.log('dry-run:将把上述 purchase_status=20 的单改为 30'); return; } await client.query('BEGIN'); for (const t of TARGETS) { const r = await client.query( `UPDATE purchase_orders SET purchase_status = 30, updated_at = NOW() WHERE purchase_code = $1 AND sku_code = $2 AND purchase_status = 20 RETURNING purchase_code, purchase_status`, [t.purchaseCode, t.skuCode] ); console.log(`[写] ${t.name} updated=${r.rowCount} → ${r.rows[0]?.purchase_status || '未改(已不是20)'}`); } await client.query('COMMIT'); for (const t of TARGETS) { const snap = await inspect(client, t); console.log(`[后] ${t.name} po=${snap.po?.purchase_status} order=${snap.order?.order_status}`); } } catch (e) { try { await client.query('ROLLBACK'); } catch { /* ignore */ } throw e; } finally { client.release(); await pool.end(); } } main().catch((e) => { console.error(e); process.exit(1); });