import pg from 'pg'; // 二次开发版(供应商系统):删除原先硬编码的主系统库连接串(含明文口令 + canteen_delivery)。 // 现在必须显式给 DATABASE_URL,否则拒绝执行,避免误连主系统库。 const databaseUrl = (process.env.DATABASE_URL || '').trim(); if (!databaseUrl) { console.error( '拒绝执行:未设置 DATABASE_URL。\n' + '本脚本不再提供任何兜底连接串(原兜底含明文口令并指向主系统库 canteen_delivery)。\n' + '请显式指定,例如:DATABASE_URL=postgresql://supplier:<密码>@127.0.0.1:5432/supplier_delivery' ); process.exit(2); } const pool = new pg.Pool({ connectionString: databaseUrl }); async function main() { const schemas = await pool.query( `SELECT nspname FROM pg_namespace WHERE nspname LIKE 'tenant_%' OR nspname = 'legacy_public' ORDER BY 1` ); console.log( 'schemas', schemas.rows.map((r) => r.nspname) ); for (const s of schemas.rows) { const schema = s.nspname as string; await pool.query(`SET search_path TO ${schema}, public`); const skus = await pool.query( `SELECT sku_code, sku_name, cost_price, sale_price FROM skus WHERE sku_name ILIKE '%猪耳%' OR sku_name ILIKE '%耳朵%' LIMIT 20` ); if (!skus.rows.length) continue; console.log('\n===', schema, 'skus ==='); console.log(skus.rows); const codes = skus.rows.map((r) => r.sku_code); const quote = await pool.query( `SELECT qs.quote_sheet_code, qs.branch_code, qs.approval_status, qs.status, qs.expires_at, qs.updated_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 50`, [codes] ); console.log('quotes', quote.rows); const prep = 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', prep.rows); // Simulate resolveQuoteSheetCostPrice for each prep for (const p of prep.rows.slice(0, 10)) { const customerCode = String(p.customer_code || '').trim(); const branchCode = String(p.order_branch || p.branch_code || '').trim() || null; if (!customerCode || !p.sku_code) continue; const hit = await pool.query( `SELECT COALESCE(i.cost_price, 0)::float AS cost_price, qs.quote_sheet_code, qs.branch_code AS qs_branch 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, qs.created_at DESC LIMIT 1`, [customerCode, p.sku_code, branchCode] ); console.log('resolve for', { order: p.order_code, customer: customerCode, branch: branchCode, sku: p.sku_code, prep_price: p.purchase_price, hit: hit.rows[0] || null, }); } } await pool.end(); } main().catch((e) => { console.error(e); process.exit(1); });