本文介绍一种基于 laravel 的库存扣减策略,当同一商品存在多条库存记录(如来自不同订单)时,按优先级顺序(如 id_order)逐条扣减,确保不超卖且精准分配。
在实际电商或仓储系统中,同一商品(如 id_wine = 1)可能因分批入库、多渠道采购等原因,在数据库中存在多条库存记录,每条对应不同来源(如不同 id_order)和独立可用数量(quantita_restante)。此时若用户一次性下单 5 瓶,而库存分散为 4 + 1,直接对“第一条记录”执行 decrement('quantita_restante', 5) 将导致负库存(如 4 − 5 = −1),违反业务约束。
正确做法是:按预设顺序遍历所有可用库存记录,贪心式逐条扣减,直到订单数量耗尽。Laravel 原生不支持单条 SQL 完成跨行条件递减,因此需结合查询 + 循环 + 事务保障原子性。
以下是推荐实现方案(含事务与边界处理):
use Illuminate\Support\Facades\DB;
$requestedQty = $request->quantita;
$wineId = $wine_id;
$restaurantId = Auth::user()->id_restaurant;
DB::transaction(function () use ($requestedQty, $wineId, $restaurantId) {
// 按 id_order 升序获取所有可用库存(先到先扣)
$stocks = warehouse::where('id_restaurant', $restaurantId)
->where('id_wine', $wineId)
->where('quantita_restante', '>', 0)
->orderBy('id_order')
->get();
$remaining = $requestedQty;
foreach ($stocks as $stock) {
if ($remaining <= 0) break;
// 当前库存可扣减量 = min(剩余需扣量, 当前库存余量)
$toDeduct = min($remaining, $stock->quantita_restante);
// 执行原子扣减(避免并发覆盖)
$affected = warehouse::where('id',
$stock->id)
->where('quantita_restante', '>=', $toDeduct)
->decrement('quantita_restante', $toDeduct);
if ($affected === 0) {
throw new \Exception("库存并发冲突:记录 ID {$stock->id} 余量不足,无法扣减 {$toDeduct}");
}
$remaining -= $toDeduct;
}
if ($remaining > 0) {
throw new \Exception("库存不足:请求 {$requestedQty},仅能分配 " . ($requestedQty - $remaining));
}
});✅ 关键要点说明:
该方案兼顾准确性、健壮性与可维护性,是处理“分布式库存聚合扣减”场景的标准实践。