来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
124 lines
4.2 KiB
JavaScript
124 lines
4.2 KiB
JavaScript
/**
|
|
* 抽查指定订单:自采 vs 规则解析 vs 履约
|
|
* 用法: node --env-file=.env /tmp/prod-diag-order-allocation-detail.cjs --order=ORDxxx
|
|
*/
|
|
const path = require('path');
|
|
const serverRoot = process.env.SERVER_ROOT || process.cwd();
|
|
process.chdir(serverRoot);
|
|
const { pool } = require(path.join(serverRoot, 'dist/db'));
|
|
const { resolveItemAllocation, DEFAULT_ALLOCATION_RULES } = require(
|
|
path.join(serverRoot, 'dist/services/allocationRules')
|
|
);
|
|
const { isOrderItemFulfillmentValid } = require(
|
|
path.join(serverRoot, 'dist/services/orderItemFulfillmentService')
|
|
);
|
|
const { fetchCategoryRows } = require(path.join(serverRoot, 'dist/services/categoryCatalogService'));
|
|
|
|
function parseArg(name, fallback) {
|
|
const hit = process.argv.find((a) => a.startsWith(`--${name}=`));
|
|
return hit ? hit.slice(name.length + 3) : fallback;
|
|
}
|
|
|
|
async function main() {
|
|
const orderCode = parseArg('order', 'ORD_MP_1784184988527TF2W');
|
|
const order = (
|
|
await pool.query(
|
|
`SELECT order_code, order_status, branch_code, customer_code FROM orders WHERE order_code = $1`,
|
|
[orderCode]
|
|
)
|
|
).rows[0];
|
|
if (!order) throw new Error('order not found');
|
|
console.log('order', order);
|
|
|
|
const items = (
|
|
await pool.query(
|
|
`SELECT oi.id, oi.sku_code, oi.item_name, oi.category_code, oi.category_name,
|
|
oi.supplier_code, oi.quantity, oi.price, oi.unit,
|
|
s.category_code AS sku_category
|
|
FROM order_items oi
|
|
LEFT JOIN skus s ON s.sku_code = oi.sku_code
|
|
WHERE oi.order_code = $1
|
|
ORDER BY oi.sort_order NULLS LAST, oi.id`,
|
|
[orderCode]
|
|
)
|
|
).rows;
|
|
|
|
const [suppliers, supplierCategories, skuSuppliers, categoryRows] = await Promise.all([
|
|
pool.query(`SELECT supplier_code, company_name, rating, status FROM suppliers WHERE status IN (10,20,30,40)`),
|
|
pool.query(`SELECT supplier_code, category_code, is_primary FROM supplier_categories`),
|
|
pool.query(
|
|
`SELECT sku_code, supplier_code, supply_price, is_preferred, in_stock, status, quality_rating
|
|
FROM sku_suppliers WHERE status = 1`
|
|
),
|
|
fetchCategoryRows(order.branch_code),
|
|
]);
|
|
|
|
const pos = (
|
|
await pool.query(
|
|
`SELECT purchase_code, source_id, sku_code, purchase_status, source_type, remark
|
|
FROM purchase_orders WHERE order_code = $1 ORDER BY created_at`,
|
|
[orderCode]
|
|
)
|
|
).rows;
|
|
console.log('purchase_orders', pos.length);
|
|
console.table(
|
|
pos.map((p) => ({
|
|
purchase_code: p.purchase_code,
|
|
source_id: p.source_id,
|
|
sku: p.sku_code,
|
|
status: p.purchase_status,
|
|
source_type: p.source_type,
|
|
}))
|
|
);
|
|
|
|
for (const item of items) {
|
|
const categoryCode = item.category_code || item.sku_category || '';
|
|
const resolved = resolveItemAllocation({
|
|
orderCode,
|
|
item: {
|
|
id: String(item.id),
|
|
sku_code: String(item.sku_code || ''),
|
|
sku_name: String(item.item_name || item.sku_code || ''),
|
|
category_code: categoryCode || undefined,
|
|
category_name: item.category_name || undefined,
|
|
quantity: Number(item.quantity) || 0,
|
|
price: Number(item.price) || 0,
|
|
unit: String(item.unit || '斤'),
|
|
},
|
|
rules: DEFAULT_ALLOCATION_RULES,
|
|
supplierCategories: supplierCategories.rows,
|
|
suppliers: suppliers.rows,
|
|
skuSuppliers: skuSuppliers.rows,
|
|
categoryRows,
|
|
});
|
|
const fulfilled = await isOrderItemFulfillmentValid(pool, String(item.id));
|
|
const openPo = pos.find((p) => String(p.source_id) === String(item.id) && [10, 32].includes(Number(p.purchase_status)));
|
|
console.log(
|
|
JSON.stringify({
|
|
id: String(item.id).slice(0, 8),
|
|
sku: item.sku_code,
|
|
name: item.item_name,
|
|
category: categoryCode,
|
|
item_supplier: item.supplier_code,
|
|
fulfilled,
|
|
openPo: openPo ? openPo.purchase_code : null,
|
|
openPoStatus: openPo ? openPo.purchase_status : null,
|
|
resolve_target: resolved?.target,
|
|
resolve_source: resolved?.allocation_source,
|
|
resolve_supplier: resolved?.supplier_code,
|
|
resolve_reason: resolved?.reason,
|
|
})
|
|
);
|
|
}
|
|
|
|
await pool.end();
|
|
}
|
|
|
|
main().catch(async (e) => {
|
|
console.error(e);
|
|
try {
|
|
await pool.end();
|
|
} catch (_) {}
|
|
process.exit(1);
|
|
});
|