- 拉单任务:新增 order_sync_tasks 异步任务池,按店铺逐店拉取外部打单平台订单,前端轮询进度 - 外部发货同步:配送后把发货结果同步回店管家,提供数量统计与批量重试(externalSendSync) - 取号:新增 waybill_fetch_records 记录取号过程,外部快递接口补充菜鸟授权信息 - 补齐相关迁移、订单页同步入口/状态展示,并新增对接说明文档 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
147 lines
5.3 KiB
JavaScript
147 lines
5.3 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 重建订单明细(平台订单解析口径变更后的存量修复工具)。
|
||
*
|
||
* 背景:平台订单明细需要按店铺商品绑定关系展开为最小单位(组合商品拆成子商品行),
|
||
* 旧代码只把绑定结果写回明细、不做拆分,存量订单会残留组合行。
|
||
* 另有存量问题:绑定系统商品后明细的 item_code/item_name 仍是平台原值(页面看起来"没转换"),
|
||
* 用 `--stale` 可只挑出这类不一致的订单重建。
|
||
*
|
||
* 用法:
|
||
* node scripts/rebuild-order-items.js # 重建全部外部打单平台订单
|
||
* node scripts/rebuild-order-items.js --order 226 # 只重建指定订单(可逗号分隔)
|
||
* node scripts/rebuild-order-items.js --stale # 只重建「已绑定但编码仍是平台值」的订单
|
||
* node scripts/rebuild-order-items.js --dry-run # 只统计不写库
|
||
*/
|
||
const mm = require('egg-mock');
|
||
|
||
/**
|
||
* 解析命令行参数
|
||
* @param {Array<string>} argv 进程参数(process.argv.slice(2))
|
||
* @return {{orderIds: Array<number>, dryRun: boolean, stale: boolean}} 解析结果
|
||
*/
|
||
function parseArgs(argv) {
|
||
const args = { orderIds: [], dryRun: false, stale: false };
|
||
for (let i = 0; i < argv.length; i++) {
|
||
const arg = argv[i];
|
||
if (arg === '--dry-run') args.dryRun = true;
|
||
else if (arg === '--stale') args.stale = true;
|
||
else if (arg === '--order' || arg === '--orders') {
|
||
const value = argv[i + 1] || '';
|
||
args.orderIds.push(...value.split(',').map(v => Number(v.trim())).filter(v => v > 0));
|
||
i++;
|
||
}
|
||
}
|
||
return args;
|
||
}
|
||
|
||
/**
|
||
* 找出「已绑定系统商品、但明细编码/名称仍是平台原值」的待审核/异常订单
|
||
* (绑定后未重建明细的存量脏数据,推送会被拦截或用错编码)
|
||
* @param {Object} ctx egg context
|
||
* @return {Promise<Array<number>>} 订单ID列表
|
||
*/
|
||
async function findStaleOrderIds(ctx) {
|
||
const Op = ctx.app.Sequelize.Op;
|
||
const rows = await ctx.model.OrderStatusItem.findAll({
|
||
where: {
|
||
product_id: { [Op.ne]: null },
|
||
parse_status: 1,
|
||
},
|
||
attributes: [ 'order_id', 'product_id', 'item_code' ],
|
||
raw: true,
|
||
});
|
||
if (!rows.length) return [];
|
||
|
||
const products = await ctx.model.Product.findAll({
|
||
where: { id: { [Op.in]: [ ...new Set(rows.map(r => Number(r.product_id))) ] } },
|
||
attributes: [ 'id', 'item_code' ],
|
||
raw: true,
|
||
});
|
||
const codeById = new Map(products.map(p => [ Number(p.id), String(p.item_code || '') ]));
|
||
|
||
const staleOrderIds = new Set();
|
||
for (const row of rows) {
|
||
const expected = codeById.get(Number(row.product_id));
|
||
if (expected && expected !== String(row.item_code || '')) staleOrderIds.add(Number(row.order_id));
|
||
}
|
||
if (!staleOrderIds.size) return [];
|
||
|
||
const indexes = await ctx.model.OrderIndex.findAll({
|
||
where: { id: { [Op.in]: [ ...staleOrderIds ] }, current_status: { [Op.in]: [ 'pending', 'exception' ] } },
|
||
attributes: [ 'id' ],
|
||
raw: true,
|
||
});
|
||
return indexes.map(row => Number(row.id));
|
||
}
|
||
|
||
(async () => {
|
||
const args = parseArgs(process.argv.slice(2));
|
||
const app = mm.app();
|
||
await app.ready();
|
||
const ctx = app.mockContext();
|
||
|
||
let orderIds = args.orderIds;
|
||
const Op = app.Sequelize.Op;
|
||
if (!orderIds.length && args.stale) {
|
||
orderIds = await findStaleOrderIds(ctx);
|
||
console.log(`发现明细编码与绑定商品不一致的订单 ${orderIds.length} 个`);
|
||
}
|
||
if (!orderIds.length && !args.stale) {
|
||
// 只处理平台拉取的订单:老表 orders.external_order_id 有值 → 按 order_no 映射到订单索引表
|
||
const legacy = await ctx.model.Order.findAll({
|
||
where: { external_order_id: { [Op.ne]: null } },
|
||
attributes: [ 'order_no' ],
|
||
raw: true,
|
||
});
|
||
const orderNos = [ ...new Set(legacy.map(row => row.order_no).filter(Boolean)) ];
|
||
if (orderNos.length) {
|
||
const indexes = await ctx.model.OrderIndex.findAll({
|
||
where: { order_no: { [Op.in]: orderNos } },
|
||
attributes: [ 'id' ],
|
||
raw: true,
|
||
});
|
||
orderIds = indexes.map(row => Number(row.id));
|
||
}
|
||
}
|
||
|
||
if (!orderIds.length) {
|
||
console.log('没有需要重建的订单');
|
||
await app.close();
|
||
process.exit(0);
|
||
}
|
||
|
||
// 只重建允许维护明细的订单(待审核 / 异常):已推送仓库的订单明细保持历史原样
|
||
const candidates = await ctx.model.OrderIndex.findAll({
|
||
where: { id: { [Op.in]: orderIds }, current_status: { [Op.in]: [ 'pending', 'exception' ] } },
|
||
attributes: [ 'id', 'current_status' ],
|
||
raw: true,
|
||
});
|
||
orderIds = candidates.map(row => Number(row.id));
|
||
|
||
if (!orderIds.length) {
|
||
console.log('没有需要重建的订单(仅待审核 / 异常需要重建)');
|
||
await app.close();
|
||
process.exit(0);
|
||
}
|
||
|
||
if (args.dryRun) {
|
||
console.log(`[dry-run] 待重建订单 ${orderIds.length} 个:${orderIds.slice(0, 20).join(', ')}${orderIds.length > 20 ? ' …' : ''}`);
|
||
await app.close();
|
||
process.exit(0);
|
||
}
|
||
|
||
let affected = 0;
|
||
for (const orderId of orderIds) {
|
||
affected += await ctx.service.orderItemResolve.rebuildOrderItems([ orderId ]);
|
||
}
|
||
console.log(`重建完成:订单 ${orderIds.length} 个,明细行 ${affected} 行`);
|
||
|
||
await app.close();
|
||
process.exit(0);
|
||
})().catch(err => {
|
||
console.error('重建失败:', err);
|
||
process.exit(1);
|
||
});
|