Jelajahi Sumber

feat:赢钱输钱榜

aiden 3 bulan lalu
induk
melakukan
5ffbc7adcc

+ 92 - 0
app/controller/.php

@@ -0,0 +1,92 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\controller;
+
+use app\BaseController;
+use app\model\GameStatisModel;
+use app\common\CommonUtils;
+use app\model\GameModel;
+use think\facade\Request;
+
+class TrendStatis extends BaseController
+{    
+    /**
+     * 在线走势
+     */
+    public function Online()
+    {
+        $userInfo = $this->request->userInfo;
+        
+        // 获取查询参数
+        $page = Request::get('page', 1, 'intval');
+        $limit = Request::get('limit', 10, 'intval');
+
+        // 筛选条件
+        $filters = [
+            // 时间筛选
+            'start_time' => Request::get('start_time', date('Y-m-d', strtotime('-7 days')), 'trim'),
+            'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
+            'game_id' => Request::get('game_id', '', 'trim'),
+        ];
+        
+        try {
+            // 获取游戏每日数据
+            $result = GameStatisModel::getGameDailyList(
+                $userInfo['merchant_id'],
+                $page, 
+                $limit,
+                $filters,
+            );
+
+            // 获取游戏信息信息
+            $game_ids = array_unique(array_column($result['list'], 'game_id'));
+            $tempGameList = GameModel::where([['merchant_id', '=', $userInfo['merchant_id']], ['game_id', 'in', $game_ids]])->field(['game_id','title','image'])->select()->toArray();
+            $gameList = [];
+            foreach ($tempGameList as $item) {
+                $game_id = $item['game_id'];
+                $gameList[$game_id] = $item;
+            }            
+            
+            // 格式化数据
+            foreach ($result['list'] as &$item) {
+                $item['game_profit'] = CommonUtils::convertBalance($item['game_profit'], false);
+                $item['bet_amount'] = CommonUtils::convertBalance($item['bet_amount'], false);
+                $item['commission_amount'] = CommonUtils::convertBalance($item['game_profit'] * 0.08, false);
+                $item['platform_fee'] = CommonUtils::convertBalance($item['bet_amount'] * 0.08, false);
+                $item['buy_free_bet'] = 0;
+                $item['game_title'] = $gameList[$item['game_id']]['title'] ?? '';
+            }
+            
+            return json_success($result, '获取成功');
+        } catch (\Exception $e) {
+            return json_error([], '获取游戏每日数据失败:' . $e->getMessage());
+        }
+    }  
+    
+    /**
+     * 游戏走势
+     */
+    public function Game() {
+
+    }
+
+    /**
+     * 输赢走势
+     */
+    public function Win() {
+
+    }
+    
+    /**
+     * 返奖倍数走势
+     */
+    public function PrizeMultiple() {
+
+    }
+
+    /**
+     * 
+     */
+    
+}

+ 123 - 0
app/controller/RankList.php

