来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
138 lines
5.6 KiB
JavaScript
138 lines
5.6 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* 排查荣县客户今日订单中「番茄」与「番茄酱/番茄沙司」映射问题
|
|
*/
|
|
process.chdir('/opt/ai-fresh-delivery/server');
|
|
require('./dist/env.js').loadEnv();
|
|
const { pool } = require('./dist/db.js');
|
|
|
|
const TODAY = process.env.CHINA_DATE || new Date(Date.now() + 8 * 3600 * 1000).toISOString().slice(0, 10);
|
|
|
|
(async () => {
|
|
console.log(`\n=== 荣县 · 番茄/番茄酱 排查 (${TODAY}) ===\n`);
|
|
|
|
const customers = await pool.query(
|
|
`SELECT customer_code, company_name, branch_code, status
|
|
FROM customers
|
|
WHERE company_name ILIKE '%荣县%' OR customer_code ILIKE '%荣县%'
|
|
ORDER BY company_name`
|
|
);
|
|
console.log(`匹配客户 (${customers.rowCount}):`);
|
|
for (const c of customers.rows) {
|
|
console.log(` ${c.customer_code} | ${c.company_name} | branch=${c.branch_code} | status=${c.status}`);
|
|
}
|
|
|
|
const customerCodes = customers.rows.map((r) => r.customer_code);
|
|
if (!customerCodes.length) {
|
|
console.log('\n未找到名称含「荣县」的客户,尝试按分公司/地址搜索...');
|
|
const branches = await pool.query(
|
|
`SELECT branch_code, branch_name FROM branches WHERE branch_name ILIKE '%荣县%' OR branch_code ILIKE '%荣县%'`
|
|
);
|
|
console.log('分公司:', branches.rows);
|
|
|
|
const byAddr = await pool.query(
|
|
`SELECT customer_code, company_name, branch_code FROM customers
|
|
WHERE address ILIKE '%荣县%' OR company_name ILIKE '%荣%'
|
|
ORDER BY company_name LIMIT 20`
|
|
);
|
|
console.log('相关客户:', byAddr.rows);
|
|
await pool.end();
|
|
return;
|
|
}
|
|
|
|
for (const code of customerCodes) {
|
|
const cust = customers.rows.find((r) => r.customer_code === code);
|
|
console.log(`\n--- 客户 ${cust.company_name} (${code}) ---`);
|
|
|
|
const orders = await pool.query(
|
|
`SELECT order_code, order_status, delivery_date, total_amount, created_at::text
|
|
FROM orders
|
|
WHERE customer_code = $1
|
|
AND (delivery_date::date = $2::date OR created_at::date = $2::date)
|
|
ORDER BY created_at DESC`,
|
|
[code, TODAY]
|
|
);
|
|
console.log(`今日订单: ${orders.rowCount}`);
|
|
for (const o of orders.rows) {
|
|
console.log(` ${o.order_code} status=${o.order_status} delivery=${String(o.delivery_date).slice(0, 10)} amount=${o.total_amount}`);
|
|
}
|
|
|
|
const orderCodes = orders.rows.map((r) => r.order_code);
|
|
if (orderCodes.length) {
|
|
const items = await pool.query(
|
|
`SELECT oi.order_code, oi.id, oi.item_name, oi.sku_code, s.sku_name, oi.quantity, oi.unit,
|
|
oi.category_code, oi.category_name, oi.supplier_code
|
|
FROM order_items oi
|
|
LEFT JOIN skus s ON s.sku_code = oi.sku_code
|
|
WHERE oi.order_code = ANY($1::text[])
|
|
AND (oi.item_name ILIKE '%番茄%' OR s.sku_name ILIKE '%番茄%')
|
|
ORDER BY oi.order_code, oi.item_name`,
|
|
[orderCodes]
|
|
);
|
|
console.log(`\n番茄相关明细 (${items.rowCount}):`);
|
|
for (const it of items.rows) {
|
|
const likelyWrong =
|
|
(it.item_name?.includes('番茄') && !it.item_name?.includes('酱') && it.sku_name?.includes('酱')) ||
|
|
(it.item_name?.includes('酱') && it.sku_name && !it.sku_name.includes('酱'));
|
|
console.log(
|
|
` ${likelyWrong ? '⚠ ' : ' '}${it.order_code} | 录单名="${it.item_name}" | SKU=${it.sku_code} | 商品="${it.sku_name}" | ${it.quantity}${it.unit} | 分类=${it.category_name || it.category_code || '-'}`
|
|
);
|
|
}
|
|
}
|
|
|
|
console.log('\n客户映射库 (customer_skus):');
|
|
const mappings = await pool.query(
|
|
`SELECT cs.customer_sku_name, cs.sku_code, s.sku_name, cs.category_code, cs.usage_count
|
|
FROM customer_skus cs
|
|
LEFT JOIN skus s ON s.sku_code = cs.sku_code
|
|
WHERE cs.customer_code = $1
|
|
AND (cs.customer_sku_name ILIKE '%番茄%' OR s.sku_name ILIKE '%番茄%')
|
|
ORDER BY cs.customer_sku_name`,
|
|
[code]
|
|
);
|
|
for (const m of mappings.rows) {
|
|
const wrong =
|
|
(m.customer_sku_name?.includes('番茄') && !m.customer_sku_name?.includes('酱') && m.sku_name?.includes('酱')) ||
|
|
(m.customer_sku_name?.includes('酱') && m.sku_name && !m.sku_name.includes('酱'));
|
|
console.log(
|
|
` ${wrong ? '⚠ ' : ' '}"${m.customer_sku_name}" -> ${m.sku_code} "${m.sku_name}" (usage=${m.usage_count})`
|
|
);
|
|
}
|
|
|
|
console.log('\n文字别名 (customer_sku_aliases):');
|
|
const aliases = await pool.query(
|
|
`SELECT csa.alias_name, csa.sku_code, s.sku_name, csa.usage_count, csa.status
|
|
FROM customer_sku_aliases csa
|
|
LEFT JOIN skus s ON s.sku_code = csa.sku_code
|
|
WHERE csa.customer_code = $1
|
|
AND (csa.alias_name ILIKE '%番茄%' OR s.sku_name ILIKE '%番茄%')
|
|
ORDER BY csa.alias_name`,
|
|
[code]
|
|
);
|
|
for (const a of aliases.rows) {
|
|
const wrong =
|
|
(a.alias_name?.includes('番茄') && !a.alias_name?.includes('酱') && a.sku_name?.includes('酱')) ||
|
|
(a.alias_name?.includes('酱') && a.sku_name && !a.sku_name.includes('酱'));
|
|
console.log(
|
|
` ${wrong ? '⚠ ' : ' '}"${a.alias_name}" -> ${a.sku_code} "${a.sku_name}" (usage=${a.usage_count}, status=${a.status})`
|
|
);
|
|
}
|
|
}
|
|
|
|
console.log('\n全局 SKU 含「番茄/西红柿/杭茄/酱」:');
|
|
const skus = await pool.query(
|
|
`SELECT sku_code, sku_name, unit, category_code FROM skus
|
|
WHERE status = 1 AND (
|
|
sku_name ILIKE '%番茄%' OR sku_name ILIKE '%西红柿%' OR sku_name ILIKE '%杭茄%'
|
|
) ORDER BY sku_name`
|
|
);
|
|
for (const s of skus.rows) {
|
|
console.log(` ${s.sku_code} | ${s.sku_name} | ${s.unit} | cat=${s.category_code}`);
|
|
}
|
|
|
|
await pool.end();
|
|
})().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|