来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
161 lines
4.9 KiB
JavaScript
161 lines
4.9 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 生产机回冲入口:从 dist 加载服务(无 tsx/src)
|
||
* 用法:
|
||
* node scripts/prod-backfill-delivery-outbound.mjs --branch=BR002
|
||
* node scripts/prod-backfill-delivery-outbound.mjs --branch=BR002 --execute
|
||
*/
|
||
import path from 'path';
|
||
import { fileURLToPath, pathToFileURL } from 'url';
|
||
import { createRequire } from 'module';
|
||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||
const serverRoot = path.resolve(__dirname, '..');
|
||
const require = createRequire(path.join(serverRoot, 'package.json'));
|
||
|
||
// 加载 .env → process.env(与 dist/config 一致)
|
||
try {
|
||
const dotenv = require('dotenv');
|
||
dotenv.config({ path: path.join(serverRoot, '.env') });
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
|
||
function parseArgs(argv) {
|
||
const opts = {
|
||
execute: false,
|
||
branch: undefined,
|
||
order: undefined,
|
||
since: undefined,
|
||
limit: 500,
|
||
help: false,
|
||
};
|
||
for (const arg of argv) {
|
||
if (arg === '--execute') opts.execute = true;
|
||
else if (arg === '--help' || arg === '-h') opts.help = true;
|
||
else if (arg.startsWith('--branch=')) opts.branch = arg.slice(9).trim();
|
||
else if (arg.startsWith('--order=')) opts.order = arg.slice(8).trim();
|
||
else if (arg.startsWith('--since=')) opts.since = arg.slice(8).trim();
|
||
else if (arg.startsWith('--limit=')) opts.limit = Math.max(1, Number(arg.slice(8)) || 500);
|
||
}
|
||
return opts;
|
||
}
|
||
|
||
const opts = parseArgs(process.argv.slice(2));
|
||
if (opts.help) {
|
||
console.log(`用法: node scripts/prod-backfill-delivery-outbound.mjs [--branch=BR002] [--since=YYYY-MM-DD] [--order=..] [--limit=N] [--execute]`);
|
||
process.exit(0);
|
||
}
|
||
|
||
const distServices = path.join(serverRoot, 'dist', 'services');
|
||
const outboundUrl = pathToFileURL(path.join(distServices, 'inventoryOutboundService.js')).href;
|
||
const tenantUrl = pathToFileURL(path.join(distServices, 'tenantSchemaFallback.js')).href;
|
||
const dbUrl = pathToFileURL(path.join(serverRoot, 'dist', 'db.js')).href;
|
||
|
||
const {
|
||
backfillDeliveryLoadingOutboundForOrder,
|
||
listOrdersMissingDeliveryOutbound,
|
||
} = await import(outboundUrl);
|
||
const { listActiveTenantBranches, runInBranchSchema } = await import(tenantUrl);
|
||
const { pool } = await import(dbUrl);
|
||
|
||
const dryRun = !opts.execute;
|
||
console.log(
|
||
JSON.stringify(
|
||
{
|
||
mode: dryRun ? 'dry-run' : 'execute',
|
||
branch: opts.branch || null,
|
||
order: opts.order || null,
|
||
since: opts.since || null,
|
||
limit: opts.limit,
|
||
},
|
||
null,
|
||
2
|
||
)
|
||
);
|
||
|
||
async function listCandidates() {
|
||
if (opts.branch) {
|
||
return runInBranchSchema(opts.branch, () =>
|
||
listOrdersMissingDeliveryOutbound({
|
||
branchCode: opts.branch,
|
||
orderCode: opts.order,
|
||
since: opts.since,
|
||
limit: opts.limit,
|
||
})
|
||
);
|
||
}
|
||
if (opts.order) {
|
||
const tenants = await listActiveTenantBranches();
|
||
for (const t of tenants) {
|
||
const rows = await runInBranchSchema(t.branch_code, () =>
|
||
listOrdersMissingDeliveryOutbound({
|
||
orderCode: opts.order,
|
||
since: opts.since,
|
||
limit: opts.limit,
|
||
})
|
||
);
|
||
if (rows.length) return rows;
|
||
}
|
||
return [];
|
||
}
|
||
const tenants = await listActiveTenantBranches();
|
||
if (!tenants.length) {
|
||
return listOrdersMissingDeliveryOutbound({ since: opts.since, limit: opts.limit });
|
||
}
|
||
const all = [];
|
||
for (const t of tenants) {
|
||
if (all.length >= opts.limit) break;
|
||
const rows = await runInBranchSchema(t.branch_code, () =>
|
||
listOrdersMissingDeliveryOutbound({
|
||
branchCode: t.branch_code,
|
||
since: opts.since,
|
||
limit: opts.limit - all.length,
|
||
})
|
||
);
|
||
all.push(...rows);
|
||
}
|
||
return all;
|
||
}
|
||
|
||
const candidates = await listCandidates();
|
||
console.log(`\n候选订单: ${candidates.length}`);
|
||
|
||
const summary = { planned: 0, deducted: 0, skipped: 0, failed: 0 };
|
||
const failures = [];
|
||
|
||
for (const row of candidates) {
|
||
const runner = () =>
|
||
backfillDeliveryLoadingOutboundForOrder(row.order_code, {
|
||
dryRun,
|
||
remark: `配送核货出库回冲 ${row.order_code}`,
|
||
});
|
||
const result = row.branch_code
|
||
? await runInBranchSchema(row.branch_code, runner)
|
||
: await runner();
|
||
const lineCount = result.lines?.length || 0;
|
||
const qtySum = (result.lines || []).reduce((s, l) => s + l.qty, 0);
|
||
console.log(
|
||
[result.order_code, result.branch_code || '-', result.status, result.reason || result.error || '', `lines=${lineCount}`, `qty=${qtySum}`].join(
|
||
'\t'
|
||
)
|
||
);
|
||
if (result.status === 'planned') summary.planned += 1;
|
||
else if (result.status === 'deducted') summary.deducted += 1;
|
||
else if (result.status === 'skipped') summary.skipped += 1;
|
||
else {
|
||
summary.failed += 1;
|
||
failures.push(result);
|
||
}
|
||
}
|
||
|
||
console.log('\n汇总:', summary);
|
||
if (failures.length) {
|
||
console.log('\n失败明细:');
|
||
for (const f of failures) console.log(`- ${f.order_code}: ${f.error || f.reason}`);
|
||
}
|
||
if (dryRun) console.log('\n[dry-run] 未写库。确认后加 --execute');
|
||
|
||
await pool.end();
|
||
process.exit(summary.failed > 0 ? 2 : 0);
|