@@ -0,0 +1,123 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\controller;
+
+use app\BaseController;
+use app\model\GameStatisModel;
+use app\model\GameBetGameModel;
+use app\common\CommonUtils;
+use app\model\GameModel;
+use think\facade\Request;
+
+class RankList extends BaseController
+{    
+    /**
+     * 赢钱榜
+     */
+    public function WinAmount()
+    {
+        $userInfo = $this->request->userInfo;
+        
+        // 获取查询参数
+        $page = Request::get('page', 1, 'intval');
+        $limit = Request::get('limit', 10, 'intval');
+        $filters['date'] = Request::get('date', '', 'trim');
+        
+        try {
+            // 获取赢钱榜数据
+            $result = GameBetGameModel::getWinRanking(
+                $userInfo['merchant_id'],
+                $page, 
+                $limit,
+                $filters
+            );
+            
+            // 格式化金额数据
+            foreach ($result['list'] as &$item) {
+                $item['total_win'] = CommonUtils::convertBalance($item['total_win'], false);
+                $item['total_bet'] = CommonUtils::convertBalance($item['total_bet'], false);
+                $item['balance'] = CommonUtils::convertBalance($item['balance'], false);
+            }
+            
+            return json_success($result, '获取成功');
+        } catch (\Exception $e) {
+            return json_error([], '获取赢钱榜失败:' . $e->getMessage());
+        }
+    }  
+    
+    /**
+     * 输钱榜
+     */
+    public function LoseAmount() {
+        $userInfo = $this->request->userInfo;
+        
+        // 获取查询参数
+        $page = Request::get('page', 1, 'intval');
+        $limit = Request::get('limit', 10, 'intval');
+        $filters['date'] = Request::get('date', '', 'trim');
+        
+        try {
+            // 获取输钱榜数据
+            $result = GameBetGameModel::getLoseRanking(
+                $userInfo['merchant_id'],
+                $page, 
+                $limit,
+                $filters
+            );
+            
+            // 格式化金额数据
+            foreach ($result['list'] as &$item) {
+                $item['total_lose'] = CommonUtils::convertBalance($item['total_lose'], false);
+                $item['total_bet'] = CommonUtils::convertBalance($item['total_bet'], false);
+                $item['balance'] = CommonUtils::convertBalance($item['balance'], false);
+
+            }
+            
+            return json_success($result, '获取成功');
+        } catch (\Exception $e) {
+            return json_error([], '获取输钱榜失败:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 注单榜
+     */
+    public function BetAmount() {
+        $userInfo = $this->request->userInfo;
+        
+        // 获取查询参数
+        $page = Request::get('page', 1, 'intval');
+        $limit = Request::get('limit', 10, 'intval');
+
+        // 筛选条件
+        $filters = [
+            'start_time' => Request::get('start_time', date('Y-m-d', strtotime('-7 days')), 'trim'),
+            'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
+            'game_id' => Request::get('game_id', '', 'trim'),
+        ];
+        
+        try {
+            // 获取注单金额榜数据
+            $result = GameBetGameModel::getBetAmountRanking(
+                $userInfo['merchant_id'],
+                $page, 
+                $limit,
+                $filters
+            );
+            
+            // 格式化金额数据
+            foreach ($result['list'] as &$item) {
+                $item['total_bet_amount'] = CommonUtils::convertBalance($item['total_bet_amount'], false);
+                $item['total_profit'] = CommonUtils::convertBalance($item['total_profit'], false);
+                $item['max_bet'] = CommonUtils::convertBalance($item['max_bet'], false);
+                $item['avg_bet'] = CommonUtils::convertBalance($item['avg_bet'], false);
+            }
+            
+            return json_success($result, '获取成功');
+        } catch (\Exception $e) {
+            return json_error([], '获取注单金额榜失败:' . $e->getMessage());
+        }
+    }
+    
+}

+ 92 - 0
app/controller/TrendStatis.php

@@ -0,0 +1,92 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\controller;
+
+use app\BaseController;
+use app\model\GameStatisModel;
+use app\common\CommonUtils;
+use app\model\GameModel;
+use think\facade\Request;
+
+class TrendStatis extends BaseController
+{    
+    /**
+     * 在线走势
+     */
+    public function Online()
+    {
+        $userInfo = $this->request->userInfo;
+        
+        // 获取查询参数
+        $page = Request::get('page', 1, 'intval');
+        $limit = Request::get('limit', 10, 'intval');
+
+        // 筛选条件
+        $filters = [
+            // 时间筛选
+            'start_time' => Request::get('start_time', date('Y-m-d', strtotime('-7 days')), 'trim'),
+            'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
+            'game_id' => Request::get('game_id', '', 'trim'),
+        ];
+        
+        try {
+            // 获取游戏每日数据
+            $result = GameStatisModel::getGameDailyList(
+                $userInfo['merchant_id'],
+                $page, 
+                $limit,
+                $filters,
+            );
+
+            // 获取游戏信息信息
+            $game_ids = array_unique(array_column($result['list'], 'game_id'));
+            $tempGameList = GameModel::where([['merchant_id', '=', $userInfo['merchant_id']], ['game_id', 'in', $game_ids]])->field(['game_id','title','image'])->select()->toArray();
+            $gameList = [];
+            foreach ($tempGameList as $item) {
+                $game_id = $item['game_id'];
+                $gameList[$game_id] = $item;
+            }            
+            
+            // 格式化数据
+            foreach ($result['list'] as &$item) {
+                $item['game_profit'] = CommonUtils::convertBalance($item['game_profit'], false);
+                $item['bet_amount'] = CommonUtils::convertBalance($item['bet_amount'], false);
+                $item['commission_amount'] = CommonUtils::convertBalance($item['game_profit'] * 0.08, false);
+                $item['platform_fee'] = CommonUtils::convertBalance($item['bet_amount'] * 0.08, false);
+                $item['buy_free_bet'] = 0;
+                $item['game_title'] = $gameList[$item['game_id']]['title'] ?? '';
+            }
+            
+            return json_success($result, '获取成功');
+        } catch (\Exception $e) {
+            return json_error([], '获取游戏每日数据失败:' . $e->getMessage());
+        }
+    }  
+    
+    /**
+     * 游戏走势
+     */
+    public function Game() {
+
+    }
+
+    /**
+     * 输赢走势
+     */
+    public function Win() {
+
+    }
+    
+    /**
+     * 返奖倍数走势
+     */
+    public function PrizeMultiple() {
+
+    }
+
+    /**
+     * 
+     */
+    
+}

+ 247 - 0
app/model/GameBetGameModel.php

@@ -87,4 +87,251 @@ class GameBetGameModel extends Model
             'limit' => $limit
         ];
     }
+    
+    /**
+     * 获取赢钱榜
+     */
+    public static function getWinRanking($appId, $page = 1, $limit = 20, $filters = [])
+    {
+        $wheres = [];
+        $wheres[] = ['action_type', '=', 1];
+        $wheres[] = ['app_id', '=', $appId];
+        
+        // 时间筛选
+        if (!empty($filters['date'])) {
+            $startTime = strtotime($filters['date']);
+            $endTime = strtotime($filters['date'] . ' 23:59:59');
+
+            $wheres[] = ['create_time', '>=', $startTime];
+            $wheres[] = ['create_time', '<=', $endTime];
+        }
+        
+        // 使用子查询获取每个用户的统计数据
+        $subQuery = self::field([
+            'user_id',
+            'SUM(total_win_amount) as total_win', // 总赢钱
+            'COUNT(*) as bet_count', // 注单数
+            'SUM(bet) as total_bet', // 总投注
+        ])
+        ->where($wheres)
+        ->group('user_id')
+        ->having('total_win > 0')
+        ->buildSql();
+        
+        // echo $subQuery;
+
+        // 获取总数
+        $total = self::table($subQuery . ' t')->count();
+
+        // 获取排行榜数据
+        $list = self::table($subQuery . ' t')
+            ->order('total_win', 'desc')
+            ->page($page, $limit)
+            ->select()
+            ->toArray();
+         
+        // 获取用户ID列表
+        $userIds = array_column($list, 'user_id');
+        
+        if (!empty($userIds)) {
+            // 获取用户信息
+            $users = self::table('tp_merchants_user')
+                ->whereIn('user_id', $userIds)
+                ->field(['user_id', 'nickname'])
+                ->select()
+                ->toArray();
+            // 获取用户余额    
+            $userBanlance = self::table('tp_merchants_user_balance')
+                ->whereIn('user_id', $userIds)
+                ->field(['user_id', 'balance'])
+                ->select()
+                ->toArray();
+
+            $users = array_column($users, null, 'user_id');
+            $userBanlance = array_column($userBanlance, null, 'user_id');
+ 
+            // 补充用户信息和计算RTP
+            foreach ($list as &$item) {
+                $userId = (int)$item['user_id'];
+                $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
+                $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
+                // 计算RTP (返还率)
+                $item['rtp'] = $item['total_bet'] > 0 ? round($item['total_win'] / $item['total_bet'] * 100, 2) : 0;
+            }
+        }
+        
+        return [
+            'list' => $list,
+            'total' => $total,
+            'page' => $page,
+            'limit' => $limit
+        ];
+    }
+    
+    /**
+     * 获取输钱榜
+     */
+    public static function getLoseRanking($appId, $page = 1, $limit = 20, $filters = [])
+    {
+        $wheres = [];
+        $wheres[] = ['action_type', '=', 1];
+        $wheres[] = ['app_id', '=', $appId];
+        
+        // 时间筛选
+        if (!empty($filters['date'])) {
+            $startTime = strtotime($filters['date']);
+            $endTime = strtotime($filters['date'] . ' 23:59:59');
+
+            $wheres[] = ['create_time', '>=', $startTime];
+            $wheres[] = ['create_time', '<=', $endTime];
+        }
+        
+        // 使用子查询获取每个用户的统计数据
+        $subQuery = self::field([
+            'user_id',
+            'SUM(total_win_amount) as total_lose', // 总输钱
+            'COUNT(*) as bet_count', // 注单数
+            'SUM(bet) as total_bet', // 总投注
+        ])
+        ->where($wheres)
+        ->group('user_id')
+        ->having('total_lose < 0')
+        ->buildSql();
+        
+        // 获取总数
+        $total = self::table($subQuery . ' t')->count();
+
+        // 获取排行榜数据
+        $list = self::table($subQuery . ' t')
+            ->order('total_lose', 'asc')
+            ->page($page, $limit)
+            ->select()
+            ->toArray();
+         
+        // 获取用户ID列表
+        $userIds = array_column($list, 'user_id');
+        
+        if (!empty($userIds)) {
+            // 获取用户信息
+            $users = self::table('tp_merchants_user')
+                ->whereIn('user_id', $userIds)
+                ->field(['user_id', 'nickname'])
+                ->select()
+                ->toArray();
+            // 获取用户余额    
+            $userBanlance = self::table('tp_merchants_user_balance')
+                ->whereIn('user_id', $userIds)
+                ->field(['user_id', 'balance'])
+                ->select()
+                ->toArray();
+
+            $users = array_column($users, null, 'user_id');
+            $userBanlance = array_column($userBanlance, null, 'user_id');
+ 
+            // 补充用户信息和计算RTP
+            foreach ($list as &$item) {
+                $userId = (int)$item['user_id'];
+                $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
+                $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
+                // 计算RTP (返还率)
+                $item['rtp'] = $item['total_bet'] > 0 ? round(abs((int)$item['total_lose']) / $item['total_bet'] * 100, 2) : 0;
+            }
+        }
+        
+        return [
+            'list' => $list,
+            'total' => $total,
+            'page' => $page,
+            'limit' => $limit
+        ];
+    }
+    
+    /**
+     * 获取注单金额榜
+     */
+    public static function getBetAmountRanking($appId, $page = 1, $limit = 20, $filters = [])
+    {
+        $wheres = [];
+        $wheres[] = ['action_type', '=', 1];
+        $wheres[] = ['app_id', '=', $appId];
+        
+        // 时间筛选
+        if (!empty($filters['start_time'])) {
+            $startTime = strtotime($filters['start_time']);
+            $wheres[] = ['create_time', '>=', $startTime];
+        }
+        
+        if (!empty($filters['end_time'])) {
+            $endTime = strtotime($filters['end_time'] . ' 23:59:59');
+            $wheres[] = ['create_time', '<=', $endTime];
+        }
+        
+        // 游戏ID筛选
+        if (!empty($filters['game_id'])) {
+            $wheres[] = ['game_id', '=', $filters['game_id']];
+        }
+        
+        // 使用子查询获取每个用户的统计数据
+        $subQuery = self::field([
+            'user_id',
+            'SUM(bet) as total_bet_amount', // 总投注金额
+            'COUNT(*) as bet_count', // 注单数
+            'SUM(win_amount) as total_profit', // 总盈亏
+            'AVG(bet) as avg_bet', // 平均投注
+            'MAX(bet) as max_bet' // 最大投注
+        ])
+        ->where($wheres)
+        ->group('user_id')
+        ->buildSql();
+        
+        // 获取总数
+        $total = Db::connection('fortue_tiger')
+            ->table($subQuery . ' t')
+            ->count();
+        
+        // 获取排行榜数据(按总投注金额降序)
+        $list = Db::connection('fortue_tiger')
+            ->table($subQuery . ' t')
+            ->order('total_bet_amount', 'desc')
+            ->page($page, $limit)
+            ->select()
+            ->toArray();
+        
+        // 获取用户ID列表
+        $userIds = array_column($list, 'user_id');
+        
+        if (!empty($userIds)) {
+            // 获取用户信息
+            $users = Db::connection('fortue_tiger')
+                ->table('tp_user')
+                ->whereIn('id', $userIds)
+                ->field(['id', 'nickname', 'avatar', 'phone'])
+                ->select()
+                ->toArray();
+            
+            $userMap = [];
+            foreach ($users as $user) {
+                $userMap[$user['id']] = $user;
+            }
+            
+            // 补充用户信息和计算RTP
+            foreach ($list as &$item) {
+                $item['nickname'] = $userMap[$item['user_id']]['nickname'] ?? '未知用户';
+                $item['avatar'] = $userMap[$item['user_id']]['avatar'] ?? '';
+                $item['phone'] = $userMap[$item['user_id']]['phone'] ?? '';
+                // 计算RTP (返还率)
+                $item['rtp'] = $item['total_bet_amount'] > 0 ? 
+                    round(($item['total_bet_amount'] + $item['total_profit']) / $item['total_bet_amount'] * 100, 2) : 0;
+                // 格式化平均投注
+                $item['avg_bet'] = round($item['avg_bet'], 2);
+            }
+        }
+        
+        return [
+            'list' => $list,
+            'total' => $total,
+            'page' => $page,
+            'limit' => $limit
+        ];
+    }
 }

