Files
SupplierSystem/server/check-pig-ear-quote2.ts
Ubuntu 1d47267a8d
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore(sync): 提交剩余本地改动(规则文档 / 部署配置 / 小程序入库归档 / 工具与测试)
- .cursor/rules + AGENTS.md:供应商二次开发的隔离硬规则落库(库/端口/上传目录/对象前缀)
- 部署与本地配置:docker-compose*.yml、.env.*.example(仅占位值)、start-split-native / sync-prod-db-to-local 脚本
- 小程序:入库归档识别(stockInboundArchive + 识别与入库面板改动)
- tools:设计门禁脚本(check-migrations / run-all / verify-facts)与架构图脚本更新
- docs:跨系统改价改量审核、第三方订单退货跟随、免入库推进、500+ 分公司规模改造、
  拍照入库识别与档案回写等设计文档;对接清单新增 §4.5.4 我方回执(第六批端到端)
- server:存量修数脚本(legacy CS / 供应商应付 / 结算口径)、采购耳标核对脚本;
  新增测试:入库归档回写、租户优选顺序、规模并发
2026-10-09 22:28:15 +08:00

140 lines
5.1 KiB
TypeScript

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() {
await pool.query(`SET search_path TO tenant_br002, public`);
// broader name search
const names = await pool.query(
`SELECT DISTINCT item_name, sku_code, purchase_price, prepare_status, order_code, created_at
FROM supplier_preparations
WHERE item_name ILIKE '%耳%' OR item_name ILIKE '%猪%'
ORDER BY created_at DESC NULLS LAST
LIMIT 40`
);
console.log('prep by name', names.rows);
const oi = await pool.query(
`SELECT oi.order_code, oi.sku_code, oi.item_name, oi.price, oi.quantity, oi.unit,
o.customer_code, o.branch_code, o.created_at
FROM order_items oi
JOIN orders o ON o.order_code = oi.order_code
WHERE oi.item_name ILIKE '%耳%' OR oi.sku_code = 'SKU78664119567'
ORDER BY o.created_at DESC NULLS LAST
LIMIT 30`
);
console.log('order items', oi.rows);
// recent zero-price preps for this sku across any name
const zero = await pool.query(
`SELECT sp.id, sp.order_code, sp.sku_code, sp.item_name, sp.purchase_price,
sp.branch_code, o.customer_code, o.branch_code AS order_branch, sp.created_at
FROM supplier_preparations sp
LEFT JOIN orders o ON o.order_code = sp.order_code
WHERE sp.sku_code = 'SKU78664119567'
ORDER BY sp.created_at DESC
LIMIT 20`
);
console.log('prep by sku', zero.rows);
// Test resolve for a customer on the quote
const customer = 'CUS34931115429';
const branch = 'BR002';
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`,
[customer, 'SKU78664119567', branch]
);
console.log('resolve hit BR002', hit.rows);
// What if branch is wrong / null?
for (const b of [null, 'BR001', 'BR002', '']) {
const r = await pool.query(
`SELECT COALESCE(i.cost_price, 0)::float AS cost_price
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
LIMIT 1`,
[customer, 'SKU78664119567', b === '' ? null : b]
);
console.log('branch=', b, '->', r.rows[0] || 'MISS');
}
// customers NOT on this quote who ordered this sku
const missCust = await pool.query(
`SELECT DISTINCT o.customer_code, o.branch_code, oi.order_code, oi.item_name, oi.sku_code
FROM order_items oi
JOIN orders o ON o.order_code = oi.order_code
WHERE oi.sku_code = 'SKU78664119567'
AND o.created_at > NOW() - INTERVAL '14 days'
LIMIT 30`
);
console.log('recent orders with sku', missCust.rows);
for (const row of missCust.rows) {
const r = await pool.query(
`SELECT COALESCE(i.cost_price, 0)::float AS cost_price
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)
LIMIT 1`,
[row.customer_code, row.sku_code, row.branch_code]
);
console.log(
'order',
row.order_code,
'cust',
row.customer_code,
'hit',
r.rows[0] || 'MISS'
);
}
await pool.end();
}
main().catch((e) => {
console.error(e);
process.exit(1);
});