店管家侧没有 Alibaba(1688)面单模板,1688 单实际要用下游渠道的电子面单, 按平台 1688 找模板必然取号失败。拉单落库时按硬规则换算(dgj.normalizeOrder): | 原始平台 | 判定(Tags / OrderTags) | 落库平台 | |---|---|---| | 1688(PlatformType=Alibaba) | Tag=OutChannel 且 TagValue=douyin | 抖音小店 DYXD | | 1688 | 有 OutChannel 但渠道未映射 | 其他 OTHER(渠道留痕,等后续映射) | | 1688 | 无 OutChannel(非分销单) | 淘宝 TB | | 其它平台 | —— | 原平台不变 | - 原始报文完整保留:orders.external_data 仍存店管家整包(含 Tags),platform_type 保留 Alibaba - 状态表 extend_props 增加 original_platform_code='1688' 与 distribute_channel=<渠道原值>,便于追溯 - 历史数据由迁移 20261107020000-convert-1688-orders-to-real-waybill-platform 按同一规则换算 (8 张状态表 + orders 镜像表,迁移不做反向) - 新增单测覆盖:抖音分销→DYXD、非分销→TB、未知渠道→OTHER(并断言原始报文保留)、其它平台不受影响 - 文档 店管家取号-旧系统提取.md §4.2.1 补充规则表与留痕字段说明 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
130 lines
4.5 KiB
JavaScript
130 lines
4.5 KiB
JavaScript
'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;
|
||
},
|
||
};
|