fix: 拣货完成扣减库存事务漏洞 - picking查询添加FOR UPDATE行锁
- recheckOrder._completePickForOrder: picking查询添加FOR UPDATE,防止并发重复扣减 - pick_compensation.compensateInTransaction: 同上 - pick_compensation.releaseCancelledInTransaction: 同上 与主路径 wavePickTaskComplete 保持一致的双重行锁防护(picking FOR UPDATE + storage LOCK.UPDATE)
This commit is contained in:
parent
9b1a6034af
commit
b9fe9e774c
@ -3995,13 +3995,15 @@ class RecheckOrderService extends OrderBaseService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// 步骤3:检查是否存在 wms_picking 记录(仅统计 pending 且未删除的记录)
|
// 步骤3:检查是否存在 wms_picking 记录(仅统计 pending 且未删除的记录)
|
||||||
|
// 使用 FOR UPDATE 行锁,防止并发事务读取到相同的 picking 记录导致重复扣减库存
|
||||||
const pickingRows = await ctx.model.query(
|
const pickingRows = await ctx.model.query(
|
||||||
`SELECT id, storage_id, quantity, space_id
|
`SELECT id, storage_id, quantity, space_id
|
||||||
FROM wms_picking
|
FROM wms_picking
|
||||||
WHERE order_id = :orderId
|
WHERE order_id = :orderId
|
||||||
AND warehouse_id = :warehouseId
|
AND warehouse_id = :warehouseId
|
||||||
AND status = 'pending'
|
AND status = 'pending'
|
||||||
AND deleted_at IS NULL`,
|
AND deleted_at IS NULL
|
||||||
|
FOR UPDATE`,
|
||||||
{
|
{
|
||||||
replacements: { orderId, warehouseId },
|
replacements: { orderId, warehouseId },
|
||||||
type: ctx.model.QueryTypes.SELECT,
|
type: ctx.model.QueryTypes.SELECT,
|
||||||
|
|||||||
@ -335,6 +335,7 @@ class PickCompensationService extends MainService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Step 1: 重新查询 wms_picking,确保只处理仍然 status='pending' 的记录
|
// Step 1: 重新查询 wms_picking,确保只处理仍然 status='pending' 的记录
|
||||||
|
// 使用 FOR UPDATE 行锁,防止并发事务读取到相同的 picking 记录导致重复释放锁定库存
|
||||||
const confirmedRows = await ctx.model.query(`
|
const confirmedRows = await ctx.model.query(`
|
||||||
SELECT
|
SELECT
|
||||||
p.id,
|
p.id,
|
||||||
@ -348,6 +349,7 @@ class PickCompensationService extends MainService {
|
|||||||
AND p.storage_id IS NOT NULL
|
AND p.storage_id IS NOT NULL
|
||||||
AND p.storage_id > 0
|
AND p.storage_id > 0
|
||||||
AND p.deleted_at IS NULL -- 排除软删除记录
|
AND p.deleted_at IS NULL -- 排除软删除记录
|
||||||
|
FOR UPDATE
|
||||||
`, {
|
`, {
|
||||||
replacements: { pickingIds, warehouseId },
|
replacements: { pickingIds, warehouseId },
|
||||||
type: ctx.model.QueryTypes.SELECT,
|
type: ctx.model.QueryTypes.SELECT,
|
||||||
@ -474,6 +476,7 @@ class PickCompensationService extends MainService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Step 1: 重新查询 wms_picking,确保只处理仍然 status='pending' 的记录
|
// Step 1: 重新查询 wms_picking,确保只处理仍然 status='pending' 的记录
|
||||||
|
// 使用 FOR UPDATE 行锁,防止并发事务读取到相同的 picking 记录导致重复扣减库存
|
||||||
const confirmedRows = await ctx.model.query(`
|
const confirmedRows = await ctx.model.query(`
|
||||||
SELECT
|
SELECT
|
||||||
p.id,
|
p.id,
|
||||||
@ -487,6 +490,7 @@ class PickCompensationService extends MainService {
|
|||||||
AND p.storage_id IS NOT NULL
|
AND p.storage_id IS NOT NULL
|
||||||
AND p.storage_id > 0
|
AND p.storage_id > 0
|
||||||
AND p.deleted_at IS NULL -- 排除软删除记录
|
AND p.deleted_at IS NULL -- 排除软删除记录
|
||||||
|
FOR UPDATE
|
||||||
`, {
|
`, {
|
||||||
replacements: { pickingIds, warehouseId },
|
replacements: { pickingIds, warehouseId },
|
||||||
type: ctx.model.QueryTypes.SELECT,
|
type: ctx.model.QueryTypes.SELECT,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user