diff --git a/application/api/model/GiveGiftBases.php b/application/api/model/GiveGiftBases.php index fa134c72..546ce2be 100644 --- a/application/api/model/GiveGiftBases.php +++ b/application/api/model/GiveGiftBases.php @@ -141,6 +141,7 @@ class GiveGiftBases extends Model $this->success('获取成功', $result); } + /** * 用户送礼统计 */ @@ -382,4 +383,43 @@ class GiveGiftBases extends Model return $result; } + + /** + * 获取房间内送礼排行 + */ + + public function getRoomGiftRanking($params,$type) + { + $where = []; + $options = []; + + // 时间范围 + if (!empty($params['start_time'])) { + $options['start_time'] = $params['start_time']; + } + if (!empty($params['end_time'])) { + $options['end_time'] = $params['end_time']; + } + // 构建查询条件 + if (!empty($params['from_id'])) { + $where['from_id'] = $params['from_id']; + } + + // 分页参数 + $options['page'] = $params['page'] ?? 1; + $options['limit'] = min($params['limit'] ?? 20, 100); // 限制最大100条 + + if($type == 1){ + $options['group_by'] = 'user_id'; + }else{ + $options['group_by'] = 'gift_user'; + } + + + // 查询数据 + $result = $this->giftModel->getGiftStatisticsRanking($where, $options); + + return $result; + } + } \ No newline at end of file diff --git a/application/api/model/Room.php b/application/api/model/Room.php index 9db597c2..77edcbea 100644 --- a/application/api/model/Room.php +++ b/application/api/model/Room.php @@ -1855,51 +1855,44 @@ class Room extends Model //房间排行 public function room_rank($room_id, $type, $timetype, $page, $limit) { - $where['a.from_id'] = $room_id; - $where['a.from'] = 2; if($timetype == 1){ //时间榜 查询当前这一小时的数据 $begin_time = strtotime(date('Y-m-d H:00:00')); $end_time = strtotime(date('Y-m-d H:59:59')); - $where['a.createtime'] = ['between', [$begin_time, $end_time]]; }elseif($timetype == 2){ //天榜 查询当天数据 - $where['a.createtime'] = ['between', [strtotime(date('Y-m-d')), time()]]; + $begin_time = strtotime(date('Y-m-d')); + $end_time = time(); }elseif ($timetype == 3){ - //周榜 查询当周数据 从周一起查 - //周一到周天 - $where['a.createtime'] = ['between', [strtotime('this week Monday'), time()]]; + //周榜 查询当周数据 从周一起查 //周一到周天 + $begin_time = strtotime('this week Monday'); + $end_time = time(); + }else{//时间榜 默认 + $begin_time = strtotime(date('Y-m-d H:00:00')); + $end_time = strtotime(date('Y-m-d H:59:59')); } + + $params['from_id'] = $room_id; + $params['start_time'] = $begin_time; + $params['end_time'] = $end_time; + $params['page'] = $page; + $params['limit'] = $limit; + if($type == 1){//1财富榜,2魅力榜 - $list = db::name('vs_give_gift')->alias('a') - ->join('user b', 'a.user_id = b.id') - ->field('a.user_id,b.nickname,b.avatar,sum(a.total_price) * 10 as total') - ->where($where) - ->order('total desc') - ->group('a.user_id') - ->page($page, $limit) - ->select(); + $res = model('api/GiveGiftBases')->getRoomGiftRanking($params, $type); + $list = $res['data']; }else{ - $list = db::name('vs_give_gift')->alias('a') - ->join('user b', 'a.gift_user = b.id') - ->field('a.gift_user as user_id,b.nickname,b.avatar,sum(a.total_price) * 10 as total') - ->where($where) - ->order('total desc') - ->group('a.gift_user') - ->page($page, $limit) - ->select(); + $res = model('api/GiveGiftBases')->getRoomGiftRanking($params, $type); + $list = $res['data']; } if(isset($list)){ - foreach($list as &$v){ - //用户等级标签 - $v['icon'][0] = model('UserData')->user_wealth_icon($v['user_id']);//财富图标 - $v['icon'][1] = model('UserData')->user_charm_icon($v['user_id']);//魅力图标 - //判断是否是歌手 如果是 返回等级图标 - $isSinger = db::name('vs_singer')->where(['user_id' => $v['user_id'],'status' => 1])->find(); - if($isSinger){ - $v['icon'][2] = db::name('vs_singer_level')->where(['level' => $isSinger['level'],'deletetime' => 0])->value('image');//等级图标 + foreach ($list as &$value) { + if(isset($value['gift_user'])){//$value['gift_user'] + $value['user_id'] = $value['gift_user']; } + $value['nickname'] = db::name('user')->where('id', $value['user_id'])->value('nickname'); + $value['avatar'] = db::name('user')->where('id', $value['user_id'])->value('avatar'); } } return ['code' => 1, 'msg' => '成功', 'data' => $list]; diff --git a/application/common/model/GiveGiftBase.php b/application/common/model/GiveGiftBase.php index 66440606..21c85a70 100644 --- a/application/common/model/GiveGiftBase.php +++ b/application/common/model/GiveGiftBase.php @@ -539,7 +539,7 @@ class GiveGiftBase extends Model SUM(total_price) * 10 as total FROM ({$unionSql}) as tmp GROUP BY {$options['group_by']} - ORDER BY total_price"; + ORDER BY total desc"; } else { $finalSql = "SELECT SUM(total_number) as total_number, SUM(total_price) as total,