From 8315eeae2d3aa2bd68423659172fae2637069cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=92=8A?= Date: Fri, 24 Oct 2025 14:58:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B2=E7=9B=92=E8=BD=AC=E7=9B=98=E4=BC=98?= =?UTF-8?q?=E5=8C=96-=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/BlindBoxTurntableGiftDrawWorld.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/application/api/model/BlindBoxTurntableGiftDrawWorld.php b/application/api/model/BlindBoxTurntableGiftDrawWorld.php index ab8a8e8..056c408 100644 --- a/application/api/model/BlindBoxTurntableGiftDrawWorld.php +++ b/application/api/model/BlindBoxTurntableGiftDrawWorld.php @@ -576,10 +576,24 @@ class BlindBoxTurntableGiftDrawWorld extends Model // 批量更新 foreach ($inventoryUpdates as $giftId => $count) { + // 使用 FOR UPDATE 锁定记录 + $giftBagDetail = Db::name("vs_gift_bag_detail") + ->where('id', $giftId) + ->lock(true) // 添加悲观锁 + ->find(); + if (!$giftBagDetail) { + throw new \Exception("礼物详情不存在,ID: " . $giftId); + } + + // 检查库存是否足够 + if ($giftBagDetail['remaining_number'] < $count) { + throw new \Exception("礼物库存不足,ID: " . $giftId); + } + // 执行更新 $ret = db::name("vs_gift_bag_detail")->where('id',$giftId) ->setDec('remaining_number', $count); if (!$ret) { - Log::record('巡乐会更新礼物剩余数量: ' . $room_id."【数据】".var_export($precomputedResults, true),"info"); + $this->redis->setex('blind_box_draw_world_errors_' . date('Y-m-d-H-i-s'), 86400 * 7, "更新礼物剩余数量失败 礼物ID:".$giftId.' 数量:'.$count.' 预处理结果:'.json_encode($precomputedResults)); throw new \Exception('更新礼物剩余数量失败'); } } @@ -1042,7 +1056,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model * @param int $gift_bag_id 礼物包ID */ private function reset_gift_pool($gift_bag_id,$remaining_available_gifts=[]) { - $bag_detail = db::name("vs_gift_bag_detail")->where('gift_bag_id',$gift_bag_id)->select(); +// $bag_detail = db::name("vs_gift_bag_detail")->where('gift_bag_id',$gift_bag_id)->select(); db::name("vs_gift_bag")->where('id',$gift_bag_id)->setInc('periods'); //更新期数 db::name("vs_gift_bag_detail")->where('gift_bag_id',$gift_bag_id)->update(['remaining_number'=>db::raw('quantity')]);//重置奖池 //防止并发,上把如果件数小于0,则加上