fix(express): 菠萝派平台返回空 results 时判定为系统级失败

- polyapi 取号结果 results 为空数组且请求含订单时(如 code=40000 / GSE.REQUEST_FAILURE.SYSTEMERROR),
  旧逻辑会当成 0 条结果继续走成功分支,订单既不回写运单号也不标记异常;
  现直接按失败抛出,失败原因拼接 submessage/message,并附带 polyapirequestid 便于平台侧定位。
- 顺带修正 providercode 判断处的代码风格(if 后空格)。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-10-28 07:22:33 +08:00
parent 15c739c645
commit 8f2e141fe0
2 changed files with 95 additions and 6 deletions

View File

@ -928,7 +928,7 @@ class ExpressPlatformService extends MainService {
};
console.log('matchedPlatform.platid', matchedPlatform.platid);
console.log('startPlayload', playload);
if(playload.providercode === 'JD'){
if (playload.providercode === 'JD') {
playload.pickUpDate = pickUpDate;
playload.pickUpEndDate = pickUpEndDate;
playload.shipperno = listByWarehousePlatformCode.shipperno || '';
@ -1080,6 +1080,34 @@ class ExpressPlatformService extends MainService {
{
// 逐条检查 issuccess,将平台返回的业务失败项单独提取,避免因 logisticno 为空被误判为"运单号为空"
const allResults = Array.isArray(result.results) ? result.results : [];
// 平台系统级失败(如 code=40000 / GSE.REQUEST_FAILURE.SYSTEMERROR)时 results 是空数组,
// 继续往下走会把"0 条结果"当成成功,订单既不回写运单号也不标记异常,这里直接按失败抛出。
if (allResults.length === 0 && normalizedOrders.length > 0) {
const polyapiRequestId = String(
result && (result.polyapirequestid || result.requestid || result.request_id) || ''
).trim();
const emptyResultReason = String(
result && (result.submessage || result.message) || ''
).trim() || `菠萝派平台接口未返回取号结果(code=${String(result && result.code || '') || '空'})`;
// 带上菠萝派请求ID,便于平台侧按 polyapirequestid 定位本次调用
const reasonWithRequestId = polyapiRequestId
? `${emptyResultReason}(polyapirequestid=${polyapiRequestId})`
: emptyResultReason;
this.ctx.logger.error('[expressPlatform.polyapi] 平台返回空 results,判定为系统级失败', {
traceId,
polyapirequestid: polyapiRequestId,
code: result && result.code,
subcode: result && result.subcode,
reason: emptyResultReason,
orderCount: normalizedOrders.length,
response: JSON.stringify(result),
});
const allFailedOrders = this.buildFailedOrderCleanupList(orders);
await this.cleanupFailedWaybillOrders(allFailedOrders, reasonWithRequestId);
throw new Error(`菠萝派分配快递号失败: ${reasonWithRequestId}`);
}
const itemFailedList = [];
const itemSuccessList = [];
for (const item of allResults) {

View File

@ -34,6 +34,8 @@ function createPolyapiHarness(options = {}) {
orderExpressUpdateArgsList: [],
orderExpressFindAll: 0,
orderExpressFindAllQuery: null,
orderDeliveryFindAll: 0,
orderDeliveryFindAllQuery: null,
orderDeliveryUpdate: 0,
orderDeliveryUpdateArgsList: [],
storeWarehouseFindAll: 0,
@ -52,6 +54,11 @@ function createPolyapiHarness(options = {}) {
};
const orderDeliveryModel = {
async findAll(query) {
calls.orderDeliveryFindAll += 1;
calls.orderDeliveryFindAllQuery = query;
return [];
},
async update(values, query) {
calls.callSequence.push('orderDelivery.update');
calls.orderDeliveryUpdate += 1;
@ -103,6 +110,19 @@ function createPolyapiHarness(options = {}) {
in: 'in',
},
},
formatDate(date, formatStr = 'YYYY-MM-DD HH:mm:ss') {
const targetDate = date instanceof Date ? date : new Date(date);
const values = {
YYYY: String(targetDate.getFullYear()),
MM: String(targetDate.getMonth() + 1).padStart(2, '0'),
DD: String(targetDate.getDate()).padStart(2, '0'),
HH: String(targetDate.getHours()).padStart(2, '0'),
mm: String(targetDate.getMinutes()).padStart(2, '0'),
ss: String(targetDate.getSeconds()).padStart(2, '0'),
};
return formatStr.replace(/YYYY|MM|DD|HH|mm|ss/g, token => values[token]);
},
},
model: {
async transaction(callback) {
@ -193,11 +213,14 @@ function createPolyapiHarness(options = {}) {
matchedPlatform,
token,
};
if (apiName === 'Differ.JH.Logistics.PostOrder' && options.postOrderResult !== undefined) {
return options.postOrderResult;
}
return options.result || {
code: '10000',
results: [
{
orderno: '186a0_101_9101',
orderno: '186a0_65_9101',
waybill_code: 'REAL-WB-101',
logisticno: 'PRE-WB-101',
printinfo: 'print-101',
@ -206,7 +229,7 @@ function createPolyapiHarness(options = {}) {
issuccess: '1',
},
{
orderno: '186a0_102_9102',
orderno: '186a0_66_9102',
logisticno: 'WB-102',
printinfo: 'print-102',
signature: 'sign-102',
@ -214,7 +237,7 @@ function createPolyapiHarness(options = {}) {
issuccess: '1',
},
{
orderno: '186a0_103_9103',
orderno: '186a0_67_9103',
logisticno: 'WB-103',
printinfo: 'print-103',
signature: 'sign-103',
@ -284,6 +307,7 @@ function buildPolyapiPayload() {
},
listByWarehousePlatformCode: {
id: 11,
warehouse_id: 100000,
platform_code: 'KS',
sys_express_code: 'SF',
logistics_code: 'SF-LOGI',
@ -353,6 +377,8 @@ describe('test/app/service/systems/express_platform.polyapi.test.js', () => {
assert.equal(calls.storeWarehouseUpdateArgsList[0].query.where.id, 77);
assert.equal(calls.orderExpressFindAll, 1);
assert.deepEqual(calls.orderExpressFindAllQuery.where.order_id.in, [ 101, 102, 103 ]);
assert.equal(calls.orderDeliveryFindAll, 1);
assert.deepEqual(calls.orderDeliveryFindAllQuery.where.id, [ 101, 102, 103 ]);
assert.equal(calls.printQuantityLogCreateArgsList[0].opts.transaction, calls.transactionRef);
assert.equal(calls.printQuantityLogCreateArgsList[0].values.change_type, 'order_deduct');
assert.ok(calls.printQuantityLogCreateArgsList[0].values.change_quantity < 0);
@ -360,8 +386,8 @@ describe('test/app/service/systems/express_platform.polyapi.test.js', () => {
assert.ok(calls.printQuantityLogCreateArgsList[0].values.balance_after >= 0);
assert.equal(relationRows[0].print_quantity, 6);
assert.equal(relationRows[0].print_quantity_locked, 3);
assert.equal(calls.getWaybillAsyncOrderDeliveryModel, 3);
assert.deepEqual(calls.getWaybillAsyncOrderDeliveryModelArgs, [ 100000, 100000, 100000 ]);
assert.equal(calls.getWaybillAsyncOrderDeliveryModel, 4);
assert.deepEqual(calls.getWaybillAsyncOrderDeliveryModelArgs, [ 100000, 100000, 100000, 100000 ]);
assert.equal(calls.operationLogArgsList[0].businessId, '101');
assert.equal(calls.requestArgs.bizcontent.orders[0].logisticno, 'REAL-WB-101');
assert.equal(calls.requestArgs.bizcontent.orders[1].logisticno, 'REAL-WB-102');
@ -379,6 +405,41 @@ describe('test/app/service/systems/express_platform.polyapi.test.js', () => {
assert.equal(calls.orderDeliveryUpdateArgsList[0].values.post_status, 'fin');
});
it('should throw when polyapi returns system error with empty results', async () => {
const { calls, service } = createPolyapiHarness({
postOrderResult: {
code: '40000',
env: '3',
messagefrom: 'POLY',
msg: 'System Error',
polyapirequestid: '1276199519688854528P3672M010E11',
polyapitotalms: 5,
results: [],
subcode: 'GSE.REQUEST_FAILURE.SYSTEMERROR',
submessage: '服务异常请稍后再试',
},
});
await assert.rejects(
() => service.polyapi(buildPolyapiPayload()),
/菠萝派分配快递号失败: 服务异常请稍后再试/
);
// 取号失败不应进入回写事务,也不应写成功日志
assert.equal(calls.transaction, 0);
assert.equal(calls.orderExpressUpdate, 0);
assert.equal(calls.orderDeliveryUpdate, 0);
assert.equal(calls.operationLogBatchCreate, 0);
// 失败订单的锁定打印量需要回退
assert.equal(calls.storeWarehouseIncrement, 1);
assert.equal(
calls.storeWarehouseIncrementArgsList[0].field,
'print_quantity_locked'
);
// 只有 101/102 带 store_id,回退 2 笔
assert.equal(calls.storeWarehouseIncrementArgsList[0].query.by, -2);
});
it('should still deduct print balance when locked balance is zero after polyapi callback success', async () => {
const { calls, relationRows, service } = createPolyapiHarness({
relationRows: [