'use strict'; /** * 把历史 1688 订单的「电商平台」换算成实际取号渠道(与拉单时的硬规则保持一致)。 * * 业务口径(分销代发):1688 单不按 1688 出号,面单要用下游渠道的: * - 抖音分销单(`external_data.Tags` 里 `Tag=OutChannel` 且 `TagValue=douyin`)→ 抖音小店(DYXD) * - 其它分销渠道(有 OutChannel 但值未映射)→ 其他(OTHER),渠道与原始报文本就保留,等后续映射 * - 非分销单(无 OutChannel 标签)→ 淘宝(TB) * 否则取号按平台 1688 找模板,永远匹配不到。 * * 同时把原始平台(1688)与分销渠道写入状态表 `extend_props` 便于追溯; * `orders.external_data` 里的完整报文保持不动(后续再做渠道映射时要用)。 */ /** 订单状态表(与 app/lib/orderStatus.js 的 ORDER_STATUS 对齐) */ const STATUS_TABLES = [ 'order_status_pending', 'order_status_queued', 'order_status_exception', 'order_status_outsourcing', 'order_status_delivery', 'order_status_void', 'order_status_canceled', 'order_status_history', ]; /** * 从店管家原始结构里取分销渠道(Tags / OrderTags 里 Tag=OutChannel 的 TagValue) * @param {Object} raw 店管家订单原始结构 * @return {string} 渠道标识(小写),无则空串 */ function resolveDistributeChannel(raw) { const data = typeof raw === 'string' ? safeParse(raw) : raw; const tags = [ ...(Array.isArray(data && data.Tags) ? data.Tags : []), ...(Array.isArray(data && data.OrderTags) ? data.OrderTags : []), ]; for (const tag of tags) { if (!tag || String(tag.Tag || '') !== 'OutChannel') continue; const value = String(tag.TagValue || '').trim().toLowerCase(); if (value) return value; } return ''; } /** * 安全解析 JSON 字符串 * @param {string} text JSON 文本 * @return {Object} 解析结果,失败为空对象 */ function safeParse(text) { try { return JSON.parse(text); } catch { return {}; } } /** * 分销渠道 → 平台编码/名称(与 service 里的硬规则一致) * @param {string} channel 渠道标识 * @return {{code: string, name: string}} 平台 */ function resolvePlatformByChannel(channel) { if (channel === 'douyin') return { code: 'DYXD', name: '抖音小店' }; if (channel) return { code: 'OTHER', name: '其他' }; return { code: 'TB', name: '淘宝' }; } module.exports = { async up(queryInterface) { const sequelize = queryInterface.sequelize; const transaction = await sequelize.transaction(); try { for (const table of STATUS_TABLES) { const rows = await sequelize.query( `SELECT t.id, o.external_data FROM \`${table}\` t JOIN \`orders\` o ON o.order_no = t.order_no AND o.principal_id = t.principal_id WHERE t.source_platform_code = '1688'`, { type: sequelize.QueryTypes.SELECT, transaction } ); for (const row of rows) { const channel = resolveDistributeChannel(row.external_data); const platform = resolvePlatformByChannel(channel); await sequelize.query( `UPDATE \`${table}\` SET source_platform_code = ?, source_platform_name = ?, extend_props = JSON_SET( COALESCE(extend_props, JSON_OBJECT()), '$.original_platform_code', '1688', '$.distribute_channel', ? ) WHERE id = ?`, { replacements: [ platform.code, platform.name, channel || null, Number(row.id) ], transaction, } ); } } // 老订单表镜像的平台编码同步换算 const legacyRows = await sequelize.query( "SELECT id, external_data FROM `orders` WHERE platform_type = '1688'", { type: sequelize.QueryTypes.SELECT, transaction } ); for (const row of legacyRows) { const channel = resolveDistributeChannel(row.external_data); const platform = resolvePlatformByChannel(channel); await sequelize.query( 'UPDATE `orders` SET platform_type = ? WHERE id = ?', { replacements: [ platform.code, Number(row.id) ], transaction } ); } await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } }, // 换算属业务口径修正:撤销会把 1688 分销单重新变回取不到号的状态,故不做反向操作 async down() { return undefined; }, };