'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); });