Browse Source

Merge remote-tracking branch 'origin/main'

ssvfdn 3 months ago
parent
commit
a9b642c6c0
3 changed files with 332 additions and 0 deletions
  1. 176 0
      app/controller/GameStatis.php
  2. 152 0
      app/model/GameStatisModel.php
  3. 4 0
      route/app.php

+ 176 - 0
app/controller/GameStatis.php

@@ -74,4 +74,180 @@ class GameStatis extends BaseController
             return json_error([], '获取游戏每日数据失败:' . $e->getMessage());
         }
     }   
+    
+    /**
+     * 游戏输赢汇总
+     */
+    public function Summary()
+    {
+        $userInfo = $this->request->userInfo;
+        
+        // 获取查询参数
+        $page = Request::get('page', 1, 'intval');
+        $limit = Request::get('limit', 10, 'intval');
+        
+        // 筛选条件
+        $filters = [
+            'start_date' => Request::get('start_date', date('Y-m-d'), 'trim'),
+            'end_date' => Request::get('end_date', date('Y-m-d'), 'trim'),
+            'game_id' => Request::get('game_id', '', 'trim'),
+        ];
+        
+        try {
+            // 获取游戏汇总数据
+            $result = GameStatisModel::getGameSummary(
+                $userInfo['merchant_id'],
+                $page, 
+                $limit,
+                $filters
+            );
+            
+            // 获取游戏信息
+            if (!empty($result['list'])) {
+                $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', 'game_platform', 'rtp'])
+                ->select()
+                ->toArray();
+                
+                $gameList = [];
+                foreach ($tempGameList as $game) {
+                    $game_id = $game['game_id'];
+                    $gameList[$game_id] = $game;
+                }
+                
+                // 格式化数据
+                foreach ($result['list'] as &$item) {
+                    $game_id = $item['game_id'];
+                    $gameInfo = $gameList[$game_id] ?? [];
+                    
+                    // 游戏信息
+                    $item['game_title'] = $gameInfo['title'] ?? '未知游戏';
+                    $item['game_platform'] = CommonUtils::getGameTypeConfig($gameInfo['game_platform']);
+                    $item['rtp'] = $gameInfo['rtp'] ?? '';
+
+                    // 计算游戏RTP
+                    $item['game_rtp'] = $item['total_bet'] > 0 ? bcdiv($item['total_score'], $item['total_bet'], 4) * 100 : 0;                    
+                    
+                    // 格式化数据
+                    $item['game_id'] = (int) $item['game_id'];
+                    $item['buy_game_amount'] = (int) $item['buy_game_amount'];
+                    $item['total_bet'] = CommonUtils::convertBalance($item['total_bet'], false);
+                    $item['total_win'] = CommonUtils::convertBalance($item['total_win'], false);
+                    $item['total_score'] = CommonUtils::convertBalance($item['total_score'], false);
+                    
+                    // 计算历史游戏RTP
+                    if (isset($result['history'][$game_id])) {
+                        $history = $result['history'][$game_id];
+                        $item['history_rtp'] = (float) bcdiv($history['total_score'], $history['total_bet'], 4) * 100;
+                    } else {
+                        $item['history_rtp'] = 0;
+                    }
+                }
+            }
+            
+            // 计算汇总数据
+            $summary = [
+                'total_bet' => CommonUtils::convertBalance($result['summary']['total_bet'], false), 
+                'total_bet_count' => $result['summary']['total_bet_count'], 
+                'total_win' => CommonUtils::convertBalance($result['summary']['total_win'], false),
+                'total_user_count' => $result['summary']['total_user_count'], 
+            ];
+            
+            return json_success([
+                'list' => $result['list'],
+                'total' => $result['total'],
+                'page' => $page,
+                'limit' => $limit,
+                'summary' => $summary
+            ], '获取成功');
+            
+        } catch (\Exception $e) {
+            return json_error([], '获取游戏汇总数据失败:' . $e->getMessage());
+        }
+    }
+    
+    /**
+     * 档位押注数据
+     */
+    public function BetLevel()
+    {
+        $userInfo = $this->request->userInfo;
+        
+        // 获取查询参数
+        $page = Request::get('page', 1, 'intval');
+        $limit = Request::get('limit', 10, 'intval');
+        
+        // 筛选条件
+        $filters = [
+            'start_date' => Request::get('start_date', date('Y-m-d'), 'trim'),
+            'end_date' => Request::get('end_date', date('Y-m-d'), 'trim'),
+            'game_id' => Request::get('game_id', '', 'trim'),
+        ];
+        
+        try {
+            // 获取档位押注数据
+            $result = GameStatisModel::getBetLevelData(
+                $userInfo['merchant_id'],
+                $page, 
+                $limit,
+                $filters
+            );
+            
+            // 获取游戏信息
+            if (!empty($result['list'])) {
+                $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', 'game_platform'])
+                ->select()
+                ->toArray();
+                
+                $gameList = [];
+                foreach ($tempGameList as $game) {
+                    $game_id = $game['game_id'];
+                    $gameList[$game_id] = $game;
+                }
+                
+                // 格式化数据
+                foreach ($result['list'] as &$item) {
+                    $game_id = $item['game_id'];
+                    $gameInfo = $gameList[$game_id] ?? [];
+                    
+                    // 游戏信息
+                    $item['game_title'] = $gameInfo['title'] ?? '未知游戏';
+                    $item['game_platform'] = CommonUtils::getGameTypeConfig($gameInfo['game_platform']);
+                    
+                    // 游戏RTP
+                    $item['game_rtp'] = bcmul(bcdiv($item['total_score'], $item['total_bet'], 4), "100", 2);
+
+                     $item['bet'] = CommonUtils::convertBalance($item['bet'], false);
+                    $item['total_bet_rate'] = bcmul(bcdiv($item['total_bet'], $result['total_stats']['total_bet_all'], 4), "100", 2);
+                    $item['total_bet'] = CommonUtils::convertBalance($item['total_bet'], false);
+                    $item['bet_count_rate'] = bcmul(bcdiv((string)$item['bet_count'], (string)$result['total_stats']['total_count_all'], 4), "100", 2);
+                    $item['bet_count'] = ($item['bet_count']);
+                    
+                    // 游戏输赢
+                    $item['total_win'] = CommonUtils::convertBalance($item['total_win'], false);
+                    // 游戏返奖
+                    $item['total_score'] = CommonUtils::convertBalance($item['total_score'], false);                    
+                }
+            }
+            
+            return json_success([
+                'list' => $result['list'],
+                'total' => $result['total'],
+                'page' => $page,
+                'limit' => $limit
+            ], '获取成功');
+            
+        } catch (\Exception $e) {
+            return json_error([], '获取档位押注数据失败:' . $e->getMessage());
+        }
+    }
 }

