oms/backend/scripts/migrate-platform-codes.js
liufumingren 69b26bf7cc feat(platform): 统一系统预设平台编码,外部平台标识按预设转换
- 新增 app/lib/platformCode.js:17 个预设平台(1688/DW/DYCS/DYGX/DYXD/JD/KS/MAOCHAO/
  MTYYJK/OFFLINE/OTHER/PDD/SPH/TB/TGC/TM/XHS)+ 别名表(中英文、大小写)+ 未知落 OTHER
- 店管家硬编码 PlatformType → 预设映射(Pinduoduo→PDD、TouTiao→DYXD、WxVideo→SPH、
  Alibaba→1688、Taobao→TB、KuaiShou→KS、Tmall→TM、JingDong→JD…)
- 拉单落库统一:orders.platform_type、order_status_*.source_platform_code/name
  写预设编码与本系统中文名(推 WMS 的 sourcePlatformCode/Name 同源)
- 店铺商品 platform_type 同样落预设编码
- 新增 scripts/migrate-platform-codes.js(npm run platforms:migrate):
  按同一口径迁移历史数据,默认 dry-run;已执行迁移 14 类(订单 27 / 店铺商品 44 / 状态表 15+13)
- 前端新增 constants/platform.ts(code→中文名):店铺商品「平台类型」列显示中文名,
  订单详情新增「来源平台」行
- 新增 platformCode 单测(5 项,含未知平台落 OTHER)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-03 17:31:44 +08:00

102 lines
4.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 把外部打单平台写下的「平台类型」统一迁移为系统预设平台编码/名称。
*
* 背景:早期把店管家的 PlatformType(Pinduoduo / TouTiao / WxVideo…)直接写进了
* orders.platform_type、订单状态表的 source_platform_code/source_platform_name、
* 店铺商品的 platform_type;系统预设见 app/lib/platformCode.js(PDD / DYXD / SPH…),
* 推送 WMS 的 sourcePlatformCode/sourcePlatformName 也要求这套口径。
*
* 用法:
* node scripts/migrate-platform-codes.js # 只统计(dry-run)
* node scripts/migrate-platform-codes.js --apply # 执行迁移
*/
const mm = require('egg-mock');
const DgjService = require('../app/lib/printPlatform/dgj');
const { ORDER_STATUS } = require('../app/lib/orderStatus');
/** 需要迁移来源平台编码的状态表 */
const STATUS_TABLES = Object.values(ORDER_STATUS).map(status => `order_status_${status}`);
(async () => {
const apply = process.argv.includes('--apply');
const app = mm.app();
await app.ready();
const ctx = app.mockContext();
const select = sql => ctx.model.query(sql, { type: app.Sequelize.QueryTypes.SELECT });
/** scope -> 需要迁移的旧值统计 */
const plans = new Map();
const add = (scope, oldValue, count) => {
const platform = DgjService.resolveOrderPlatform(oldValue);
if (oldValue === platform.code) return;
const key = `${scope}|${oldValue}`;
const exists = plans.get(key);
if (exists) exists.count += Number(count);
else plans.set(key, { scope, oldValue, code: platform.code, name: platform.name, count: Number(count) });
};
const orderRows = await select("SELECT platform_type AS v, COUNT(*) AS c FROM `orders` WHERE platform_type IS NOT NULL AND platform_type <> '' GROUP BY platform_type");
for (const row of orderRows) add('orders.platform_type', row.v, row.c);
const shopProductRows = await select("SELECT platform_type AS v, COUNT(*) AS c FROM `shop_products` WHERE platform_type IS NOT NULL AND platform_type <> '' GROUP BY platform_type");
for (const row of shopProductRows) add('shop_products.platform_type', row.v, row.c);
for (const table of STATUS_TABLES) {
const rows = await select(
`SELECT source_platform_code AS v, COUNT(*) AS c FROM \`${table}\`
WHERE source_platform_code IS NOT NULL AND source_platform_code <> ''
GROUP BY source_platform_code`
);
for (const row of rows) add('order_status.source_platform_code', row.v, row.c);
}
const changes = [ ...plans.values() ];
for (const item of changes) {
console.log(`${item.scope}:${item.oldValue} → ${item.code}(${item.name})× ${item.count}`);
}
console.log(`待迁移项 ${changes.length} 类`);
if (!changes.length || !apply) {
if (changes.length) console.log('[dry-run] 未写库,确认后加 --apply 执行');
await app.close();
process.exit(0);
}
const transaction = await ctx.model.transaction();
try {
for (const item of changes) {
if (item.scope === 'orders.platform_type') {
await ctx.model.query('UPDATE `orders` SET platform_type = ? WHERE platform_type = ?', {
replacements: [ item.code, item.oldValue ], transaction,
});
} else if (item.scope === 'shop_products.platform_type') {
await ctx.model.query('UPDATE `shop_products` SET platform_type = ? WHERE platform_type = ?', {
replacements: [ item.code, item.oldValue ], transaction,
});
} else {
for (const table of STATUS_TABLES) {
await ctx.model.query(
`UPDATE \`${table}\` SET source_platform_code = ?, source_platform_name = ? WHERE source_platform_code = ?`,
{ replacements: [ item.code, item.name, item.oldValue ], transaction }
);
}
}
}
await transaction.commit();
console.log(`已迁移 ${changes.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);
});