feat(order): 1688 单按分销渠道换算电商平台(抖音小店/淘宝/其他)
店管家侧没有 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>
This commit is contained in:
parent
1e454d399c
commit
0e222789dd
@ -1947,6 +1947,22 @@ class DgjService extends PrintPlatformService {
|
||||
const externalOrderId = logicOrderId || platformOrderId;
|
||||
// 平台类型转系统预设编码(PDD / DYXD / SPH…),订单表与推 WMS 都用这一套
|
||||
const platform = resolveOrderPlatform(data.PlatformType);
|
||||
const distributeChannel = this.resolveOrderDistributeChannel(data);
|
||||
// 硬规则(分销代发业务口径):1688 单不按 1688 出号,统一换算成实际取号渠道
|
||||
// - 抖音分销单(OutChannel=douyin)→ 抖音小店(DYXD)
|
||||
// - 其它分销渠道(OutChannel=未知值)→ 其他(OTHER),原始报文与渠道留痕,等待后续映射
|
||||
// - 非分销单(无 OutChannel 标签)→ 淘宝(TB)
|
||||
// 否则按 1688 永远匹配不到面单模板,取号必失败
|
||||
let platformCode = platform.code;
|
||||
let platformName = platform.name;
|
||||
if (platform.code === '1688') {
|
||||
let targetCode = 'TB';
|
||||
if (distributeChannel === 'douyin') targetCode = 'DYXD';
|
||||
else if (distributeChannel) targetCode = 'OTHER';
|
||||
const target = resolvePlatform(targetCode);
|
||||
platformCode = target.code;
|
||||
platformName = target.name;
|
||||
}
|
||||
return {
|
||||
external_order_id: externalOrderId,
|
||||
// 平台单号保留下来展示/查询用(与外部环境对账时需要)
|
||||
@ -1955,8 +1971,12 @@ class DgjService extends PrintPlatformService {
|
||||
external_shop_name: truncate(data.ShopName || data.AgentShopName || data.SelfShopName, 128),
|
||||
// 原始值仅作留痕;落库用 platform_code / platform_name
|
||||
platform_type: truncate(data.PlatformType, 32),
|
||||
platform_code: platform.code,
|
||||
platform_name: platform.name,
|
||||
platform_code: platformCode,
|
||||
platform_name: platformName,
|
||||
// 分销渠道与原始平台:换算后仍可追溯「这单其实是 1688 分销到哪个渠道的」
|
||||
distribute_channel: distributeChannel || null,
|
||||
original_platform_code: platform.code,
|
||||
original_platform_name: platform.name,
|
||||
platform_status: truncate(data.ErpState || data.Status, 32),
|
||||
order_no: externalOrderId,
|
||||
total_amount: toNumber(data.TotalAmount),
|
||||
@ -1981,6 +2001,28 @@ class DgjService extends PrintPlatformService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 取订单的分销渠道(Tags/OrderTags 里 `Tag=OutChannel` 的 `TagValue`)
|
||||
*
|
||||
* 1688 分销/代发采购单会带这个标签,值是对下游渠道的标识(实测 `douyin`、
|
||||
* `chaoshibianli_plaintext`、`thyny_plaintext` 等)。分销单必须用下游渠道的电子面单,
|
||||
* 所以「平台」要按下游渠道算,不能按 1688 算,否则取号永远匹配不到模板。
|
||||
* @param {Object} data 店管家订单原始结构
|
||||
* @return {string} 分销渠道标识(小写),无则空串
|
||||
*/
|
||||
resolveOrderDistributeChannel(data) {
|
||||
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 '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 把店管家订单明细转换为统一结构:平台商品快照原样保留,
|
||||
* 系统商品关联由上层按店铺商品绑定关系决定(未绑定时保持原样)
|
||||
|
||||
@ -696,6 +696,9 @@ class ExternalPrintOrderService extends Service {
|
||||
extend_props: {
|
||||
source: 'external_print',
|
||||
platform: account.platform.code,
|
||||
// 1688 分销单会按下游渠道换算平台(douyin→抖音小店),原始平台与渠道留痕便于追溯
|
||||
original_platform_code: item.original_platform_code || null,
|
||||
distribute_channel: item.distribute_channel || null,
|
||||
platform_shop_id: item.external_shop_id || null,
|
||||
platform_shop_name: item.external_shop_name || null,
|
||||
// 平台单号(店管家「订单编号」)与来源订单号(「系统编号」)分开保存,便于对账
|
||||
|
||||
@ -0,0 +1,129 @@
|
||||
'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;
|
||||
},
|
||||
};
|
||||
83
backend/test/app/lib/dgjOrderPlatformRule.test.js
Normal file
83
backend/test/app/lib/dgjOrderPlatformRule.test.js
Normal file
@ -0,0 +1,83 @@
|
||||
'use strict';
|
||||
|
||||
const { assert } = require('egg-mock/bootstrap');
|
||||
const DgjService = require('../../../app/lib/printPlatform/dgj');
|
||||
|
||||
/** 构造一个最小可用的店管家平台服务(normalizeOrder 不触网) */
|
||||
function buildService() {
|
||||
return new DgjService({ platform: { code: 'dgj', name: '店管家' }, account: { token: 't', cookie: 'c' } });
|
||||
}
|
||||
|
||||
/**
|
||||
* 造一条店管家订单行
|
||||
* @param overrides
|
||||
*/
|
||||
function buildRow(overrides = {}) {
|
||||
return {
|
||||
LogicOrderId: '10000279767413',
|
||||
PlatformOrderId: '5127406131122008631',
|
||||
PlatformType: 'Alibaba',
|
||||
ShopId: '7398588',
|
||||
ShopName: '13668975305(意洁)',
|
||||
ErpState: 'waitsellersend',
|
||||
CreateTime: '2026-11-06 10:00:00',
|
||||
UpdateTime: '2026-11-06 10:05:00',
|
||||
TotalAmount: 10,
|
||||
ToName: '张三',
|
||||
ToPhone: '13800000000',
|
||||
SubOrders: [{ OrderItemId: '1', ProductID: '100', Count: 1, Price: 10, OrderItemCode: 'SKU-1' }],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('printPlatform/dgj normalizeOrder 平台换算硬规则', () => {
|
||||
const service = buildService();
|
||||
|
||||
it('1688 抖音分销单 → 抖音小店', () => {
|
||||
const row = buildRow({
|
||||
Tags: [{ Tag: 'OutChannel', TagValue: 'douyin', Platform: 'Alibaba' }],
|
||||
});
|
||||
const order = service.normalizeOrder(row);
|
||||
console.log('抖音分销:', order.platform_code, order.platform_name, order.distribute_channel, order.original_platform_code);
|
||||
assert.strictEqual(order.platform_code, 'DYXD');
|
||||
assert.strictEqual(order.platform_name, '抖音小店');
|
||||
assert.strictEqual(order.distribute_channel, 'douyin');
|
||||
assert.strictEqual(order.original_platform_code, '1688');
|
||||
// 原始平台类型仍留痕
|
||||
assert.strictEqual(order.platform_type, 'Alibaba');
|
||||
});
|
||||
|
||||
it('1688 非分销单(无 OutChannel 标签)→ 淘宝', () => {
|
||||
const order = service.normalizeOrder(buildRow({ Tags: [] }));
|
||||
console.log('非分销:', order.platform_code, order.platform_name, order.distribute_channel);
|
||||
assert.strictEqual(order.platform_code, 'TB');
|
||||
assert.strictEqual(order.platform_name, '淘宝');
|
||||
assert.strictEqual(order.distribute_channel, null);
|
||||
assert.strictEqual(order.original_platform_code, '1688');
|
||||
});
|
||||
|
||||
it('1688 未识别的分销渠道 → 其他(保留渠道与原始报文)', () => {
|
||||
const order = service.normalizeOrder(buildRow({
|
||||
Tags: [{ Tag: 'OutChannel', TagValue: 'thyny_plaintext' }],
|
||||
}));
|
||||
console.log('未知渠道:', order.platform_code, order.platform_name, order.distribute_channel);
|
||||
assert.strictEqual(order.platform_code, 'OTHER');
|
||||
assert.strictEqual(order.platform_name, '其他');
|
||||
assert.strictEqual(order.distribute_channel, 'thyny_plaintext');
|
||||
assert.strictEqual(order.original_platform_code, '1688');
|
||||
// 原始报文完整保留(后续再做渠道映射时要用)
|
||||
assert.deepStrictEqual(order.raw.Tags, [{ Tag: 'OutChannel', TagValue: 'thyny_plaintext' }]);
|
||||
assert.strictEqual(order.platform_type, 'Alibaba');
|
||||
});
|
||||
|
||||
it('其它平台不受该硬规则影响', () => {
|
||||
const taobao = service.normalizeOrder(buildRow({ PlatformType: 'Taobao', Tags: [] }));
|
||||
assert.strictEqual(taobao.platform_code, 'TB');
|
||||
|
||||
const douyin = service.normalizeOrder(buildRow({ PlatformType: 'TouTiao', Tags: [] }));
|
||||
assert.strictEqual(douyin.platform_code, 'DYXD');
|
||||
|
||||
const pdd = service.normalizeOrder(buildRow({ PlatformType: 'Pinduoduo', Tags: [] }));
|
||||
assert.strictEqual(pdd.platform_code, 'PDD');
|
||||
});
|
||||
});
|
||||
@ -166,6 +166,20 @@ Filters[2] TableAlias=pfn FieldType=int TableName=PathFlowNode Name=UpFxUs
|
||||
|
||||
时间窗默认值对齐平台页面:**首次拉取回看 15 天**(平台「待发货订单」页默认下单时间范围就是近 15 天)。
|
||||
|
||||
**平台换算硬规则(分销代发口径)**:店管家 1688 单不按 1688 出号(平台侧没有 Alibaba 面单模板,
|
||||
抖音/1688 分销单必须用下游渠道的电子面单),拉单落库时 `normalizeOrder` 直接换算电商平台:
|
||||
|
||||
| 原始平台 | 判定(`Tags` / `OrderTags`) | 落库平台 |
|
||||
|---|---|---|
|
||||
| 1688(`PlatformType=Alibaba`) | `Tag=OutChannel` 且 `TagValue=douyin`(抖音分销单) | 抖音小店 `DYXD` |
|
||||
| 1688 | 有 `OutChannel` 但渠道未映射(如 `thyny_plaintext`、`chaoshibianli_plaintext`) | 其他 `OTHER`(渠道留痕,等后续映射) |
|
||||
| 1688 | 无 `OutChannel`(非分销单) | 淘宝 `TB` |
|
||||
| 其它平台 | —— | 原平台不变 |
|
||||
|
||||
留痕:状态表 `extend_props.original_platform_code='1688'`、`extend_props.distribute_channel=<渠道>`;
|
||||
`orders.external_data` 保留店管家完整报文(含 `Tags`),后续做渠道映射时直接可用。
|
||||
历史数据由迁移 `20261107020000-convert-1688-orders-to-real-waybill-platform` 按同一规则换算。
|
||||
|
||||
#### 4.2.2「所在区」的刷新(手动 + 定时)
|
||||
|
||||
`DbAreas` 会随平台迁移分区、商家解绑而变化,声明过期会让拉单查错分区或漏单,因此 `shopAreaSync` 负责刷新:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user