fix(order): 推单跨分表单号查重
delivery_order_code 查重由仅查 current 分表扩展为 current/dispatch/history 三表并发查重,命中任一表即拒绝重复建单;dispatch/history 模型获取可能执行 DDL(隐式提交),已移至事务外完成。
This commit is contained in:
parent
ce04778021
commit
ce3e0185e1
@ -711,7 +711,10 @@ class DeliveryOrderService extends Service {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
info = { ...info, warehouseId: warehouseIdStr };
|
info = { ...info, warehouseId: warehouseIdStr };
|
||||||
|
// 跨分表单号查重模型:dispatch/history 模型获取可能执行 DDL(隐式提交),必须在事务外完成
|
||||||
const DeliveryModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseIdStr);
|
const DeliveryModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseIdStr);
|
||||||
|
const DeliveryDispatchModel = await ctx.model.OrderDeliveryFactory.getDispatchModel(warehouseIdStr);
|
||||||
|
const DeliveryHistoryModel = await ctx.model.OrderDeliveryFactory.getHistoryModel(warehouseIdStr);
|
||||||
const { deliveryOrder, orderLines } = data.request;
|
const { deliveryOrder, orderLines } = data.request;
|
||||||
|
|
||||||
// 请求级 extendProps,携带 ewaybill_order_code 等关键字段
|
// 请求级 extendProps,携带 ewaybill_order_code 等关键字段
|
||||||
@ -759,22 +762,44 @@ class DeliveryOrderService extends Service {
|
|||||||
throw new Error('订单信息不完整');
|
throw new Error('订单信息不完整');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查订单是否已存在(根据唯一索引)
|
// 检查订单是否已存在:依次查 current/dispatch/history 分表
|
||||||
const existingOrder = await DeliveryModel.findOne({
|
// (各分表唯一索引均为 单号+货主+仓库,跨表间约束独立,必须逐表查重)
|
||||||
where: {
|
const codeDuplicateWhere = {
|
||||||
delivery_order_code: deliveryOrder.deliveryOrderCode,
|
delivery_order_code: deliveryOrder.deliveryOrderCode,
|
||||||
store_id: info.storeId,
|
store_id: info.storeId,
|
||||||
warehouse_id: info.warehouseId,
|
warehouse_id: info.warehouseId,
|
||||||
},
|
};
|
||||||
attributes: [ 'id' ],
|
const [ existingOrder, existingDispatchOrder, existingHistoryOrder ] = await Promise.all([
|
||||||
transaction,
|
DeliveryModel.findOne({
|
||||||
});
|
where: codeDuplicateWhere,
|
||||||
if (existingOrder) {
|
attributes: [ 'id' ],
|
||||||
|
transaction,
|
||||||
|
}),
|
||||||
|
DeliveryDispatchModel.findOne({
|
||||||
|
where: codeDuplicateWhere,
|
||||||
|
attributes: [ 'id' ],
|
||||||
|
transaction,
|
||||||
|
}),
|
||||||
|
DeliveryHistoryModel.findOne({
|
||||||
|
where: codeDuplicateWhere,
|
||||||
|
attributes: [ 'id' ],
|
||||||
|
transaction,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const duplicateOrder = existingOrder || existingDispatchOrder || existingHistoryOrder;
|
||||||
|
if (duplicateOrder) {
|
||||||
|
const duplicateLocation = existingOrder
|
||||||
|
? 'current'
|
||||||
|
: (existingDispatchOrder ? 'dispatch' : 'history');
|
||||||
|
ctx.logger.warn(
|
||||||
|
`[DeliveryOrderService.addOrder] 跨分表单号重复拒绝: ${deliveryOrder.deliveryOrderCode}, 命中的表: ${duplicateLocation}, 已存在订单 id: ${duplicateOrder.id}`
|
||||||
|
);
|
||||||
await transaction.rollback();
|
await transaction.rollback();
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: '订单已存在',
|
message: '订单已存在',
|
||||||
data: { deliveryOrderId: existingOrder.id },
|
data: { deliveryOrderId: duplicateOrder.id },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user