fix(order): 已取消订单跳过分配储位与分配快递
- order_storage_allocation: allocatePicking 过滤循环校验 status=cancel,已取消订单跳过并记 skipped_reason=order_cancelled,不再锁库存写 picking - express: createWaybillCodePipeline step1 的 current/dispatch 查询排除 status=cancel,createWaybillCode 事务内再次剔除取消订单(不标记异常、不写失败日志、不取号) - 新增回归测试:分配储位跳过取消订单、分配快递流水线跳过取消订单
This commit is contained in:
parent
7ad08e488d
commit
d0621dd4ec
@ -2510,6 +2510,9 @@ class ExpressService extends Service {
|
||||
step: {
|
||||
[Op.in]: [ 1, 2, 3, 4 ],
|
||||
},
|
||||
status: {
|
||||
[Op.ne]: 'cancel',
|
||||
},
|
||||
},
|
||||
attributes: [ 'id', 'sys_platform_code', 'logistics_code', 'sys_platform_name', 'logistics_name', 'store_id', 'pay_no', 'from_key' ],
|
||||
raw: true,
|
||||
@ -2531,6 +2534,9 @@ class ExpressService extends Service {
|
||||
step: {
|
||||
[Op.in]: [ 4 ],
|
||||
},
|
||||
status: {
|
||||
[Op.ne]: 'cancel',
|
||||
},
|
||||
},
|
||||
attributes: [ 'id', 'sys_platform_code', 'logistics_code', 'sys_platform_name', 'logistics_name', 'store_id', 'pay_no', 'from_key' ],
|
||||
raw: true,
|
||||
@ -3025,10 +3031,33 @@ class ExpressService extends Service {
|
||||
};
|
||||
}
|
||||
|
||||
// 跳过已取消订单:已取消订单不允许取号,且不标记异常、不写失败日志。
|
||||
const cancelledOrderIdSet = new Set(
|
||||
allOrderDeliveryRows
|
||||
.filter(row => String(row.status || '').trim() === 'cancel')
|
||||
.map(row => Number(row.id))
|
||||
.filter(id => Number.isFinite(id) && id > 0)
|
||||
);
|
||||
let effectivePendingOrders = pendingOrders;
|
||||
if (cancelledOrderIdSet.size > 0) {
|
||||
effectivePendingOrders = pendingOrders.filter(order => !cancelledOrderIdSet.has(Number(order.order_id)));
|
||||
ctx.logger.info('[ExpressService.createWaybillCode] 跳过已取消订单取号', {
|
||||
traceId,
|
||||
skippedOrderIds: Array.from(cancelledOrderIdSet),
|
||||
});
|
||||
}
|
||||
|
||||
if (effectivePendingOrders.length === 0) {
|
||||
return {
|
||||
orders: [],
|
||||
waybill_codes: [],
|
||||
};
|
||||
}
|
||||
|
||||
const {
|
||||
relationGroupMap,
|
||||
missingOrderIds,
|
||||
} = this.buildWaybillCreateRelationGroups(pendingOrders, allOrderDeliveryRows, currentWarehouseId);
|
||||
} = this.buildWaybillCreateRelationGroups(effectivePendingOrders, allOrderDeliveryRows, currentWarehouseId);
|
||||
|
||||
const relationWhereList = Array.from(relationGroupMap.values()).map(group => ({
|
||||
store_id: group.store_id,
|
||||
@ -3083,7 +3112,7 @@ class ExpressService extends Service {
|
||||
ctx.logger.warn('[ExpressService.createWaybillCode] 余额不足但允许超额分配: %s', balanceWarnings.join(';'));
|
||||
}
|
||||
|
||||
const successOrders = pendingOrders.filter(order => successOrderKeySet.has(`${order.order_id}:${order.business_key}`));
|
||||
const successOrders = effectivePendingOrders.filter(order => successOrderKeySet.has(`${order.order_id}:${order.business_key}`));
|
||||
const existingRows = await orderExpressModel.findAll({
|
||||
where: {
|
||||
[Op.or]: successOrders.map(order => ({
|
||||
|
||||
@ -4,6 +4,7 @@ const MainService = require('../main');
|
||||
|
||||
const STORAGE_STATUS_FIN = 'fin';
|
||||
const STORAGE_STATUS_ERROR = 'error';
|
||||
const ORDER_STATUS_CANCEL = 'cancel';
|
||||
const ORDER_STEP_EXCEPTION = 3;
|
||||
const ALLOCATION_LOG_SAMPLE_LIMIT = 20;
|
||||
|
||||
@ -613,6 +614,20 @@ class OrderStorageAllocationService extends MainService {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (String(orderRow.status || '').trim() === ORDER_STATUS_CANCEL) {
|
||||
skippedOrders.push({
|
||||
order_id: orderId,
|
||||
status: 'skipped',
|
||||
order_status: orderRow.status,
|
||||
skipped_reason: 'order_cancelled',
|
||||
});
|
||||
writeAllocationTextLog('[OrderStorageAllocationService.allocatePicking] 过滤订单结果', {
|
||||
orderId,
|
||||
skippedReason: 'order_cancelled',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
if (String(orderRow.storage_status || 'none') === STORAGE_STATUS_FIN) {
|
||||
skippedOrders.push({
|
||||
order_id: orderId,
|
||||
|
||||
@ -0,0 +1,217 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const ExpressService = require('../../../../app/service/systems/express');
|
||||
|
||||
/**
|
||||
* 创建 createWaybillCodePipeline 跳过取消订单的测试上下文。
|
||||
* 模拟数据库行为:current/dispatch 订单查询按 where(含 status != 'cancel')过滤。
|
||||
*
|
||||
* @param {Object} [options={}] 测试覆盖配置。
|
||||
* @return {{ ctx: Object, calls: Object }} 测试上下文与调用记录。
|
||||
*/
|
||||
function createPipelineSkipCancelledHarness(options = {}) {
|
||||
const calls = {
|
||||
orderDeliveryFindAllQueries: [],
|
||||
dispatchFindAllQueries: [],
|
||||
asyncTaskPayloads: [],
|
||||
};
|
||||
|
||||
const orderDeliveryRows = Array.isArray(options.orderDeliveryRows) ? options.orderDeliveryRows : [];
|
||||
const dispatchRows = Array.isArray(options.dispatchRows) ? options.dispatchRows : [];
|
||||
|
||||
const matchWhere = (row, where = {}) => {
|
||||
const idValues = Array.isArray(where.id)
|
||||
? where.id
|
||||
: (where.id && Array.isArray(where.id.in) ? where.id.in : []);
|
||||
if (idValues.length > 0 && !idValues.includes(Number(row.id))) {
|
||||
return false;
|
||||
}
|
||||
const stepValues = Array.isArray(where.step)
|
||||
? where.step
|
||||
: (where.step && Array.isArray(where.step.in) ? where.step.in : []);
|
||||
if (stepValues.length > 0 && !stepValues.includes(Number(row.step))) {
|
||||
return false;
|
||||
}
|
||||
const excludedStatus = where.status && where.status.ne ? String(where.status.ne).trim() : '';
|
||||
if (excludedStatus && String(row.status || '').trim() === excludedStatus) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const ctx = {
|
||||
app: {
|
||||
Sequelize: {
|
||||
Op: {
|
||||
in: 'in',
|
||||
ne: 'ne',
|
||||
},
|
||||
},
|
||||
},
|
||||
user: {
|
||||
store_id: 100000,
|
||||
},
|
||||
model: {
|
||||
OrderDeliveryFactory: {
|
||||
async getCurrentModel() {
|
||||
return {
|
||||
async findAll(query = {}) {
|
||||
calls.orderDeliveryFindAllQueries.push(query);
|
||||
return orderDeliveryRows.filter(row => matchWhere(row, query.where)).map(row => ({ ...row }));
|
||||
},
|
||||
async update() {
|
||||
return [ 1 ];
|
||||
},
|
||||
};
|
||||
},
|
||||
async getDispatchModel() {
|
||||
return {
|
||||
async findAll(query = {}) {
|
||||
calls.dispatchFindAllQueries.push(query);
|
||||
return dispatchRows.filter(row => matchWhere(row, query.where)).map(row => ({ ...row }));
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
PrintPlatform: {
|
||||
async findAll() {
|
||||
return [
|
||||
{
|
||||
id: 901,
|
||||
platform_code: 'PDD_PRINT',
|
||||
request_platform_code: 'OPEN_API',
|
||||
},
|
||||
];
|
||||
},
|
||||
},
|
||||
},
|
||||
service: {
|
||||
systems: {
|
||||
platform: {
|
||||
async getPlatformList() {
|
||||
return [
|
||||
{
|
||||
code: 'PDD',
|
||||
print_platform_id: 901,
|
||||
default_express_interface: 'POLYAPI',
|
||||
},
|
||||
];
|
||||
},
|
||||
},
|
||||
setting: {
|
||||
expressInterface: {
|
||||
async listByWarehousePlatformCode() {
|
||||
return {
|
||||
data: [
|
||||
{
|
||||
id: 22,
|
||||
sys_express_code: 'SF',
|
||||
express_code: 'SF',
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
logger: {
|
||||
info() {},
|
||||
warn() {},
|
||||
error() {},
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
ctx,
|
||||
calls,
|
||||
};
|
||||
}
|
||||
|
||||
describe('test/app/service/systems/express.createWaybillCodePipeline.skipCancelled.test.js', () => {
|
||||
it('should exclude cancelled orders via status filter in current and dispatch queries', async () => {
|
||||
const { ctx, calls } = createPipelineSkipCancelledHarness({
|
||||
orderDeliveryRows: [
|
||||
{
|
||||
id: 1,
|
||||
warehouse_id: 100000,
|
||||
status: 'cancel',
|
||||
step: 4,
|
||||
sys_platform_code: 'PDD',
|
||||
logistics_code: 'SF',
|
||||
sys_platform_name: '拼多多',
|
||||
logistics_name: '顺丰',
|
||||
store_id: 100000,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
warehouse_id: 100000,
|
||||
status: 'created',
|
||||
step: 2,
|
||||
sys_platform_code: 'PDD',
|
||||
logistics_code: 'SF',
|
||||
sys_platform_name: '拼多多',
|
||||
logistics_name: '顺丰',
|
||||
store_id: 100000,
|
||||
},
|
||||
],
|
||||
});
|
||||
const service = new ExpressService(ctx);
|
||||
service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => {
|
||||
calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig });
|
||||
};
|
||||
|
||||
const result = await service.createWaybillCodePipeline([
|
||||
{ order_id: 1, business_key: 1 },
|
||||
{ order_id: 2, business_key: 1 },
|
||||
]);
|
||||
|
||||
assert.deepEqual(result, { message: '分配快递任务已提交' });
|
||||
|
||||
// current 表查询必须携带 status != 'cancel' 过滤
|
||||
const currentQuery = calls.orderDeliveryFindAllQueries[0];
|
||||
assert.deepEqual(currentQuery.where.status, { ne: 'cancel' });
|
||||
assert.deepEqual(currentQuery.where.step, { in: [ 1, 2, 3, 4 ] });
|
||||
|
||||
// dispatch 补查同样必须携带 status != 'cancel' 过滤
|
||||
assert.equal(calls.dispatchFindAllQueries.length, 1);
|
||||
assert.deepEqual(calls.dispatchFindAllQueries[0].where.status, { ne: 'cancel' });
|
||||
|
||||
// 只有正常订单进入取号任务批次,取消订单被跳过
|
||||
assert.equal(calls.asyncTaskPayloads.length, 1);
|
||||
assert.deepEqual(
|
||||
calls.asyncTaskPayloads[0].batch.map(order => Number(order.id)),
|
||||
[ 2 ]
|
||||
);
|
||||
});
|
||||
|
||||
it('should not submit any waybill task when all orders are cancelled', async () => {
|
||||
const { ctx, calls } = createPipelineSkipCancelledHarness({
|
||||
orderDeliveryRows: [
|
||||
{
|
||||
id: 1,
|
||||
warehouse_id: 100000,
|
||||
status: 'cancel',
|
||||
step: 4,
|
||||
sys_platform_code: 'PDD',
|
||||
logistics_code: 'SF',
|
||||
sys_platform_name: '拼多多',
|
||||
logistics_name: '顺丰',
|
||||
store_id: 100000,
|
||||
},
|
||||
],
|
||||
});
|
||||
const service = new ExpressService(ctx);
|
||||
service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => {
|
||||
calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig });
|
||||
};
|
||||
|
||||
const result = await service.createWaybillCodePipeline([
|
||||
{ order_id: 1, business_key: 1 },
|
||||
]);
|
||||
|
||||
assert.deepEqual(result, { message: '所有订单均已分配运单号,无需处理' });
|
||||
assert.equal(calls.asyncTaskPayloads.length, 0);
|
||||
});
|
||||
});
|
||||
@ -540,6 +540,64 @@ describe('test/app/service/systems/order_storage_allocation.test.js', () => {
|
||||
assert.equal(orders[1].storage_status, 'fin');
|
||||
});
|
||||
|
||||
it('should skip cancelled orders when allocating storage', async () => {
|
||||
const { service, captures, orders } = createHarness({
|
||||
orders: [
|
||||
{ id: 31, warehouse_id: 100000, storage_status: 'none', order_flag: '', step: 4, status: 'cancel' },
|
||||
{ id: 32, warehouse_id: 100000, storage_status: 'fin', order_flag: '', step: 4, status: 'cancel' },
|
||||
{ id: 33, warehouse_id: 100000, storage_status: 'none', order_flag: '', step: 2, status: 'created' },
|
||||
],
|
||||
orderItems: [
|
||||
{ id: 331, order_id: 33, stock_id: 3311, plan_qty: 1, order_line_no: '1' },
|
||||
],
|
||||
stockRows: [
|
||||
{ id: 3311, item_code: 'SKU-3311', item_name: '测试商品3311', warehouse_id: 100000, deleted_at: null, is_valid: 'Y', locked_quantity: 0 },
|
||||
],
|
||||
storageRows: [
|
||||
{
|
||||
id: 33301,
|
||||
warehouse_id: 100000,
|
||||
stock_id: 3311,
|
||||
is_enabled: 1,
|
||||
delete_at: null,
|
||||
available_quantity: 1,
|
||||
quantity: 1,
|
||||
locked_quantity: 0,
|
||||
production_date: null,
|
||||
inbound_time: '2026-04-15 08:00:00',
|
||||
space_id: 3501,
|
||||
space: {
|
||||
id: 3501,
|
||||
space_code: 'ST-331',
|
||||
custom_name: '存储位331',
|
||||
is_preferred_pick: false,
|
||||
locationType: {
|
||||
id: 1,
|
||||
allow_allocation: 1,
|
||||
is_active: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = await service.allocateStorage({
|
||||
allow_storage_location: true,
|
||||
order_ids: [ 31, 32, 33 ],
|
||||
});
|
||||
|
||||
assert.equal(result.code, 200);
|
||||
assert.deepEqual(result.data.pending_order_ids, [ 33 ]);
|
||||
assert.deepEqual(result.data.allocated_order_ids, [ 33 ]);
|
||||
|
||||
const cancelledSkips = result.data.skipped_orders.filter(item => item.skipped_reason === 'order_cancelled');
|
||||
assert.equal(cancelledSkips.length, 2);
|
||||
assert.deepEqual(cancelledSkips.map(item => item.order_id), [ 31, 32 ]);
|
||||
assert.equal(captures.pickingRows.length, 1);
|
||||
assert.equal(captures.pickingRows[0].order_id, 33);
|
||||
assert.equal(orders[2].storage_status, 'fin');
|
||||
});
|
||||
|
||||
it('should group syncable orders by create_channel and mark them pending sync', async () => {
|
||||
const { service, captures } = createHarness({
|
||||
orders: [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user