diff --git a/application/api/model/GiveGiftBases.php b/application/api/model/GiveGiftBases.php index de75b7fc..aa73bc4a 100644 --- a/application/api/model/GiveGiftBases.php +++ b/application/api/model/GiveGiftBases.php @@ -82,7 +82,7 @@ class GiveGiftBases extends Model /** * 房间送礼统计 */ - public function getRoomStatistics() + public function getRoomStatistics($params) { $fromId = $this->request->param('from_id/d', 0); $startTime = $this->request->param('start_time/d', strtotime('-7 days')); @@ -144,9 +144,10 @@ class GiveGiftBases extends Model /** * 用户送礼统计 */ - public function getUserStatistics() + public function getUserStatistics($params) { $userId = $this->request->param('user_id/d', 0); + $userId = $params['user_id']; $startTime = $this->request->param('start_time/d', strtotime('-30 days')); $endTime = $this->request->param('end_time/d', time()); @@ -346,4 +347,41 @@ class GiveGiftBases extends Model $result = array("total" => $total, "rows" => $data); } + + + /** + * 获取送礼排行 + */ + + public function getGiftRanking($params) + { + $where = []; + $options = []; + + // 时间范围 + if (!empty($params['start_time'])) { + $options['start_time'] = strtotime($params['start_time']); + } + if (!empty($params['end_time'])) { + $options['end_time'] = strtotime($params['end_time']); + } + + + // 分页参数 + $options['page'] = $params['page'] ?? 1; + $options['limit'] = min($params['limit'] ?? 20, 100); // 限制最大100条 + + + $options['group_by'] = 'user_id'; + + + // 查询数据 + $result = $this->giftModel->getGiftStatisticsRanking($where, $options); + + Log::info("查询送礼记录,条件: " . json_encode($where) . ", 结果数: " . count($result['data'])); + +// $this->success('获取成功', $result); + return $result; + } + } \ No newline at end of file diff --git a/application/api/model/Ranking.php b/application/api/model/Ranking.php index 664f33ed..05f8bf80 100644 --- a/application/api/model/Ranking.php +++ b/application/api/model/Ranking.php @@ -41,27 +41,23 @@ class Ranking extends Model public function ranking($ranking_type) { $where['a.createtime'] = ['between', [$this->begin_time, $this->end_time]]; + $my_ranking = db::name('user') - ->field('user_code,avatar,nickname,id as user_id,user_code,sex') + ->field('user_code,avatar,nickname,id as user_id,sex') ->where('id',$this->user_id)->find(); - $my_ranking['icon'][0] = model('UserData')->user_wealth_icon($my_ranking['user_id']);//财富图标 + $my_ranking['total'] = 0; $my_ranking['rank'] = -1; $my_ranking['diff'] = 0; - if($ranking_type == 1){//1财富榜,2魅力榜 - $lists = db::name('vs_give_gift')->alias('a') - ->join('user b', 'a.user_id = b.id') - ->field('a.user_id,b.nickname,b.avatar,b.user_code,sum(a.total_price) * 10 as total') - ->where($where) - ->order('total desc') - ->group('a.user_id') -// ->page($this->page, $this->page_limit) - ->select(); + if($ranking_type == 1){//1财富榜,2魅力榜 + $params['start_time']=$this->begin_time; + $params['end_time']=$this->end_time; + $lists = model('GiveGiftBases')->getGiftRanking($params); + var_dump($lists);exit; if($lists){ foreach ($lists as $key => &$value) { - $lists[$key]['icon'][0] = model('UserData')->user_wealth_icon($value['user_id']);//财富图标 - $lists[$key]['icon'][1] = model('UserData')->user_charm_icon($value['user_id']);//魅力图标 + $lists[$key]['rank'] = $key + 1; if ($this->user_id == $value['user_id']) { $my_ranking = $lists[$key]; @@ -78,8 +74,7 @@ class Ranking extends Model ->where('a.user_id',$this->user_id) ->page($this->page, $this->page_limit) ->select(); - $my_ranking_data['icon'][0] = model('UserData')->user_wealth_icon($my_ranking_data['user_id']);//财富图标 - $my_ranking_data['icon'][1] = model('UserData')->user_charm_icon($my_ranking_data['user_id']);//魅力图标 + $lists_last_number = $lists[count($lists) - 1]['total'] ?? 0; if (!isset($my_ranking_data['user_id']) || !$my_ranking_data['user_id']) { @@ -110,8 +105,7 @@ class Ranking extends Model if($lists){ foreach ($lists as $key => &$value) { - $lists[$key]['icon'][0] = model('UserData')->user_wealth_icon($value['user_id']);//财富图标 - $lists[$key]['icon'][1] = model('UserData')->user_charm_icon($value['user_id']);//魅力图标 + $lists[$key]['rank'] = $key + 1; if ($this->user_id == $value['user_id']) { $my_ranking = $lists[$key]; @@ -128,8 +122,7 @@ class Ranking extends Model ->where('a.gift_user',$this->user_id) ->select(); - $my_ranking_data['icon'][0] = model('UserData')->user_wealth_icon($my_ranking_data['user_id']);//财富图标 - $my_ranking_data['icon'][1] = model('UserData')->user_charm_icon($my_ranking_data['user_id']);//魅力图标 + $lists_last_number = $lists[count($lists) - 1]['total'] ?? 0; if (!isset($my_ranking_data['user_id']) || !$my_ranking_data['user_id']) { diff --git a/application/common/controller/BaseCom.php b/application/common/controller/BaseCom.php index 8d340c01..0fc68af3 100644 --- a/application/common/controller/BaseCom.php +++ b/application/common/controller/BaseCom.php @@ -55,6 +55,8 @@ class BaseCom extends Controller $this->uid = 0; //定义一个常量 define('UID', $this->uid); + }else{ + $this->check_login($is_maintenance) ; } }else{ $this->check_login($is_maintenance) ; diff --git a/application/common/model/GiveGiftBase.php b/application/common/model/GiveGiftBase.php index d7ae41e0..635898a4 100644 --- a/application/common/model/GiveGiftBase.php +++ b/application/common/model/GiveGiftBase.php @@ -459,4 +459,100 @@ class GiveGiftBase extends Model return []; } } + + + + /** + * 统计送礼数据排行 + * @param array $where 查询条件 + * @param array $options 统计选项 + * @return array + */ + public function getGiftStatisticsRanking($where = [], $options = []) + { + $defaultOptions = [ + 'group_by' => null, // 分组字段 + 'start_time' => null, + 'end_time' => null, + ]; + + $options = array_merge($defaultOptions, $options); + + // 获取需要查询的表 + $tables = GiftTableManager::getTablesByTimeRange( + $options['start_time'], + $options['end_time'] + ); + + if (empty($tables)) { + return []; + } + + // 构建查询条件 + $conditions = $this->buildQueryConditions($where); + + $unionSql = ''; + $params = []; + + // 构建统计SQL + $fields = 'SUM(total_price) as total_price'; + if ($options['group_by']) { + $fields .= ", {$options['group_by']}"; + } + + foreach ($tables as $table) { + $sql = "SELECT {$fields} FROM `{$table}` WHERE 1=1"; + + foreach ($conditions as $field => $condition) { + if (is_array($condition) && count($condition) == 2) { + $operator = $condition[0]; + $value = $condition[1]; + $sql .= " AND `{$field}` {$operator} ?"; + $params[] = $value; + } + } + + // 时间范围 + if ($options['start_time']) { + $sql .= " AND createtime >= ?"; + $params[] = $options['start_time']; + } + + if ($options['end_time']) { + $sql .= " AND createtime <= ?"; + $params[] = $options['end_time']; + } + + if ($options['group_by']) { + $sql .= " GROUP BY {$options['group_by']}"; + } + + if ($unionSql) { + $unionSql .= " UNION ALL "; + } + $unionSql .= "(" . $sql . ")"; + } + + // 最终统计 + if ($options['group_by']) { + $finalSql = "SELECT {$options['group_by']}, + SUM(total_price) as total_price, + FROM ({$unionSql}) as tmp + GROUP BY {$options['group_by']} + ORDER BY total_price"; + } else { + $finalSql = "SELECT SUM(total_number) as total_number, + SUM(total_price) as total_price, + SUM(total_count) as total_count + FROM ({$unionSql}) as tmp"; + } + + try { + $result = Db::query($finalSql, $params); + return $options['group_by'] ? $result : ($result[0] ?? []); + } catch (\Exception $e) { + Log::error("统计送礼数据失败: " . $e->getMessage()); + return []; + } + } } \ No newline at end of file