修改解锁库存接口
This commit is contained in:
parent
cd360c8866
commit
58fa463934
@ -3267,15 +3267,15 @@ func (s *ProcessService) lockInventory(tx *gorm.DB, warehouseID, productID, quan
|
|||||||
func (s *ProcessService) unlockInventory(tx *gorm.DB, warehouseID, productID, quantity int64, now int64) error {
|
func (s *ProcessService) unlockInventory(tx *gorm.DB, warehouseID, productID, quantity int64, now int64) error {
|
||||||
var inventories []models.Inventory
|
var inventories []models.Inventory
|
||||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||||
Where("warehouse_id = ? AND product_id = ? AND is_del = 0 AND (locked_quantity > 0 or quality > 0)",
|
Where("product_id = ? AND is_del = 0 AND locked_quantity > 0",
|
||||||
warehouseID, productID).
|
productID).
|
||||||
Order("created_at DESC").
|
Order("created_at DESC").
|
||||||
Find(&inventories).Error; err != nil {
|
Find(&inventories).Error; err != nil {
|
||||||
return fmt.Errorf("查询锁定库存失败: %v", err)
|
return fmt.Errorf("查询锁定库存失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(inventories) == 0 {
|
if len(inventories) == 0 {
|
||||||
return fmt.Errorf("商品ID=%d在仓库ID=%d中无锁定库存", productID, warehouseID)
|
return fmt.Errorf("商品ID=%d无锁定库存", productID)
|
||||||
}
|
}
|
||||||
|
|
||||||
remainingUnlock := quantity
|
remainingUnlock := quantity
|
||||||
@ -3304,48 +3304,6 @@ func (s *ProcessService) unlockInventory(tx *gorm.DB, warehouseID, productID, qu
|
|||||||
return fmt.Errorf("锁定库存已被其他事务修改,请重试")
|
return fmt.Errorf("锁定库存已被其他事务修改,请重试")
|
||||||
}
|
}
|
||||||
|
|
||||||
var inventoryDetails []models.InventoryDetail
|
|
||||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
|
||||||
Where("warehouse_id = ? AND product_id = ? AND is_del = 0 AND locked_quantity > 0",
|
|
||||||
warehouseID, productID).
|
|
||||||
Order("created_at DESC").
|
|
||||||
Find(&inventoryDetails).Error; err != nil {
|
|
||||||
return fmt.Errorf("查询库存明细失败: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
detailRemainingUnlock := unlockQty
|
|
||||||
for j := range inventoryDetails {
|
|
||||||
if detailRemainingUnlock <= 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
detailUnlockQty := inventoryDetails[j].LockedQuantity
|
|
||||||
if detailUnlockQty > detailRemainingUnlock {
|
|
||||||
detailUnlockQty = detailRemainingUnlock
|
|
||||||
}
|
|
||||||
|
|
||||||
detailResult := tx.Model(&inventoryDetails[j]).
|
|
||||||
Where("locked_quantity >= ?", detailUnlockQty).
|
|
||||||
UpdateColumns(map[string]interface{}{
|
|
||||||
"locked_quantity": gorm.Expr("locked_quantity - ?", detailUnlockQty),
|
|
||||||
"updated_at": now,
|
|
||||||
})
|
|
||||||
|
|
||||||
if detailResult.Error != nil {
|
|
||||||
return fmt.Errorf("解锁库存明细失败: %v", detailResult.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
if detailResult.RowsAffected == 0 {
|
|
||||||
return fmt.Errorf("库存明细锁定数量已被其他事务修改,请重试")
|
|
||||||
}
|
|
||||||
|
|
||||||
detailRemainingUnlock -= detailUnlockQty
|
|
||||||
}
|
|
||||||
|
|
||||||
if detailRemainingUnlock > 0 {
|
|
||||||
return fmt.Errorf("库存明细锁定数量不足,还需解锁:%d", detailRemainingUnlock)
|
|
||||||
}
|
|
||||||
|
|
||||||
remainingUnlock -= unlockQty
|
remainingUnlock -= unlockQty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user