- .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 / 供应商应付 / 结算口径)、采购耳标核对脚本; 新增测试:入库归档回写、租户优选顺序、规模并发
111 lines
4.0 KiB
TypeScript
111 lines
4.0 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() {
|
|
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);
|
|
});
|