diff --git a/app/service/systemMethods/order/recheckOrder.js b/app/service/systemMethods/order/recheckOrder.js index 1df67c6..dd34ba5 100644 --- a/app/service/systemMethods/order/recheckOrder.js +++ b/app/service/systemMethods/order/recheckOrder.js @@ -3995,13 +3995,15 @@ class RecheckOrderService extends OrderBaseService { ); // 步骤3:检查是否存在 wms_picking 记录(仅统计 pending 且未删除的记录) + // 使用 FOR UPDATE 行锁,防止并发事务读取到相同的 picking 记录导致重复扣减库存 const pickingRows = await ctx.model.query( `SELECT id, storage_id, quantity, space_id FROM wms_picking WHERE order_id = :orderId AND warehouse_id = :warehouseId AND status = 'pending' - AND deleted_at IS NULL`, + AND deleted_at IS NULL + FOR UPDATE`, { replacements: { orderId, warehouseId }, type: ctx.model.QueryTypes.SELECT, diff --git a/app/service/systems/pick_compensation.js b/app/service/systems/pick_compensation.js index cd5ba78..a6ce4a7 100644 --- a/app/service/systems/pick_compensation.js +++ b/app/service/systems/pick_compensation.js @@ -335,6 +335,7 @@ class PickCompensationService extends MainService { try { // Step 1: 重新查询 wms_picking,确保只处理仍然 status='pending' 的记录 + // 使用 FOR UPDATE 行锁,防止并发事务读取到相同的 picking 记录导致重复释放锁定库存 const confirmedRows = await ctx.model.query(` SELECT p.id, @@ -348,6 +349,7 @@ class PickCompensationService extends MainService { AND p.storage_id IS NOT NULL AND p.storage_id > 0 AND p.deleted_at IS NULL -- 排除软删除记录 + FOR UPDATE `, { replacements: { pickingIds, warehouseId }, type: ctx.model.QueryTypes.SELECT, @@ -474,6 +476,7 @@ class PickCompensationService extends MainService { try { // Step 1: 重新查询 wms_picking,确保只处理仍然 status='pending' 的记录 + // 使用 FOR UPDATE 行锁,防止并发事务读取到相同的 picking 记录导致重复扣减库存 const confirmedRows = await ctx.model.query(` SELECT p.id, @@ -487,6 +490,7 @@ class PickCompensationService extends MainService { AND p.storage_id IS NOT NULL AND p.storage_id > 0 AND p.deleted_at IS NULL -- 排除软删除记录 + FOR UPDATE `, { replacements: { pickingIds, warehouseId }, type: ctx.model.QueryTypes.SELECT,