diff --git a/application/api/model/BlindBoxTurntableGiftDrawWorld.php b/application/api/model/BlindBoxTurntableGiftDrawWorld.php index 8650ca9..2a93e21 100644 --- a/application/api/model/BlindBoxTurntableGiftDrawWorld.php +++ b/application/api/model/BlindBoxTurntableGiftDrawWorld.php @@ -785,7 +785,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model $text_list_new = [ 'text' => $text, 'room_id' => $room_id, - 'from_type' => 1 + 'from_type' => 101 ]; $push->xunlehui($text_list_new); } @@ -797,7 +797,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model $text_list_new = [ 'text' => $text, 'room_id' => $room_id, - 'from_type' => 2 + 'from_type' => 102 ]; $push->xunlehui($text_list_new); // 巡乐会正式开始 @@ -840,7 +840,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model $text_list_new = [ 'xlh_data' => $xlh, 'text' => "", - 'from_type' => 0 + 'from_type' => 100 ]; $push->xunlehui($text_list_new); @@ -1044,40 +1044,31 @@ class BlindBoxTurntableGiftDrawWorld extends Model $ext = $this->getCachedXlhConfig(); // 2. 检查用户金币和房间状态 $bag_gift_price = $ext['xlh_box_price'] * $num; - $user_waller = db::name('user_wallet')->where(['user_id' => $user_id])->find(); - if (!$user_waller || $user_waller['coin'] < $bag_gift_price) { - return ['code' => 0, 'msg' => '用户金币不足', 'data' => null]; - } // 3. 检查巡乐会状态 $pan_xlh = db::name('vs_room_pan_xlh') + ->where(['send_time' => 0]) ->order('id', 'desc') ->find(); if (empty($pan_xlh)) { - return ['code' => 0, 'msg' => '未开始', 'data' => null]; + return ['code' => 0, 'msg' => '本轮未开始', 'data' => null]; } if ($pan_xlh['end_time'] <= time()) { return ['code' => 0, 'msg' => '本轮已结束', 'data' => null]; } - if ($pan_xlh['send_time'] != 0) { - return ['code' => 0, 'msg' => '本轮已结束,礼物已发放', 'data' => null]; - } // 4. 预加载必要数据 $gift_bag_detail = db::name("vs_gift_bag_detail") ->field('id,quantity,remaining_number,weight,foreign_id,gift_bag_id') ->where(['gift_bag_id'=>$gift_bag_id]) + ->where(['remaining_number' => ['>', 0]]) ->select(); - if (empty($gift_bag_detail)) { - return ['code' => 0, 'msg' => '当前房间未配置抽奖礼物,请联系管理员', 'data' => []]; - } - // 计算总数量和剩余数量 $total_quantity = db::name("vs_gift_bag_detail") ->where(['gift_bag_id' => $gift_bag_id]) ->sum('quantity'); $total_remaining = array_sum(array_column($gift_bag_detail, 'remaining_number')); - $total_draw_times = $total_quantity - $total_remaining; + $total_draw_times = $total_quantity - $total_remaining; // 已抽奖次数 if ($total_draw_times < 0) $total_draw_times = 0; @@ -1094,6 +1085,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model return $gift['remaining_number'] > 0; }); $this->reset_gift_pool($gift_bag_id,$remaining_available_gifts); + $available_gifts = $this->getAvailableGifts($gift_bag_id); } // 6. 预计算抽奖结果 @@ -1108,11 +1100,15 @@ class BlindBoxTurntableGiftDrawWorld extends Model $all_results = []; // 存储所有抽奖结果 while ($total_processed < $num) { - $current_batch = min($batch_size, $num - $total_processed); + $current_batch = min($batch_size, $num - $total_processed); // 当前批次处理数量 db::startTrans(); try { // 批量扣除金币(只在第一次事务中处理) if ($total_processed == 0) { + $user_waller = db::name('user_wallet')->where(['user_id' => $user_id])->find(); + if (!$user_waller || $user_waller['coin'] < $bag_gift_price) { + return ['code' => 0, 'msg' => '用户金币不足', 'data' => null]; + } $wallet_update = model('GiveGift')->change_user_cion_or_earnings_log( $user_id, $bag_gift_price, @@ -1138,15 +1134,17 @@ class BlindBoxTurntableGiftDrawWorld extends Model // 处理当前批次的抽奖 $inventory_updates = []; // 用于记录库存变化 - $gift_details_map = []; // 礼物详情映射 $remaining_available_gifts = $available_gifts; for ($i = 0; $i < $current_batch; $i++) { // 从可用礼物中选择 $selected_gift = $this->selectGiftFromAvailable($available_gifts); if (!$selected_gift) { - throw new \Exception('预计算抽奖失败'); + $gift_bag_detail = $this->resetPoolAndReload($gift_bag_id); + $selected_gift = $this->selectGiftFromAvailable($gift_bag_detail); + if(!$selected_gift){ + throw new \Exception('预计算抽奖失败,重置后无可用礼物'); + } } - // 记录库存变化 $inventory_updates[$selected_gift['id']] = ($inventory_updates[$selected_gift['id']] ?? 0) + 1; @@ -1195,9 +1193,8 @@ class BlindBoxTurntableGiftDrawWorld extends Model // 检查是否需要重置奖池 $total_remaining = array_sum(array_column($available_gifts, 'remaining_number')); - if ($total_remaining <= 0 && $total_processed + $i + 1 < $num) { - $this->reset_gift_pool($gift_bag_id,$remaining_available_gifts); - $available_gifts = $this->reloadGiftPool($room_id, $gift_bag_id); + if ($total_remaining <= 0 && $total_processed + $i + 1 < $num) {// 剩余数量小于等于0且当前处理数量小于总数量 + $available_gifts = $this->resetPoolAndReload($gift_bag_id); if (empty($available_gifts)) { throw new \Exception('重置奖池后仍无可用礼物'); } @@ -1251,8 +1248,16 @@ class BlindBoxTurntableGiftDrawWorld extends Model // 7. 批量处理结果记录 try { db::startTrans(); - if(count($drawn_gifts) > $num){ - Log::record('巡乐会抽奖失败-数量超限: ' . count($drawn_gifts),"infos"); + if(count($drawn_gifts) != $num){ + $key = 'xlh_draw_gift_errors_' . date('Y-m-d-H-i-s'); + $errorData = [ + 'user_id' => $user_id, + 'gift_bag_id' => $gift_bag_id, + 'room_id' => $room_id, + 'num' => $num, + 'drawn_gifts_num' => count($drawn_gifts) + ]; + $this->redis->setex($key, 86400 * 7, "巡乐会抽奖失败-数量超限". ' ' .json_encode($errorData)); return ['code' => 0, 'msg' => "抽奖中,请稍等...", 'data' => null]; } // 批量插入礼包发放记录 @@ -1416,14 +1421,14 @@ class BlindBoxTurntableGiftDrawWorld extends Model $room_user = db::name('user')->where('id',$room_data['user_id'])->field('nickname,avatar')->find(); //锁定礼物 - $text = [ - 'gift_num' => $pan_xlh_num, - 'FromUserInfo' => $FromUserInfo, - 'end_time' => $end_time, - 'text' => $FromUserInfo['nickname'] . ' 在 ' . ' 巡乐会活动中 获得' . $gift_info['gift_name'] . '礼物 x 1' - ]; - - model('Chat')->sendMsg(1057, $room_id, $text); +// $text = [ +// 'gift_num' => $pan_xlh_num, +// 'FromUserInfo' => $FromUserInfo, +// 'end_time' => $end_time, +// 'text' => $FromUserInfo['nickname'] . ' 在 ' . ' 巡乐会活动中 获得' . $gift_info['gift_name'] . '礼物 x 1' +// ]; +// +// model('Chat')->sendMsg(1057, $room_id, $text); //推送礼物横幅 $text = $FromUserInfo['nickname'] .' 巡乐会活动中锁定礼物'.$locking_gift['gift_name'].' x ' .$pan_xlh_num ; @@ -1431,7 +1436,7 @@ class BlindBoxTurntableGiftDrawWorld extends Model $text_list_new = [ 'text' => $text, 'room_id' => $room_id, - 'from_type' => 4, + 'from_type' => 103, 'gift_num' => $pan_xlh_num, 'FromUserInfo' => $FromUserInfo, 'end_time' => $end_time, diff --git a/application/cron/controller/RoomPan.php b/application/cron/controller/RoomPan.php index b7b5756..411665a 100644 --- a/application/cron/controller/RoomPan.php +++ b/application/cron/controller/RoomPan.php @@ -137,7 +137,7 @@ class RoomPan $text_list_new = [ 'text' => $text, 'room_id' => $value['room_id'], - 'from_type' => 3 + 'from_type' => 104 ]; $push->xunlehui($text_list_new); continue; @@ -184,7 +184,7 @@ class RoomPan $text_list_new = [ 'text' => $text, 'room_id' => $value['room_id'], - 'from_type' => 3 + 'from_type' => 104 ]; $push->xunlehui($text_list_new); }catch (\Exception $e){