+ 79 - 0
app/model/TrendStatisModel.php

@@ -0,0 +1,79 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\model;
+
+use think\Model;
+use think\facade\Db;
+
+/**
+ * 走势数据统计
+ */
+class TrendStatisModel extends Model
+{
+    protected $name = 'game_bet_game';
+
+    // 连接到fortue_tiger数据库
+    protected $connection = 'fortue_tiger';
+    
+    /**
+     * 获取游戏每日数据列表
+     */
+    public static function getGameDailyList($merchantId, $page = 1, $limit = 20, $filters = [])
+    {
+        $where = [
+            ['app_id', '=', $merchantId]
+        ];
+        
+        // 时间筛选
+        if (!empty($filters['start_time'])) {
+            $startTime = strtotime($filters['start_time']);
+            $where[] = ['create_time', '>=', $startTime];
+        }
+        
+        if (!empty($filters['end_time'])) {
+            $endTime = strtotime($filters['end_time']);
+            $where[] = ['create_time', '<=', $endTime];
+        }
+
+        if (!empty($filters['game_id'])) {
+            $gameId = is_array($filters['game_id']) ? $filters['game_id'] : explode(',', $filters['game_id']);
+            $where[] = ['game_id', 'in', $gameId];
+        }
+        
+        $query = self::where($where);
+        // 统计总数
+        $total = $query->count();
+        // 获取列表数据
+        $list = $query->field([
+                    "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
+                    'game_id',  //游戏id
+                    'AVG(rtp) AS rtp', // 平均RTP
+                    'COUNT(DISTINCT uname) as bet_users',  // 投注用户数
+                    'COUNT(id) as bet_count',  // 注单数
+                    'SUM(bet) as bet_amount',  // 注单金额(下注分数)
+                    'SUM(total_win_amount) as game_profit',  // 游戏输赢(金额变化)
+            ])
+            ->order('date', 'desc')
+            ->group('date, game_id')
+            ->page($page, $limit)
+            ->select()
+            ->toArray();
+
+        // 获取登录用户、注册用户    
+        $merchantsUserStatic = MerchantsUserModel::getPlayerStatistics($merchantId, $filters);
+        
+        foreach ($list as &$row)
+        {
+            $row['login_users'] = $merchantsUserStatic[$row['date']]['login_users'] ?? 0;
+            $row['register_users'] = $merchantsUserStatic[$row['date']]['register_users'] ?? 0;
+        }
+        
+        return [
+            'list' => $list,
+            'total' => $total,
+            'page' => $page,
+            'limit' => $limit
+        ];
+    }
+}

+ 8 - 0
route/app.php

@@ -120,3 +120,11 @@ Route::group('game_statis', function () {
     // 游戏每日数据
     Route::get('daily', 'GameStatis/Daily');
 })->middleware([\app\middleware\AuthMiddleware::class, \app\middleware\BehaviorLogMiddleware::class]);
+
+// 排笔榜相关路由
+Route::group('rank_list', function () {
+    // 赢钱榜
+    Route::get('win_amount', 'RankList/WinAmount');
+    // 输钱榜
+    Route::get('lose_amount', 'RankList/LoseAmount');
+})->middleware([\app\middleware\AuthMiddleware::class, \app\middleware\BehaviorLogMiddleware::class]);