盲盒转盘优化-调试-加日志监控

This commit is contained in:
2025-10-25 12:01:22 +08:00
parent bad85524ef
commit c4cfcb393f

View File

@@ -236,24 +236,24 @@ class BlindBoxTurntableGiftDrawWorld extends Model
// 2. 获取可用礼物
$availableGifts = $this->getAvailableGifts($bag_data['id'], $totalDrawTimes);
if (empty($availableGifts)) {
$availableGifts = $this->resetPoolAndReload($bag_data['id']); //重置奖池并重新加载
if (empty($availableGifts)) {
throw new \Exception('重置奖池后仍无可用礼物');
}
$totalDrawTimes = 0;//总抽奖次数重置
}
// if (empty($availableGifts)) {
// $availableGifts = $this->resetPoolAndReload($bag_data['id']); //重置奖池并重新加载
// if (empty($availableGifts)) {
// throw new \Exception('重置奖池后仍无可用礼物');
// }
// $totalDrawTimes = 0;//总抽奖次数重置
// }
// 在处理奖池重置逻辑之前添加检查
if (!is_array($availableGifts)) {
throw new \Exception('可用礼物数据格式错误');
}
// if (!is_array($availableGifts)) {
// throw new \Exception('可用礼物数据格式错误');
// }
// 3. 预加载礼物信息(减少后续查询)
$giftInfoMap = $this->preloadGiftInfo($availableGifts);
// 4. 处理奖池重置逻辑 - 优化版本
$needGiftNum = count($toarray) * $num; //总需要的礼物数
$remaining_available_gifts = [];
$remainingGiftCount = array_sum(array_column($availableGifts, 'remaining_number'));
$remainingGiftCount = array_sum(array_column($availableGifts, 'remaining_number')) ?? 0;
if ($remainingGiftCount < $needGiftNum) {
// 如果当前奖池礼物不足以满足需求
@@ -552,7 +552,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model
}
// 3. 批量更新库存按ID排序避免死锁
$this->batchUpdateGiftInventory($availableGiftss);
$this->batchUpdateGiftInventory($availableGiftss,$bag_data['id']);
// 4. 批量插入礼包发放记录
$this->batchInsertGiftBagReceiveLog($user_id, $boxTurntableLog, $bag_data, $room_id, $precomputedResults);
@@ -585,7 +585,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model
/**
* 批量更新礼物库存
*/
private function batchUpdateGiftInventory($precomputedResults)
private function batchUpdateGiftInventory($precomputedResults,$gift_bag_id)
{
// 按礼物ID分组统计需要减少的数量
$inventoryUpdates = [];
@@ -620,6 +620,12 @@ class BlindBoxTurntableGiftDrawWorld extends Model
throw new \Exception('更新礼物剩余数量失败');
}
}
$total_remaining_number = db::name("vs_gift_bag_detail")
->where(['gift_bag_id' => $gift_bag_id])
->sum('remaining_number');
if($total_remaining_number <= 0){
$this->reset_gift_pool($gift_bag_id);
}
}
/**