批量审核对每一单都串行请求店管家「平台是否已发货」校验,20 单就要 8~13 秒, 50 单约 25 秒;前端 axios 超时 15 秒必然中断(nginx 日志大量 499),用户看到"审核无响应"。 - 新增 externalPrintOrder.checkPlatformFinishedBatch:同一「账号 + 订单域名 + 分区库」 的订单用一次 LogicOrderId Contract=in 批量回读;接口没返回的订单才逐单兜底 (时间窗回扫,与单条校验同口径),兜底并发 4 且受总时间预算约束 (DGJ_VERIFY_BUDGET_MS,默认 5 秒),超预算按"校验失败放行"处理,与原有口径一致 - 批量审核先整批校验平台状态、再逐单落库,落库时不再重复请求平台; 自动作废仍保留在逐单流程里,避免并发写同一批库存行 - dgj traceId 改为进程级缓存(key = 账号 + token):业务侧逐单 new 服务实例, 实例级缓存命中不了,每单都会多打一次 GetUserInfo Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
169 lines
6.5 KiB
JavaScript
169 lines
6.5 KiB
JavaScript
'use strict';
|
||
|
||
const { app, assert } = require('egg-mock/bootstrap');
|
||
|
||
/**
|
||
* 审核/推送前的平台状态批量校验
|
||
*
|
||
* 要点:同一「账号 + 域名 + 分区库」的订单只发一次接口(LogicOrderId 多值过滤),
|
||
* 只有接口没返回的订单才逐单兜底(时间窗回扫),与单条校验行为一致。
|
||
*/
|
||
describe('service/externalPrintOrder checkPlatformFinishedBatch', () => {
|
||
const PRINCIPAL_ID = 3;
|
||
const describeFinished = row => ({
|
||
finished: row.ErpState !== 'waitsellersend',
|
||
reason: row.ErpState === 'waitsellersend' ? '' : 'ErpState=sended',
|
||
});
|
||
|
||
const order = (id, extra = {}) => ({ id, shop_id: 2, source_platform_code: 'DYXD', ...extra });
|
||
const index = id => ({ id, order_no: `dgj-${id}`, parent_order_no: String(id) });
|
||
|
||
/**
|
||
* 构造服务实例,并用桩替换目标解析(不连库)
|
||
* @param {Object} [options] 桩配置
|
||
* @param {Array<Object>} [options.rows] 批量接口返回的订单行
|
||
* @param {Function} [options.fetchOrdersByIds] 批量接口桩
|
||
* @param {Function} [options.fetchPlatformOrder] 单条兜底接口桩
|
||
* @return {{service: Object, calls: Object}} 服务实例与调用记录
|
||
*/
|
||
function buildService(options = {}) {
|
||
const rows = options.rows || [];
|
||
const fetchOrdersByIds = options.fetchOrdersByIds;
|
||
const fetchPlatformOrder = options.fetchPlatformOrder;
|
||
const calls = { batch: [], single: [] };
|
||
const platformService = {
|
||
resolveTraceId: async () => 'trace-x',
|
||
fetchOrdersByIds: async (host, options) => {
|
||
calls.batch.push({ host, ...options });
|
||
if (fetchOrdersByIds) return fetchOrdersByIds(host, options);
|
||
return rows;
|
||
},
|
||
fetchPlatformOrder: async options => {
|
||
calls.single.push(options);
|
||
if (fetchPlatformOrder) return fetchPlatformOrder(options);
|
||
return null;
|
||
},
|
||
};
|
||
const service = app.mockContext().service.externalPrintOrder;
|
||
service._resolvePlatformCheckTarget = async () => ({
|
||
service: platformService,
|
||
accountId: 1,
|
||
platformType: 'TouTiao',
|
||
host: 'fxdd.dgjapp.com',
|
||
dbname: 'db-1',
|
||
describe: describeFinished,
|
||
});
|
||
return { service, calls };
|
||
}
|
||
|
||
it('同批订单只发一次批量接口,找到的按平台状态判定', async () => {
|
||
const { service, calls } = buildService({
|
||
rows: [
|
||
{ LogicOrderId: '100', ErpState: 'waitsellersend' },
|
||
{ LogicOrderId: '200', ErpState: 'sended' },
|
||
],
|
||
});
|
||
|
||
const results = await service.checkPlatformFinishedBatch([
|
||
{ order: order(100), index: index(100) },
|
||
{ order: order(200), index: index(200) },
|
||
], PRINCIPAL_ID);
|
||
|
||
assert.strictEqual(calls.batch.length, 1);
|
||
assert.deepStrictEqual(calls.batch[0].ids, [ '100', '200' ]);
|
||
assert.strictEqual(calls.single.length, 0);
|
||
assert.strictEqual(results.get(100).finished, false);
|
||
assert.strictEqual(results.get(200).finished, true);
|
||
assert.strictEqual(results.get(200).status, 'sended');
|
||
});
|
||
|
||
it('接口没返回的订单才逐单兜底(行为与单条校验一致)', async () => {
|
||
const { service, calls } = buildService({
|
||
rows: [{ LogicOrderId: '100', ErpState: 'waitsellersend' }],
|
||
fetchPlatformOrder: options => (options.logicalOrderId === '300'
|
||
? { ErpState: 'sended' }
|
||
: null),
|
||
});
|
||
|
||
const results = await service.checkPlatformFinishedBatch([
|
||
{ order: order(100), index: index(100) },
|
||
{ order: order(200), index: index(200) },
|
||
{ order: order(300), index: index(300) },
|
||
], PRINCIPAL_ID);
|
||
|
||
assert.strictEqual(calls.batch.length, 1);
|
||
assert.deepStrictEqual(calls.single.map(item => item.logicalOrderId).sort(), [ '200', '300' ]);
|
||
assert.strictEqual(results.get(100).finished, false);
|
||
assert.strictEqual(results.get(200).finished, false, '兜底也没查到时按未终结放行');
|
||
assert.strictEqual(results.get(300).finished, true);
|
||
});
|
||
|
||
it('批量接口失败时整批走兜底,不阻断审核', async () => {
|
||
const { service, calls } = buildService({
|
||
fetchOrdersByIds: () => {
|
||
throw new Error('核对店管家订单失败');
|
||
},
|
||
fetchPlatformOrder: options => ({ LogicOrderId: options.logicalOrderId, ErpState: 'waitsellersend' }),
|
||
});
|
||
|
||
const results = await service.checkPlatformFinishedBatch([
|
||
{ order: order(100), index: index(100) },
|
||
{ order: order(200), index: index(200) },
|
||
], PRINCIPAL_ID);
|
||
|
||
assert.strictEqual(calls.single.length, 2);
|
||
assert.strictEqual(results.get(100).finished, false);
|
||
assert.strictEqual(results.get(200).finished, false);
|
||
});
|
||
|
||
it('不适用平台校验的订单(非外部打单店铺等)直接放行', async () => {
|
||
const service = app.mockContext().service.externalPrintOrder;
|
||
service._resolvePlatformCheckTarget = async () => null;
|
||
|
||
const results = await service.checkPlatformFinishedBatch([
|
||
{ order: order(100), index: index(100) },
|
||
], PRINCIPAL_ID);
|
||
|
||
assert.deepStrictEqual(results.get(100), {
|
||
applicable: false, finished: false, status: '', reason: '', row: null,
|
||
});
|
||
});
|
||
|
||
it('兜底阶段预算用尽时剩余订单按放行处理(null)', async () => {
|
||
const previous = process.env.DGJ_VERIFY_BUDGET_MS;
|
||
process.env.DGJ_VERIFY_BUDGET_MS = '20';
|
||
try {
|
||
const { service, calls } = buildService({
|
||
rows: [],
|
||
fetchPlatformOrder: () => new Promise(resolve => {
|
||
setTimeout(() => resolve({ ErpState: 'sended' }), 50);
|
||
}),
|
||
});
|
||
const items = [ 1, 2, 3, 4, 5, 6 ].map(id => ({ order: order(id), index: index(id) }));
|
||
|
||
const results = await service.checkPlatformFinishedBatch(items, PRINCIPAL_ID);
|
||
|
||
assert.strictEqual(results.size, 6);
|
||
// 并发 4 先起步,剩下的 2 单在预算用尽后直接放行、不再打接口
|
||
assert.strictEqual(calls.single.length, 4);
|
||
const nulls = [ ...results.values() ].filter(value => value === null);
|
||
assert.strictEqual(nulls.length, 2);
|
||
} finally {
|
||
if (previous === undefined) delete process.env.DGJ_VERIFY_BUDGET_MS;
|
||
else process.env.DGJ_VERIFY_BUDGET_MS = previous;
|
||
}
|
||
});
|
||
|
||
it('单条校验(checkPlatformFinished)仍按原口径返回', async () => {
|
||
const { service } = buildService({
|
||
fetchPlatformOrder: () => ({ ErpState: 'sended' }),
|
||
});
|
||
|
||
const result = await service.checkPlatformFinished(order(100), index(100), PRINCIPAL_ID);
|
||
|
||
assert.strictEqual(result.applicable, true);
|
||
assert.strictEqual(result.finished, true);
|
||
assert.match(result.reason, /店管家侧已发货或已终结/);
|
||
});
|
||
});
|