diff --git a/application/api/model/BlindBoxTurntableGiftDrawWorldNew.php b/application/api/model/BlindBoxTurntableGiftDrawWorldNew.php index 6ea911e..39b634c 100644 --- a/application/api/model/BlindBoxTurntableGiftDrawWorldNew.php +++ b/application/api/model/BlindBoxTurntableGiftDrawWorldNew.php @@ -171,11 +171,10 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model $newGiftsNeeded = max(0, $total_num - $remainingGiftCount); // 计算还需要多少礼物从新奖池中抽取 // 先从上期剩余礼物中分配 if (!empty($remaining_available_gifts)) { - $aliasTableForRemaining = $this->buildAliasTable($remaining_available_gifts); // 计算上期剩余礼物总数 foreach ($gift_user_ids as $giftUserId) { // 为每个用户先分配上期剩余礼物 - $userRemainingAllocation = floor($remainingGiftCount / $total_num); + $userRemainingAllocation = floor($remainingGiftCount / count($gift_user_ids)); if (count($gift_user_ids) > 0) { // 防止除零错误 $extraGifts = $remainingGiftCount % count($gift_user_ids); if (array_search($giftUserId, $gift_user_ids) < $extraGifts) { @@ -183,19 +182,26 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model } } for ($i = 0; $i < $userRemainingAllocation; $i++) { - $selectedGift = $this->selectGiftWithAliasMethod($aliasTableForRemaining); - if ($selectedGift) { - $gift = $giftInfoMap[$selectedGift['foreign_id']]; - $precomputedResults[] = [ - 'gift_user_id' => $giftUserId, - 'gift_bag_detail' => $selectedGift, - 'gift' => $gift, - 'draw_times' => $this->getCachedPanDrawTimes($gift_bag_id), - 'periods' => $periods, - ]; - $this->getCachedXlhPeriodsNum("set");//添加寻乐会条件次数 - $pan_total_draw_times = $this->getCachedPanDrawTimes($gift_bag_id,"set"); // 总抽奖次数 - } + if(!empty($remaining_available_gifts)){ + $randomKey = array_rand($remaining_available_gifts); + $selectedGift = $remaining_available_gifts[$randomKey]; + --$remaining_available_gifts[$randomKey]['remaining_number']; + if($remaining_available_gifts[$randomKey]['remaining_number'] <=0){ + unset($remaining_available_gifts[$randomKey]); + } + if ($selectedGift) { + $gift = $giftInfoMap[$selectedGift['foreign_id']]; + $precomputedResults[] = [ + 'gift_user_id' => $giftUserId, + 'gift_bag_detail' => $selectedGift, + 'gift' => $gift, + 'draw_times' => $this->getCachedPanDrawTimes($gift_bag_id), + 'periods' => $periods, + ]; + $this->getCachedXlhPeriodsNum("set");//添加寻乐会条件次数 + $pan_total_draw_times = $this->getCachedPanDrawTimes($gift_bag_id,"set"); // 总抽奖次数 + } + } } } $pan_total_draw_times = $this->getCachedPanDrawTimes($gift_bag_id,"clear");//总抽奖次数重置 @@ -214,7 +220,22 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model } for ($i = 0; $i < $userNewAllocation; $i++) { $selectedGift = $this->selectGiftWithAliasMethod($aliasTableForNew); + if(!$selectedGift){ + //重置奖池 + $availableGifts = $this->resetPoolAndReload($gift_bag_id); + if (empty($availableGifts)) { + throw new \Exception('重置奖池后仍无可用礼物'); + } + $this->getCachedPanDrawTimes($gift_bag_id,"clear");//总抽奖次数重置 + $aliasTableForNew = $this->buildAliasTable($availableGifts); + $selectedGift = $this->selectGiftWithAliasMethod($aliasTableForNew); + } if ($selectedGift) { + $giftBagDetailCached = $this->getCachedGiftBagDetailItem($gift_bag_id, $selectedGift['id']); + if($giftBagDetailCached['remaining_number']<=0){ + $aliasTableForNew = $this->buildAliasTable($availableGifts); + $selectedGift = $this->selectGiftWithAliasMethod($aliasTableForNew); + } $gift = $giftInfoMap[$selectedGift['foreign_id']]??[]; $precomputedResults[] = [ 'gift_user_id' => $giftUserId, @@ -400,6 +421,12 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model } $attempt++; } + // 兜底方案:遍历查找第一个有库存的礼物 + foreach ($aliasTable['index_map'] as $gift) { + if ($gift['remaining_number'] > 0) { + return $gift; + } + } // 如果重试次数用完仍未抽中有效礼物,返回null return null; } @@ -477,7 +504,9 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model if ($gift_bag_detail_data) { foreach ($gift_bag_detail_data as &$item) { if ($item['id'] == $detail_id) { - $item['remaining_number'] -= $decrement; + if( $item['remaining_number'] >= $decrement){ + $item['remaining_number'] -= $decrement; + } break; } } @@ -715,6 +744,9 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model }else{ $upRemainingNumber = $giftBagDetailCached['remaining_number']; } + if($upRemainingNumber < 0){ + throw new \Exception('礼物数量不足'); + } $ret = db::name("vs_gift_bag_detail")->where('id',$giftId)->update([ 'remaining_number' => $upRemainingNumber ]);