fix(order): 订单同步/运单/库位分配防御性修复
- express: 运单查询补充 pay_no/from_key 字段;step 更新增加 status!=cancel 过滤 - order_async: 同步分发前过滤 sync_status=fin 的已完成订单;修复 Op.ne 引用缺失;强制 SELECT FOR UPDATE 重读;多处更新增加 status!=cancel 守卫 - order_storage_allocation: SN/快递分配失败回写增加 status!=cancel 守卫 - methods: allocateExpressCode 文档更新为异步接口说明
This commit is contained in:
parent
d6e4de6edc
commit
02b3a5e813
@ -1543,14 +1543,7 @@ class DeliveryOrderService extends Service {
|
||||
buildExpressAllocateRequest(context) {
|
||||
const order = context && context.order ? context.order : {};
|
||||
return {
|
||||
orders: [{
|
||||
order_id: Number(order.id || context.orderId || 0),
|
||||
business_key: 1,
|
||||
sys_platform_code: String(order.sys_platform_code || '').trim(),
|
||||
logistics_code: String(order.logistics_code || '').trim(),
|
||||
store_id: Number(order.store_id || context.storeId || 0),
|
||||
warehouse_id: Number(order.warehouse_id || context.warehouseId || 0),
|
||||
}],
|
||||
ids: [ Number(order.id || context.orderId || 0) ],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -58,6 +58,8 @@ const WAYBILL_ORDER_DELIVERY_MIN_ATTRIBUTES = Object.freeze([
|
||||
'logistics_code',
|
||||
'extend_props',
|
||||
'buyer_nick',
|
||||
'pay_no',
|
||||
'from_key'
|
||||
]);
|
||||
|
||||
class ExpressService extends Service {
|
||||
@ -2460,7 +2462,7 @@ class ExpressService extends Service {
|
||||
[Op.in]: [ 1, 2, 3, 4 ],
|
||||
},
|
||||
},
|
||||
attributes: [ 'id', 'sys_platform_code', 'logistics_code', 'sys_platform_name', 'logistics_name', 'store_id' ],
|
||||
attributes: [ 'id', 'sys_platform_code', 'logistics_code', 'sys_platform_name', 'logistics_name', 'store_id','pay_no','from_key' ],
|
||||
raw: true,
|
||||
});
|
||||
|
||||
@ -2481,7 +2483,7 @@ class ExpressService extends Service {
|
||||
[Op.in]: [ 4 ],
|
||||
},
|
||||
},
|
||||
attributes: [ 'id', 'sys_platform_code', 'logistics_code', 'sys_platform_name', 'logistics_name', 'store_id' ],
|
||||
attributes: [ 'id', 'sys_platform_code', 'logistics_code', 'sys_platform_name', 'logistics_name', 'store_id','pay_no','from_key' ],
|
||||
raw: true,
|
||||
});
|
||||
}
|
||||
@ -3106,6 +3108,9 @@ class ExpressService extends Service {
|
||||
id: {
|
||||
[Op.in]: stepUpdatableIds,
|
||||
},
|
||||
status: {
|
||||
[Op.ne]: 'cancel',
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
@ -706,7 +706,7 @@ class ExpressPlatformService extends MainService {
|
||||
let playload = {
|
||||
object_Id: `${order.warehouse_id}_${order.id}_${order_tmp.business_key}`, // 菠萝派平台要求订单唯一标识必须为字符串
|
||||
orderno: `${order.warehouse_id}_${order.id}_${order_tmp.business_key}`, // 菠萝派平台要求订单号必须为字符串
|
||||
plattradeno: order.sys_platform_code === 'DY'? order.source_order_codes.split(",")[0] : order.source_order_codes || order.delivery_order_code || '',
|
||||
plattradeno: listByWarehousePlatformCode.platform_code === 'DY' && order.from_key == 23060081? order.pay_no : order.source_order_codes || order.delivery_order_code || '',
|
||||
ismultiplepieces: order.package_count > 1 ? 1 : 0,
|
||||
logisticno: allocatedWaybillMap.get(`${Number(order.id) || 0}:${Number(order_tmp.business_key) || 0}`) || '',
|
||||
logistictype: order.logistics_code || '',
|
||||
|
||||
@ -402,6 +402,11 @@ async function syncManualOrdersInternal(service, groupedOrder = {}, context = {}
|
||||
const orderMap = new Map();
|
||||
for (const orderRow of Array.isArray(orderRows) ? orderRows : []) {
|
||||
if (orderRow && Number(orderRow.id) > 0) {
|
||||
// 分发前过滤:已同步完成的订单不再派发
|
||||
if (String(orderRow.sync_status || '').trim() === 'fin') {
|
||||
service.ctx.logger.info(`[syncManualOrders] 订单已完成同步,跳过派发 orderId=${orderRow.id}`);
|
||||
continue;
|
||||
}
|
||||
orderMap.set(Number(orderRow.id), orderRow);
|
||||
}
|
||||
}
|
||||
@ -413,6 +418,10 @@ async function syncManualOrdersInternal(service, groupedOrder = {}, context = {}
|
||||
for (let i = 0; i < orderIds.length; i += DISPATCH_BATCH_SIZE) {
|
||||
const batch = orderIds.slice(i, i + DISPATCH_BATCH_SIZE);
|
||||
const batchResults = await Promise.all(batch.map(orderId => {
|
||||
const dispathedOrderRow = orderMap.get(orderId);
|
||||
if (!dispathedOrderRow) {
|
||||
return { order_id: orderId, status: 'skipped', message: '订单已完成同步,跳过' };
|
||||
}
|
||||
return processManualOrderInternal(
|
||||
service,
|
||||
orderModel,
|
||||
@ -484,7 +493,7 @@ async function markPendingExceptionInternal(service, orderModel, orderId, wareho
|
||||
id: orderId,
|
||||
warehouse_id: warehouseId,
|
||||
status: {
|
||||
[Op.ne]: 'cancel',
|
||||
[service.app.Sequelize.Op.ne]: 'cancel',
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -509,7 +518,8 @@ async function finalizeSuccessfulQimenSyncInternal(service, orderModel, orderIte
|
||||
|
||||
await runTransactionWithLockRetryInternal(service, 'finalizeSuccessfulQimenSync', async transaction => {
|
||||
let resolvedOrderRow = orderRow;
|
||||
if (!resolvedOrderRow && orderModel && typeof orderModel.findOne === 'function') {
|
||||
// 最小修复:始终执行 SELECT ... FOR UPDATE,不再因 orderRow 已传入而跳过
|
||||
if (orderModel && typeof orderModel.findOne === 'function') {
|
||||
resolvedOrderRow = await orderModel.findOne({
|
||||
where: {
|
||||
id: orderId,
|
||||
@ -569,6 +579,9 @@ async function finalizeSuccessfulQimenSyncInternal(service, orderModel, orderIte
|
||||
where: {
|
||||
id: orderId,
|
||||
warehouse_id: warehouseId,
|
||||
status: {
|
||||
[service.app.Sequelize.Op.ne]: 'cancel',
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
@ -642,6 +655,9 @@ async function processLocalChannelOrderInternal(service, createChannel, orderMod
|
||||
where: {
|
||||
id: orderId,
|
||||
warehouse_id: warehouseId,
|
||||
status: {
|
||||
[service.app.Sequelize.Op.ne]: 'cancel',
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
@ -815,6 +831,11 @@ async function finalizeLocalChannelOrdersInternal(service, createChannel, groupe
|
||||
const orderMap = new Map();
|
||||
for (const orderRow of Array.isArray(orderRows) ? orderRows : []) {
|
||||
if (orderRow && Number(orderRow.id) > 0) {
|
||||
// 分发前过滤:已同步完成的订单不再派发
|
||||
if (String(orderRow.sync_status || '').trim() === 'fin') {
|
||||
service.ctx.logger.info(`[syncApiOrders] 订单已完成同步,跳过派发 orderId=${orderRow.id}`);
|
||||
continue;
|
||||
}
|
||||
orderMap.set(Number(orderRow.id), orderRow);
|
||||
}
|
||||
}
|
||||
@ -826,6 +847,10 @@ async function finalizeLocalChannelOrdersInternal(service, createChannel, groupe
|
||||
for (let i = 0; i < orderIds.length; i += DISPATCH_BATCH_SIZE) {
|
||||
const batch = orderIds.slice(i, i + DISPATCH_BATCH_SIZE);
|
||||
const batchResults = await Promise.all(batch.map(orderId => {
|
||||
const dispathedOrderRow = orderMap.get(orderId);
|
||||
if (!dispathedOrderRow) {
|
||||
return { order_id: orderId, status: 'skipped', message: '订单已完成同步,跳过' };
|
||||
}
|
||||
return processLocalChannelOrderInternal(
|
||||
service,
|
||||
createChannel,
|
||||
@ -1156,6 +1181,11 @@ async function syncQmOrdersInternal(service, groupedOrder = {}, context = {}) {
|
||||
const orderMap = new Map();
|
||||
for (const orderRow of Array.isArray(orderRows) ? orderRows : []) {
|
||||
if (orderRow && Number(orderRow.id) > 0) {
|
||||
// 分发前过滤:已同步完成的订单不再派发,避免重复同步覆盖 error 等异常状态
|
||||
if (String(orderRow.sync_status || '').trim() === 'fin') {
|
||||
service.ctx.logger.info(`[syncQmOrders] 订单已完成同步,跳过派发 orderId=${orderRow.id}`);
|
||||
continue;
|
||||
}
|
||||
orderMap.set(Number(orderRow.id), orderRow);
|
||||
}
|
||||
}
|
||||
@ -1182,6 +1212,12 @@ async function syncQmOrdersInternal(service, groupedOrder = {}, context = {}) {
|
||||
for (let i = 0; i < orderIds.length; i += DISPATCH_BATCH_SIZE) {
|
||||
const batch = orderIds.slice(i, i + DISPATCH_BATCH_SIZE);
|
||||
const batchResults = await Promise.all(batch.map(orderId => {
|
||||
// 分发时二次检查:orderRow 不在 map 中则跳过
|
||||
const dispathedOrderRow = orderMap.get(orderId);
|
||||
if (!dispathedOrderRow) {
|
||||
service.ctx.logger.info(`[syncQmOrders] 订单已过滤,跳过 orderId=${orderId}`);
|
||||
return { order_id: orderId, status: 'skipped', message: '订单已完成同步,跳过' };
|
||||
}
|
||||
return processQmOrderInternal(
|
||||
service,
|
||||
orderModel,
|
||||
@ -1189,7 +1225,7 @@ async function syncQmOrdersInternal(service, groupedOrder = {}, context = {}) {
|
||||
qimenClient,
|
||||
orderId,
|
||||
warehouseId,
|
||||
orderMap.get(orderId),
|
||||
dispathedOrderRow,
|
||||
itemsByOrderId.get(orderId) || [],
|
||||
context,
|
||||
waybillCodeMap.get(orderId)
|
||||
|
||||
@ -1328,6 +1328,9 @@ class OrderStorageAllocationService extends MainService {
|
||||
where: {
|
||||
id: orderId,
|
||||
warehouse_id: warehouseId,
|
||||
status: {
|
||||
[ctx.app.Sequelize.Op.ne]: 'cancel',
|
||||
},
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
@ -1831,7 +1834,11 @@ class OrderStorageAllocationService extends MainService {
|
||||
order_flag: snBatchForbiddenReason,
|
||||
updated_at: now,
|
||||
}, {
|
||||
where: { id: { [ctx.app.Sequelize.Op.in]: snFailIds }, warehouse_id: warehouseId },
|
||||
where: {
|
||||
id: { [ctx.app.Sequelize.Op.in]: snFailIds },
|
||||
warehouse_id: warehouseId,
|
||||
status: { [ctx.app.Sequelize.Op.ne]: 'cancel' },
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
@ -1845,7 +1852,11 @@ class OrderStorageAllocationService extends MainService {
|
||||
order_flag: fastFailReason,
|
||||
updated_at: now,
|
||||
}, {
|
||||
where: { id: { [ctx.app.Sequelize.Op.in]: expressFailIds }, warehouse_id: warehouseId },
|
||||
where: {
|
||||
id: { [ctx.app.Sequelize.Op.in]: expressFailIds },
|
||||
warehouse_id: warehouseId,
|
||||
status: { [ctx.app.Sequelize.Op.ne]: 'cancel' },
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
|
||||
@ -1907,14 +1907,14 @@
|
||||
{
|
||||
"version": "2026.04.18-system-02",
|
||||
"date": "2026-04-18",
|
||||
"summary": "新增 order.pendingOrder.allocateExpressCode:待处理订单模块的批量分配快递号入口,复用 express.createWaybillCodePipeline。",
|
||||
"summary": "新增 order.pendingOrder.allocateExpressCode:待处理订单模块的批量分配首单快递号(异步接口),复用 express.createWaybillCodePipeline。",
|
||||
"methods": [
|
||||
"order.pendingOrder.allocateExpressCode"
|
||||
],
|
||||
"notes": [
|
||||
"该接口直接接收订单对象数组入参;订单对象应为来自待处理订单列表或详情的原始订单对象。每个订单对象必须包含 order_id 与 business_key;sys_platform_code、logistics_code 为订单对象自带字段,前端无需额外拼装或单独构造,直接原样回传订单对象即可。",
|
||||
"实际的运单创建、归一化与跳过逻辑由 express.createWaybillCodePipeline 处理。",
|
||||
"接口返回值与现有分配链路一致,已用统一 success 包装返回。"
|
||||
"异步接口:调用后立即返回 task_id,后台执行运单创建,通过 allocateExpressCodeTaskProgress 查询进度。",
|
||||
"入参为 ids(订单 ID 数组),sys_platform_code/logistics_code 等由后端从数据库读取。",
|
||||
"拆单场景请使用 order.pendingOrder.allocateSubOrderNumber。"
|
||||
],
|
||||
"type": "added"
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user