来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
67 lines
3.6 KiB
TypeScript
67 lines
3.6 KiB
TypeScript
import { pool } from './src/db';
|
|
import { runWithTenantDataAccess } from './src/services/tenantSchemaService';
|
|
|
|
async function main() {
|
|
const branch = process.argv[2] || 'BR002';
|
|
await runWithTenantDataAccess(branch, async () => {
|
|
// 零价订单的创建时间 vs 报价生效时间:是否录单时报价还没生效?
|
|
const timing = await pool.query(
|
|
`SELECT o.order_code, o.customer_code, oi.sku_code, oi.unit AS item_unit,
|
|
o.created_at AS order_created,
|
|
(SELECT MIN(q.effective_at) FROM customer_quote_versions q
|
|
WHERE q.customer_code=o.customer_code AND q.sku_code=oi.sku_code AND q.status=1
|
|
) AS first_quote_effective,
|
|
(SELECT MAX(q.effective_at) FROM customer_quote_versions q
|
|
WHERE q.customer_code=o.customer_code AND q.sku_code=oi.sku_code AND q.status=1
|
|
) AS latest_quote_effective
|
|
FROM order_items oi
|
|
JOIN orders o ON o.order_code=oi.order_code AND o.deleted_at IS NULL
|
|
WHERE o.order_status >= 90
|
|
AND COALESCE(NULLIF(oi.locked_price,0), NULLIF(oi.price,0),
|
|
CASE WHEN COALESCE(oi.quantity,0)>0 THEN COALESCE(oi.amount,0)/oi.quantity ELSE 0 END, 0) = 0
|
|
ORDER BY o.created_at`
|
|
);
|
|
console.log('=== 零价明细:录单时间 vs 报价生效时间 ===');
|
|
for (const r of timing.rows) {
|
|
const oc = r.order_created ? new Date(r.order_created).toISOString().slice(0,16) : '-';
|
|
const fq = r.first_quote_effective ? new Date(r.first_quote_effective).toISOString().slice(0,16) : '(无)';
|
|
const lq = r.latest_quote_effective ? new Date(r.latest_quote_effective).toISOString().slice(0,16) : '(无)';
|
|
const quoteAfterOrder = r.first_quote_effective && new Date(r.first_quote_effective) > new Date(r.order_created);
|
|
console.log(` ${r.order_code.slice(-8)} | ${r.sku_code.slice(-6)} unit=${r.item_unit} | 录单 ${oc} | 报价生效 ${fq}~${lq} ${quoteAfterOrder?'⚠️报价晚于录单':''}`);
|
|
}
|
|
|
|
// 单位不匹配统计:明细unit vs 报价unit
|
|
const unitMiss = await pool.query(
|
|
`SELECT oi.unit AS item_unit, COUNT(*)::int AS cnt
|
|
FROM order_items oi
|
|
JOIN orders o ON o.order_code=oi.order_code AND o.deleted_at IS NULL
|
|
WHERE o.order_status >= 90
|
|
AND COALESCE(NULLIF(oi.locked_price,0), NULLIF(oi.price,0),
|
|
CASE WHEN COALESCE(oi.quantity,0)>0 THEN COALESCE(oi.amount,0)/oi.quantity ELSE 0 END, 0) = 0
|
|
GROUP BY oi.unit ORDER BY cnt DESC`
|
|
);
|
|
console.log('\n=== 零价明细的单位分布 ===');
|
|
for (const r of unitMiss.rows) console.log(` unit='${r.item_unit}': ${r.cnt} 条`);
|
|
|
|
// 报价表的单位分布(这些客户-SKU)
|
|
const quoteUnits = await pool.query(
|
|
`SELECT DISTINCT oi.unit AS item_unit, q.unit AS quote_unit, COUNT(*)::int AS cnt
|
|
FROM order_items oi
|
|
JOIN orders o ON o.order_code=oi.order_code AND o.deleted_at IS NULL
|
|
LEFT JOIN customer_quote_versions q
|
|
ON q.customer_code=o.customer_code AND q.sku_code=oi.sku_code AND q.status=1
|
|
WHERE o.order_status >= 90
|
|
AND COALESCE(NULLIF(oi.locked_price,0), NULLIF(oi.price,0),
|
|
CASE WHEN COALESCE(oi.quantity,0)>0 THEN COALESCE(oi.amount,0)/oi.quantity ELSE 0 END, 0) = 0
|
|
GROUP BY oi.unit, q.unit ORDER BY cnt DESC LIMIT 20`
|
|
);
|
|
console.log('\n=== 明细单位 vs 报价单位 ===');
|
|
for (const r of quoteUnits.rows) {
|
|
const qu = r.quote_unit == null ? '(无报价行)' : r.quote_unit;
|
|
console.log(` 明细unit='${r.item_unit}' 报价unit='${qu}': ${r.cnt} 条`);
|
|
}
|
|
});
|
|
await pool.end();
|
|
}
|
|
main().catch((e) => { console.error(e); process.exit(1); });
|