fix(print): express 打印组件解析支持系统平台编码回退

- 淘宝 TB/天猫 TM/1688/TGC 等系统平台编码未在 wms_print_platform 直接匹配时,
  经 wms_platform.print_platform_id 回退解析 wms_print_platform.platform_name
- 移除 loadExpressPlatformComponents 调试 console.log,改走 ctx.logger.info
- 新增 TB 回退解析成功与无映射失败的单元测试
This commit is contained in:
liufumingren 2026-08-18 23:59:11 +08:00
parent aaadce25af
commit 943a20f582
2 changed files with 223 additions and 21 deletions

View File

@ -1787,6 +1787,14 @@ class PrintService extends MainService {
/**
* 加载并校验 express 请求涉及平台的打印组件配置。
*
* 解析顺序:
* 1. 直接按 `wms_print_platform.platform_code` 匹配(快递接口透出的打印平台编码,
* 如 CAINIAO/DY/JD/KS/PDD/SPH/XHS);
* 2. 未匹配的编码视为系统平台编码(如 TB/TM/1688/TGC),经
* `wms_platform.code -> print_platform_id -> wms_print_platform.platform_name`
* 回退解析对应打印组件。
*
* @param {Object} payload 查询参数。
* @param {Array<string>} payload.platformCodes 平台编码列表。
* @return {Promise<{success:boolean,data?:Object,error?:string}>} 查询与校验结果。
@ -1803,22 +1811,81 @@ class PrintService extends MainService {
};
}
const platformRows = await this.ctx.model.PrintPlatform.findAll({
attributes: [ 'platform_code', 'platform_name' ],
where: {
platform_code: platformCodes,
},
raw: true,
});
const printPlatformModel = this.ctx && this.ctx.model && this.ctx.model.PrintPlatform;
const platformModel = this.ctx && this.ctx.model && this.ctx.model.Platform;
const platformComponentMap = {};
for (const row of platformRows) {
const platformCode = String(row && row.platform_code ? row.platform_code : '').trim();
if (!platformCode) {
continue;
// 1) 优先按打印平台编码直接匹配
if (printPlatformModel && typeof printPlatformModel.findAll === 'function') {
const platformRows = await printPlatformModel.findAll({
attributes: [ 'id', 'platform_code', 'platform_name' ],
where: {
platform_code: platformCodes,
},
raw: true,
});
for (const row of Array.isArray(platformRows) ? platformRows : []) {
const platformCode = String(row && row.platform_code ? row.platform_code : '').trim();
if (!platformCode) {
continue;
}
platformComponentMap[platformCode] = String(row && row.platform_name ? row.platform_name : '').trim();
}
platformComponentMap[platformCode] = String(row && row.platform_name ? row.platform_name : '').trim();
}
console.log('platformComponentMap', platformComponentMap);
// 2) 未匹配编码按系统平台编码回退解析(TB/TM/1688 等经 print_platform_id 关联)
const unresolvedCodes = platformCodes.filter(platformCode => {
return !String(platformComponentMap[platformCode] || '').trim();
});
if (unresolvedCodes.length > 0
&& printPlatformModel && typeof printPlatformModel.findAll === 'function'
&& platformModel && typeof platformModel.findAll === 'function') {
const systemPlatformRows = await platformModel.findAll({
attributes: [ 'code', 'print_platform_id' ],
where: {
code: unresolvedCodes,
},
raw: true,
});
const codeListByPrintPlatformIdMap = new Map();
for (const row of Array.isArray(systemPlatformRows) ? systemPlatformRows : []) {
const platformCode = String(row && row.code ? row.code : '').trim();
const printPlatformId = Number(row && row.print_platform_id || 0);
if (!platformCode || !Number.isInteger(printPlatformId) || printPlatformId <= 0) {
continue;
}
if (!codeListByPrintPlatformIdMap.has(printPlatformId)) {
codeListByPrintPlatformIdMap.set(printPlatformId, []);
}
codeListByPrintPlatformIdMap.get(printPlatformId).push(platformCode);
}
if (codeListByPrintPlatformIdMap.size > 0) {
const printPlatformRows = await printPlatformModel.findAll({
attributes: [ 'id', 'platform_name' ],
where: {
id: Array.from(codeListByPrintPlatformIdMap.keys()),
},
raw: true,
});
const componentNameByPrintPlatformIdMap = new Map();
for (const row of Array.isArray(printPlatformRows) ? printPlatformRows : []) {
const printPlatformId = Number(row && row.id || 0);
const componentName = String(row && row.platform_name ? row.platform_name : '').trim();
if (!Number.isInteger(printPlatformId) || printPlatformId <= 0) {
continue;
}
componentNameByPrintPlatformIdMap.set(printPlatformId, componentName);
}
for (const [ printPlatformId, codeList ] of codeListByPrintPlatformIdMap) {
const componentName = componentNameByPrintPlatformIdMap.get(printPlatformId) || '';
for (const platformCode of codeList) {
platformComponentMap[platformCode] = componentName;
}
}
}
}
for (const platformCode of platformCodes) {
const component = String(platformComponentMap[platformCode] || '').trim();
if (!component) {
@ -1829,6 +1896,10 @@ class PrintService extends MainService {
}
}
this.ctx.logger.info('[print.send] express 平台打印组件解析完成', {
platformComponents: platformComponentMap,
});
return {
success: true,
data: platformComponentMap,

View File

@ -25,6 +25,8 @@ function createContext(options = {}) {
sendOrderMarks: [],
redisGetKeys: [],
platformFindAllWheres: [],
systemPlatformFindAllWheres: [],
printPlatformIdFindAllWheres: [],
orderDeliveryLockUpdates: [],
operationLogCreatePayloads: [],
};
@ -66,6 +68,18 @@ function createContext(options = {}) {
douyin: '抖音',
};
const platformPrintIdMap = requestOptions.platformPrintIdMap || {
TB: 2,
TM: 2,
1688: 2,
TGC: 2,
MAOCHAO: 2,
};
const printPlatformNameById = requestOptions.printPlatformNameById || {
2: '菜鸟',
};
const printStation = requestOptions.printStation || {
id: 101,
warehouse_id: 100000,
@ -276,18 +290,41 @@ function createContext(options = {}) {
},
PrintPlatform: {
async findAll(query) {
calls.platformFindAllWheres.push(query && query.where ? query.where : {});
const codeList = query && query.where && Array.isArray(query.where.platform_code)
? query.where.platform_code
: [];
return codeList.map(platformCode => ({
platform_code: platformCode,
platform_name: Object.prototype.hasOwnProperty.call(platformComponents, platformCode)
? platformComponents[platformCode]
const where = query && query.where ? query.where : {};
if (Array.isArray(where.platform_code)) {
calls.platformFindAllWheres.push(where);
const codeList = where.platform_code;
return codeList.map(platformCode => ({
id: null,
platform_code: platformCode,
platform_name: Object.prototype.hasOwnProperty.call(platformComponents, platformCode)
? platformComponents[platformCode]
: '',
}));
}
calls.printPlatformIdFindAllWheres.push(where);
const idList = Array.isArray(where.id) ? where.id : [];
return idList.map(printPlatformId => ({
id: printPlatformId,
platform_name: Object.prototype.hasOwnProperty.call(printPlatformNameById, printPlatformId)
? printPlatformNameById[printPlatformId]
: '',
}));
},
},
Platform: {
async findAll(query) {
const where = query && query.where ? query.where : {};
calls.systemPlatformFindAllWheres.push(where);
const codeList = Array.isArray(where.code) ? where.code : [];
return codeList.map(platformCode => ({
code: platformCode,
print_platform_id: Object.prototype.hasOwnProperty.call(platformPrintIdMap, platformCode)
? platformPrintIdMap[platformCode]
: null,
}));
},
},
OrderDeliveryFactory: {
async getCurrentModel() {
return {
@ -690,6 +727,100 @@ describe('test/app/service/systemMethods/print.send.test.js', () => {
assert.equal(calls.sendToComputerPayloads.length, 0);
});
it('type=express 系统平台编码 TB 应经 wms_platform 回退解析打印组件', async () => {
const { ctx, calls } = createContext({
componentStatuses: {
'printer:PC-STATION-001:name:菜鸟:status': 'connected',
},
});
const service = new PrintService(ctx);
const result = await service.send({
type: 'express',
printStationId: 101,
requestCode: 'REQ-TB-FALLBACK-001',
data: {
waveGroups: [
{
wave_id: 1,
wave_no: 'WAVE-TB-1',
platforms: [
{
platform: 'TB',
sys_logistics_code: 'STO',
orders: [
{
print_data: {
documentID: 'DOC-TB-1',
contents: [
{
marker: 'TB-1',
},
],
},
},
],
},
],
},
],
},
});
assert.equal(result.flag, 'success');
assert.equal(calls.sendToComputerPayloads.length, 1);
const components = calls.sendToComputerPayloads.map(item => item.data.component);
assert.deepEqual(components, [ '菜鸟' ]);
assert.deepEqual(calls.systemPlatformFindAllWheres[0].code, [ 'TB' ]);
assert.deepEqual(calls.printPlatformIdFindAllWheres[0].id, [ 2 ]);
assert.ok(calls.redisGetKeys.includes('printer:PC-STATION-001:name:菜鸟:status'));
});
it('type=express 系统平台编码无映射时应失败且不发送任务', async () => {
const { ctx, calls } = createContext({
platformPrintIdMap: {},
});
const service = new PrintService(ctx);
const result = await service.send({
type: 'express',
printStationId: 101,
requestCode: 'REQ-TB-UNMAPPED-001',
data: {
waveGroups: [
{
wave_id: 1,
wave_no: 'WAVE-TB-2',
platforms: [
{
platform: 'TB',
sys_logistics_code: 'STO',
orders: [
{
print_data: {
documentID: 'DOC-TB-2',
contents: [
{
marker: 'TB-2',
},
],
},
},
],
},
],
},
],
},
});
assert.equal(result.flag, 'failure');
assert.equal(result.code, 400);
assert.match(result.message, /平台 TB 未配置打印组件/);
assert.equal(calls.printRecordCreatePayloads.length, 0);
assert.equal(calls.sendToComputerPayloads.length, 0);
});
it('type=express 快手平台应按10单分批发送并注入文档位置信息', async () => {
const { ctx, calls } = createContext({
platformComponents: {