问题:外部打单平台订单的来源订单号 / 去重键取的是 PlatformOrderId(平台单号), 但平台单号并不唯一(同一平台单可拆成多条逻辑单),唯一标识是店管家的「系统编号」LogicOrderId。 例如 订单编号 261023-105376715090914 的系统编号是 695351624597601813。 - normalizeOrder:external_order_id / order_no 改用 LogicOrderId(缺失时回落平台单号), 平台单号以 platform_order_no 单独带出 - 订单表 extend_props 增加 platform_order_no,便于按店管家「订单编号」对账 - 订单详情抽屉新增「平台订单号」一行(有值才显示) - 新增 scripts/migrate-logic-order-id.js(npm run orders:migrate-logic-id): 把历史订单的来源订单号从平台单号迁移到系统编号,同时改写 orders / order_index / 各状态表 / order_status_items / 配送单,并补写 extend_props.platform_order_no;默认 dry-run - 修正 orders.external_order_id、order_index.parent_order_no、orderFields 的字段注释 已执行迁移 20 条(备份见会话 files/backup-before-logic-order-id-migration.json), 迁移后重新拉单 created=0 / updated=14,确认不会产生重复订单。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
137 lines
5.2 KiB
JavaScript
137 lines
5.2 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 外部打单平台订单:把「来源订单号」从平台单号(PlatformOrderId)迁移到店管家系统编号(LogicOrderId)。
|
||
*
|
||
* 背景:平台单号可能重复(同一平台单会拆成多条逻辑单),只有系统编号全局唯一,
|
||
* 拉单已改为用 LogicOrderId 作为 external_order_id / 来源订单号 / 去重键;
|
||
* 历史数据需要同步改写,否则下次拉单会当成新订单重复入库。
|
||
*
|
||
* 改写范围(按 external_order_id 精确匹配,不影响手工单):
|
||
* - orders.order_no / external_order_id
|
||
* - order_index.order_no / parent_order_no
|
||
* - 各状态表的 order_no / parent_order_no / source_order_codes
|
||
* - order_status_items.source_order_code
|
||
* - order_deliveries / order_delivery_items 的来源单号
|
||
* - extend_props.platform_order_no 补写平台单号,便于与店管家页面按「订单编号」对账
|
||
*
|
||
* 用法:
|
||
* node scripts/migrate-logic-order-id.js # 只打印待迁移清单(dry-run)
|
||
* node scripts/migrate-logic-order-id.js --apply # 执行迁移
|
||
*/
|
||
const mm = require('egg-mock');
|
||
const { ORDER_STATUS } = require('../app/lib/orderStatus');
|
||
|
||
/** 需要改写的状态表 */
|
||
const STATUS_TABLES = Object.values(ORDER_STATUS)
|
||
.filter(status => status !== ORDER_STATUS.HISTORY || true)
|
||
.map(status => `order_status_${status}`);
|
||
|
||
(async () => {
|
||
const apply = process.argv.includes('--apply');
|
||
const app = mm.app();
|
||
await app.ready();
|
||
const ctx = app.mockContext();
|
||
|
||
const orders = await ctx.model.Order.findAll({
|
||
attributes: [ 'id', 'order_no', 'principal_id', 'external_order_id', 'external_data' ],
|
||
raw: true,
|
||
});
|
||
|
||
const plans = [];
|
||
for (const order of orders) {
|
||
const oldId = String(order.external_order_id || '').trim();
|
||
if (!oldId) continue;
|
||
const raw = typeof order.external_data === 'string' ? safeJson(order.external_data) : (order.external_data || {});
|
||
const logicId = String(raw.LogicOrderId || '').trim();
|
||
if (!logicId || logicId === oldId) continue;
|
||
const prefix = String(order.order_no || '').includes('-')
|
||
? String(order.order_no).slice(0, String(order.order_no).indexOf('-'))
|
||
: 'dgj';
|
||
plans.push({
|
||
id: Number(order.id),
|
||
principalId: Number(order.principal_id),
|
||
oldId,
|
||
logicId,
|
||
oldNo: String(order.order_no),
|
||
newNo: `${prefix}-${logicId}`,
|
||
});
|
||
}
|
||
|
||
for (const plan of plans) {
|
||
console.log(`${plan.oldNo} → ${plan.newNo}(来源订单号 ${plan.oldId} → ${plan.logicId})`);
|
||
}
|
||
console.log(`待迁移订单 ${plans.length} 条`);
|
||
|
||
if (!plans.length || !apply) {
|
||
if (plans.length) console.log('[dry-run] 未写库,确认后加 --apply 执行');
|
||
await app.close();
|
||
process.exit(0);
|
||
}
|
||
|
||
const transaction = await ctx.model.transaction();
|
||
try {
|
||
for (const plan of plans) {
|
||
await ctx.model.query(
|
||
'UPDATE `orders` SET order_no = ?, external_order_id = ? WHERE id = ?',
|
||
{ replacements: [ plan.newNo, plan.logicId, plan.id ], transaction }
|
||
);
|
||
|
||
await ctx.model.query(
|
||
'UPDATE `order_index` SET order_no = ?, parent_order_no = ? WHERE principal_id = ? AND (order_no = ? OR parent_order_no = ?)',
|
||
{ replacements: [ plan.newNo, plan.logicId, plan.principalId, plan.oldNo, plan.oldId ], transaction }
|
||
);
|
||
|
||
for (const table of STATUS_TABLES) {
|
||
await ctx.model.query(
|
||
`UPDATE \`${table}\`
|
||
SET order_no = ?, parent_order_no = ?,
|
||
source_order_codes = REPLACE(source_order_codes, ?, ?),
|
||
extend_props = JSON_SET(COALESCE(extend_props, JSON_OBJECT()), '$.platform_order_no', ?)
|
||
WHERE principal_id = ? AND (order_no = ? OR parent_order_no = ?)`,
|
||
{ replacements: [ plan.newNo, plan.logicId, plan.oldId, plan.logicId, plan.oldId, plan.principalId, plan.oldNo, plan.oldId ], transaction }
|
||
);
|
||
}
|
||
|
||
await ctx.model.query(
|
||
'UPDATE `order_status_items` SET source_order_code = ? WHERE source_order_code = ? AND principal_id = ?',
|
||
{ replacements: [ plan.logicId, plan.oldId, plan.principalId ], transaction }
|
||
);
|
||
await ctx.model.query(
|
||
'UPDATE `order_deliveries` SET source_order_codes = REPLACE(source_order_codes, ?, ?) WHERE source_order_codes LIKE ?',
|
||
{ replacements: [ plan.oldId, plan.logicId, `%${plan.oldId}%` ], transaction }
|
||
);
|
||
await ctx.model.query(
|
||
'UPDATE `order_delivery_items` SET source_order_code = ? WHERE source_order_code = ?',
|
||
{ replacements: [ plan.logicId, plan.oldId ], transaction }
|
||
);
|
||
}
|
||
await transaction.commit();
|
||
console.log(`已迁移 ${plans.length} 条订单的来源订单号为店管家系统编号`);
|
||
} catch (err) {
|
||
await transaction.rollback();
|
||
console.error(`迁移失败,已回滚:${err.message}`);
|
||
await app.close();
|
||
process.exit(1);
|
||
}
|
||
|
||
await app.close();
|
||
process.exit(0);
|
||
})().catch(err => {
|
||
console.error(err);
|
||
process.exit(1);
|
||
});
|
||
|
||
/**
|
||
* 宽松解析 JSON:脏数据解析失败时返回空对象(该行跳过迁移)
|
||
* @param {string} text JSON 文本
|
||
* @return {Object} 解析结果
|
||
*/
|
||
function safeJson(text) {
|
||
try {
|
||
return JSON.parse(text) || {};
|
||
} catch {
|
||
return {};
|
||
}
|
||
}
|