diff --git a/application/api/controller/RoomHourRanking.php b/application/api/controller/RoomHourRanking.php index 859e461..b6b1089 100644 --- a/application/api/controller/RoomHourRanking.php +++ b/application/api/controller/RoomHourRanking.php @@ -2,9 +2,10 @@ namespace app\api\controller; +use app\common\controller\BaseCom; use think\Db; -class RoomHourRanking +class RoomHourRanking extends BaseCom { //房间小时榜是否开启 public function room_hour_ranking_is_open() @@ -23,6 +24,8 @@ class RoomHourRanking //房间小时榜 public function room_hour_ranking() { + $page = input('page', 1); + $page_limit = input('page_limit', 20); //判断是否开启 $open_time = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('open_time'); if ($open_time = 0) { @@ -31,22 +34,95 @@ class RoomHourRanking //当前小时开始时间 $start_time = strtotime(date('Y-m-d H:00:00')); + $profit = db::name('vs_room')->alias('a') ->join('vs_give_gift b', 'a.id = b.from_id','left') ->join('vs_room_label c', 'a.label_id = c.id','left') - ->field('a.id as room_id,a.room_name,a.label_id,a.room_cover,sum(b.total_price) as total_price,c.label_icon') - ->where('b.from',2) + ->field('a.id as room_id,a.room_name,a.label_id,a.room_cover,IFNULL(sum(b.total_price), 0) as total_price,c.label_icon') + ->where('a.room_status', 1) // 只统计正常状态的房间 + ->where('a.apply_status', 2) // 只统计审核通过的房间 + ->where('b.from', 2) ->where('b.createtime', 'between', [$start_time, time()]) + ->group('a.id') ->order('total_price', 'desc') -// ->limit(20) + ->page($page, $page_limit) ->select(); + if($profit){ - foreach ($profit as $v) { - $xlh_status = model('BlindBoxTurntableGift')->get_user_xlh_info($v['room_id']); - $v['xlh_status'] = $xlh_status['xlh_status']; -// $v['xlh_status'] = 1; + foreach ($profit as &$v) { + if($v['room_id'] > 0){ + $xlh_status = model('BlindBoxTurntableGift')->get_user_xlh_info($v['room_id']); + $v['xlh_status'] = $xlh_status['xlh_status']; + }else{ + $v['xlh_status'] = 0; + } } } return V(1, '获取成功', $profit); } + + //房间小时榜 + public function room_hour_rankings() + { + $page = input('page', 1); + $page_limit = input('page_limit', 20); + //判断是否开启 + $open_time = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('open_time'); + if ($open_time == 0) { // 修复比较运算符 +// return V(0, '排行榜暂未开启'); + } + + //当前小时开始时间 + $start_time = strtotime(date('Y-m-d H:00:00')); + + // 先查询所有符合条件的房间 + $rooms = db::name('vs_room')->alias('a') + ->join('vs_room_label c', 'a.label_id = c.id','left') + ->field('a.id as room_id,a.room_name,a.label_id,a.room_cover,0 as total_price,c.label_icon') + ->where('a.room_status', 1) + ->where('a.apply_status', 2) + ->page($page, $page_limit) + ->select(); + + if ($rooms) { + // 获取房间ID列表 + $room_ids = array_column($rooms, 'room_id'); + + // 查询这些房间在当前小时的礼物总额 + $gift_data = db::name('vs_give_gift') + ->field('from_id as room_id, sum(total_price) as total_price') + ->where('from', 2) + ->where('from_id', 'in', $room_ids) + ->where('createtime', 'between', [$start_time, time()]) + ->group('from_id') + ->select(); + + // 将礼物数据合并到房间数据中 + $gift_map = []; + foreach ($gift_data as $gift) { + $gift_map[$gift['room_id']] = $gift['total_price'] * get_system_config_value('coin_charm_exp');; + } + + // 更新房间数据 + foreach ($rooms as &$room) { + $room['total_price'] = isset($gift_map[$room['room_id']]) ? $gift_map[$room['room_id']] : 0; + + if($room['room_id'] > 0){ + $xlh_status = model('BlindBoxTurntableGift')->get_user_xlh_info($room['room_id']); + $room['xlh_status'] = $xlh_status['xlh_status']; + }else{ + $room['xlh_status'] = 0; + } + } + + // 按礼物总额排序 + usort($rooms, function($a, $b) { + return $b['total_price'] <=> $a['total_price']; + }); + } + //当前小时开始时间 和结束时间 00:00-00:59 这样的格式 + $time_range = date('H:') . '00-' . date('H:'). '59'; + + return V(1, '获取成功', ['time_range' => $time_range, 'lists' =>$rooms]); + } } \ No newline at end of file