如何在数据库中按顺序扣减库存以满足批量订单需求
技术百科
霞舞
发布时间:2026-01-21
浏览: 次 本文介绍一种基于 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));
}
});✅ 关键要点说明:
- 顺序性保障:orderBy('id_order') 确保按入库/订单时间等业务逻辑确定扣减优先级;
- 防超卖:每条记录扣减前校验 quantita_restante >= toDeduct(通过 where() 条件实现乐观锁);
- 事务安全:整个扣减过程包裹在 DB::transaction() 中,失败则全部回滚;
- 性能提示:若库存记录极多,可加索引 INDEX (id_restaurant, id_wine, id_order, quantita_restante) 提升查询效率;
- 扩展建议:可将此逻辑封装为仓库模型的静态方法(如 warehouse::reserve($wineId, $qty)),提升复用性。
该方案兼顾准确性、健壮性与可维护性,是处理“分布式库存聚合扣减”场景的标准实践。
# ai
# 数据库中
# 仅能
# 每条
# 不支持
# win
# 循环
# 数据库
# 封装
# cad
# 遍历
# sql
# laravel
# 将此
# 多条
# 升序
# 分布式
# 第一条
# 先到
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- PythonFastAPI项目实战教程_API接口
- php怎么下载安装并配置环境变量_命令行调用PHP
- 如何在Golang中实现微服务服务拆分_Golan
- Mac如何查看电池健康百分比_Mac系统信息电源检
- Win11怎么忘记WiFi网络_Win11删除已保
- Win11怎么开启专注模式_Windows11时钟
- 如何使用Golang实现跨域请求支持_Golang
- Windows蓝屏错误0x0000002C怎么解决
- Win11如何设置ipv6 Win11开启IPv6
- Python路径拼接规范_跨平台处理说明【指导】
- php本地部署支持nodejs吗_php与node
- Win11怎么解压RAR文件 Win11自带解压功
- mac本地php环境如何开启curl_curl扩展
- Python解释执行模型_字节码流程说明【指导】
- Windows11如何设置专注助手_Windows
- Win11相机打不开提示错误怎么修_相机权限开启与
- Mac的“预览”如何合并多个PDF_Mac文件处理
- Win11如何设置鼠标灵敏度_Win11鼠标灵敏度
- c++获取当前时间戳_c++ time函数使用详解
- Mac如何与安卓手机传文件_Mac和Android
- 如何使用Golang实现负载均衡_分发请求到多个服
- Windows如何查看和管理已安装的字体?(字体文
- mac怎么右键_MAC鼠标右键设置与触控板手势技巧
- php本地部署后session无法保存_sessi
- Mac如何使用听写功能_Mac语音输入打字【效率技
- Win11如何卸载OneDrive_Win11卸载
- Windows如何使用注册表查找和删除项?(reg
- Win11怎样彻底卸载自带应用_Win11彻底卸载
- Win11怎么制作U盘启动盘_Win11原版系统安
- Windows驱动无法加载错误解决方法_驱动签名验
- Win11怎么关闭自动调节屏幕亮度_Windows
- Win11怎样安装钉钉客户端_Win11安装钉钉教
- Windows服务启动类型恢复方法_错误修改导致的
- Win10怎么关闭自动更新错误重启 Win10策略
- 如何在Golang中配置代码格式化工具_使用gof
- Windows10电脑怎么设置虚拟内存_Win10
- Win10如何备份注册表_Win10注册表备份步骤
- php接口返回数据乱码怎么办_php接口调试编码问
- 如何使用Golang反射将map转换为struct
- Python 模块的 __name__ 属性如何由
- Win11怎么关闭OneDrive同步_Win11
- Win11怎么关闭任务栏小组件_Windows11
- Win11声音太小怎么办_Windows 11开启
- Django密码修改后会话失效的解决方案
- Win11应用商店下载慢怎么办 Win11更改DN
- LINUX的SELinux是什么_详解LINUX强
- c++怎么实现高并发下的无锁队列_c++ std:
- Win11怎么关闭搜索历史_Win11清除设备上的
- 如何使用Golang实现微服务状态监控_Golan
- Windows如何设置登录时的欢迎屏幕背景?(锁屏


QQ客服