+ 152 - 0
app/model/GameStatisModel.php

@@ -81,4 +81,156 @@ class GameStatisModel extends Model
             'limit' => $limit
         ];
     }
+    
+    /**
+     * 获取游戏输赢汇总数据
+     */
+    public static function getGameSummary($merchantId, $page = 1, $limit = 20, $filters = [])
+    {
+        $where = [
+            ['app_id', '=', $merchantId],
+            ['action_type', '=', 1] // 只统计下注记录
+        ];
+        
+        // 时间筛选
+        $startTime = !empty($filters['start_date']) ? strtotime($filters['start_date']) : strtotime(date('Y-m-d'));
+        $endTime = !empty($filters['end_date']) ? strtotime($filters['end_date'] . ' 23:59:59') : strtotime(date('Y-m-d 23:59:59'));
+        
+        $where[] = ['create_time', '>=', $startTime];
+        $where[] = ['create_time', '<=', $endTime];
+        
+        // 游戏ID筛选
+        if (!empty($filters['game_id'])) {
+            if (strpos($filters['game_id'], 'All') === false) {
+                $gameId = is_array($filters['game_id']) ? $filters['game_id'] : explode(',', $filters['game_id']);
+                $where[] = ['game_id', 'in', $gameId];
+            }
+        }
+        
+        // 统计当前时间段的数据
+        $subQuery = self::field([
+            'game_id',
+            'COUNT(*) as bet_count', // 注单数
+            'COUNT(DISTINCT user_id) as user_count', // 投注用户数
+            'SUM(bet) as total_bet', // 总投注
+            'SUM(total_win_amount) as total_win', // 总输赢
+            'SUM(is_buy_game) as buy_game_amount', // 购买免费游戏金额
+            'SUM(winning_score) as total_score', // 总赢奖
+        ])
+        ->where($where)
+        ->group('game_id')
+        ->buildSql();
+        
+        // 获取总数
+        $total = self::table($subQuery . ' t')->count();
+        
+        // 获取分页数据
+        $list = self::table($subQuery . ' t')
+            ->order('total_bet', 'desc')
+            ->page($page, $limit)
+            ->select()
+            ->toArray();
+        
+        // 获取游戏历史rtp
+        $historyData = self::table('tp_game_rtp_win_bet')
+        ->where([
+            ['app_id', '=', $merchantId],
+        ])
+        ->field([
+            'game_id',
+            'SUM(bet_amount) as total_bet',
+            'SUM(winning_score) as total_score',
+        ])
+        ->group('game_id')
+        ->select()
+        ->toArray();
+
+        // 获取统计数据
+        $summaryData = self::table('tp_game_rtp_win_bet')
+        ->where([
+            ['app_id', '=', $merchantId],
+        ])
+        ->field([
+            'SUM(bet_amount) as total_bet',
+            'SUM(bet_count) as total_bet_count',
+            'SUM(total_win_amount) as total_win',
+            'SUM(winning_score) as total_score',
+            'COUNT(DISTINCT user_id) as total_user_count'
+        ])
+        ->find()
+        ->toArray();
+                    
+        return [
+            'list' => $list,
+            'total' => $total,
+            'history' => $historyData ? array_column($historyData, null, 'game_id') : [],
+            'summary' => $summaryData,
+        ];
+    }
+    
+    /**
+     * 获取档位押注数据
+     */
+    public static function getBetLevelData($merchantId, $page = 1, $limit = 20, $filters = [])
+    {
+        $where = [
+            ['app_id', '=', $merchantId],
+            ['action_type', '=', 1] // 只统计下注记录
+        ];
+        
+        // 时间筛选
+        $startTime = !empty($filters['start_date']) ? strtotime($filters['start_date']) : strtotime(date('Y-m-d'));
+        $endTime = !empty($filters['end_date']) ? strtotime($filters['end_date'] . ' 23:59:59') : strtotime(date('Y-m-d 23:59:59'));
+        
+        $where[] = ['create_time', '>=', $startTime];
+        $where[] = ['create_time', '<=', $endTime];
+        
+        // 游戏ID筛选
+        if (!empty($filters['game_id'])) {
+            if (strpos($filters['game_id'], 'All') === false && $filters['game_id'] !== '全部') {
+                $gameId = is_array($filters['game_id']) ? $filters['game_id'] : explode(',', $filters['game_id']);
+                $where[] = ['game_id', 'in', $gameId];
+            }
+        }
+
+        // 使用子查询获取每个用户的统计数据
+        $subQuery = self::where($where)
+            ->field([
+                'game_id',
+                'bet',
+                'COUNT(*) as bet_count',
+                'SUM(bet) as total_bet',
+                'SUM(total_win_amount) as total_win',
+                'SUM(winning_score) as total_score'
+            ])
+            ->group('game_id, bet')
+            ->buildSql();
+        
+        // 获取总数
+        $total = self::table($subQuery . ' t')->count();
+
+        // 获取档位数据
+        $levelList = self::table($subQuery . ' t')
+            ->page($page, $limit)
+            ->select()
+            ->toArray();        
+           
+        // 获取总体统计数据(用于计算占比)
+        $totalStats = self::where($where)
+            ->field([
+                'game_id',
+                'SUM(bet) as total_bet_all',
+                'COUNT(*) as total_count_all',
+            ])
+            ->group('game_id')
+            ->find();
+                                
+        return [
+            'list' => $levelList,
+            'total_stats' => $totalStats,
+            'total' => $total,
+            'page' => $page,
+            'limit' => $limit            
+        ];
+    }
 }

+ 4 - 0
route/app.php

@@ -135,6 +135,10 @@ Route::group('merchant_statis', function () {
 Route::group('game_statis', function () {
     // 游戏每日数据
     Route::get('daily', 'GameStatis/Daily');
+    // 游戏输赢汇总
+    Route::get('summary', 'GameStatis/Summary');
+    // 押注档位数据
+    Route::get('bet_level', 'GameStatis/BetLevel');    
 })->middleware([\app\middleware\AuthMiddleware::class, \app\middleware\BehaviorLogMiddleware::class]);
 
 // 走势数据相关路由