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,则加上