后端能力: - 接入店管家订单接口(/NewOrder/List),明细归一化后按店铺商品匹配系统商品/组合商品 - 新增 service/externalPrintOrder:手动与定时拉单共用入库逻辑,同时写入待审核状态分表 - 新增 schedule/fetchExternalShopOrders:按店铺配置的间隔自动拉单(最小 3 分钟,起点重叠 10 分钟) - 新增 lib/orderItemExpand + service/orderItemResolve:平台行与最小单位行互转; 绑定组合商品时按「数量 = 平台数量 x 组合内数量、单价取子商品零售价」拆成子商品行,解绑可还原 - 明细新增 platform_snapshot,保留平台原样(编码/名称/规格/图片/原行号/原数量/原单价) - 店铺商品列表支持 has_unbound_order 筛选,订单列表支持 exclude_unbound(均已同步导出定义) - 店铺商品只读化:移除新增/编辑/删除动作,仅保留查询与绑定; 同步商品时平台已不存在的商品自动清理(未绑定且未被订单引用直接删除,已绑定或被引用仅下架) - 新增 scripts/rebuild-order-items 存量明细重建脚本,新增 orderItemExpand 单测(8 项) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
99 lines
3.3 KiB
JavaScript
99 lines
3.3 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 重建订单明细(平台订单解析口径变更后的存量修复工具)。
|
||
*
|
||
* 背景:平台订单明细需要按店铺商品绑定关系展开为最小单位(组合商品拆成子商品行),
|
||
* 旧代码只把绑定结果写回明细、不做拆分,存量订单会残留组合行。
|
||
*
|
||
* 用法:
|
||
* node scripts/rebuild-order-items.js # 重建全部外部打单平台订单
|
||
* node scripts/rebuild-order-items.js --order 226 # 只重建指定订单(可逗号分隔)
|
||
* 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}} 解析结果
|
||
*/
|
||
function parseArgs(argv) {
|
||
const args = { orderIds: [], dryRun: false };
|
||
for (let i = 0; i < argv.length; i++) {
|
||
const arg = argv[i];
|
||
if (arg === '--dry-run') args.dryRun = 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;
|
||
}
|
||
|
||
(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) {
|
||
// 只处理平台拉取的订单:老表 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);
|
||
});
|