来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
120 lines
3.8 KiB
JavaScript
120 lines
3.8 KiB
JavaScript
/**
|
||
* 生产:对未完成订单批量跑服务端 reprocessOrderAllocation(含「强制进自采/备货」)。
|
||
*
|
||
* 在 API 机执行:
|
||
* cd /opt/ai-fresh-delivery/server && node --env-file=.env /tmp/prod-reprocess-order-allocation.cjs
|
||
* 可选:
|
||
* --status=20,30,40,45,50,60
|
||
* --branch=BR002
|
||
* --limit=200
|
||
* --order=ORDxxx (只跑一单)
|
||
*/
|
||
const path = require('path');
|
||
|
||
// 本文件若在 /tmp,仍从 server 目录加载编译产物
|
||
const serverRoot = process.env.SERVER_ROOT || process.cwd();
|
||
process.chdir(serverRoot);
|
||
|
||
const { pool } = require(path.join(serverRoot, 'dist/db'));
|
||
const { reprocessOrderAllocation } = require(path.join(serverRoot, 'dist/services/orderAllocationService'));
|
||
|
||
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 orderOnly = parseArg('order', '');
|
||
const branch = parseArg('branch', '');
|
||
const limit = Math.min(Math.max(Number(parseArg('limit', '500')) || 500, 1), 2000);
|
||
const statusRaw = parseArg('status', '20,30,40,45,50,60');
|
||
const statuses = statusRaw
|
||
.split(',')
|
||
.map((s) => Number(s.trim()))
|
||
.filter((n) => Number.isFinite(n));
|
||
|
||
let orders;
|
||
if (orderOnly) {
|
||
const r = await pool.query(
|
||
`SELECT order_code, customer_code, branch_code, order_status
|
||
FROM orders WHERE order_code = $1`,
|
||
[orderOnly]
|
||
);
|
||
orders = r.rows;
|
||
} else {
|
||
const params = [statuses];
|
||
let sql = `SELECT order_code, customer_code, branch_code, order_status
|
||
FROM orders
|
||
WHERE order_status = ANY($1::int[])`;
|
||
if (branch) {
|
||
params.push(branch);
|
||
sql += ` AND branch_code = $${params.length}`;
|
||
}
|
||
params.push(limit);
|
||
sql += ` ORDER BY updated_at DESC NULLS LAST, created_at DESC LIMIT $${params.length}`;
|
||
const r = await pool.query(sql, params);
|
||
orders = r.rows;
|
||
}
|
||
|
||
console.log(`待处理订单: ${orders.length}(状态 ${statuses.join(',')})`);
|
||
let ok = 0;
|
||
let po = 0;
|
||
let prep = 0;
|
||
let skipped = 0;
|
||
const errors = [];
|
||
|
||
for (const row of orders) {
|
||
const code = String(row.order_code);
|
||
try {
|
||
const result = await reprocessOrderAllocation(
|
||
code,
|
||
row.customer_code || null,
|
||
row.branch_code || null
|
||
);
|
||
ok += 1;
|
||
po += Number(result.purchaseOrdersCreated) || 0;
|
||
prep += Number(result.preparationsCreated) || 0;
|
||
skipped += Number(result.skipped) || 0;
|
||
if (result.errors?.length) {
|
||
for (const e of result.errors) errors.push(`${code}: ${e}`);
|
||
} else {
|
||
console.log(
|
||
`OK ${code} status=${row.order_status}->${result.orderStatusUpdated ?? row.order_status} po+${result.purchaseOrdersCreated} prep+${result.preparationsCreated}`
|
||
);
|
||
}
|
||
if (result.errors?.length) {
|
||
console.log(
|
||
`WARN ${code} po+${result.purchaseOrdersCreated} prep+${result.preparationsCreated} errors=${result.errors.join(' | ')}`
|
||
);
|
||
}
|
||
} catch (error) {
|
||
const msg = error instanceof Error ? error.message : String(error);
|
||
errors.push(`${code}: ${msg}`);
|
||
console.error(`FAIL ${code}: ${msg}`);
|
||
}
|
||
}
|
||
|
||
console.log('===== summary =====');
|
||
console.log(`orders: ${ok}/${orders.length}`);
|
||
console.log(`purchaseOrdersCreated: ${po}`);
|
||
console.log(`preparationsCreated: ${prep}`);
|
||
console.log(`skipped: ${skipped}`);
|
||
console.log(`errorCount: ${errors.length}`);
|
||
if (errors.length) {
|
||
errors.slice(0, 30).forEach((e) => console.log(` - ${e}`));
|
||
}
|
||
|
||
await pool.end().catch(() => undefined);
|
||
if (errors.length) process.exitCode = 1;
|
||
}
|
||
|
||
main().catch(async (error) => {
|
||
console.error(error);
|
||
try {
|
||
await pool.end();
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
process.exit(1);
|
||
});
|