GameStatisModel.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. use think\facade\Db;
  6. /**
  7. * 游戏数据统计
  8. */
  9. class GameStatisModel extends Model
  10. {
  11. protected $name = 'game_bet_game';
  12. // 连接到fortue_tiger数据库
  13. protected $connection = 'fortue_tiger';
  14. /**
  15. * 获取游戏每日数据列表
  16. */
  17. public static function getGameDailyList($merchantId, $page = 1, $limit = 20, $filters = [])
  18. {
  19. $where = [
  20. ['app_id', '=', $merchantId]
  21. ];
  22. // 时间筛选
  23. if (!empty($filters['start_time'])) {
  24. $startTime = strtotime($filters['start_time']);
  25. $where[] = ['create_time', '>=', $startTime];
  26. }
  27. if (!empty($filters['end_time'])) {
  28. $endTime = strtotime($filters['end_time']);
  29. $where[] = ['create_time', '<=', $endTime];
  30. }
  31. if (!empty($filters['game_id'])) {
  32. $gameId = is_array($filters['game_id']) ? $filters['game_id'] : explode(',', $filters['game_id']);
  33. $where[] = ['game_id', 'in', $gameId];
  34. }
  35. // 使用子查询获取每个用户的统计数据
  36. $subQuery = self::field([
  37. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  38. 'game_id', //游戏id
  39. 'AVG(rtp) AS rtp', // 平均RTP
  40. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  41. 'COUNT(id) as bet_count', // 注单数
  42. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  43. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  44. ])
  45. ->where($where)
  46. ->group('date, game_id')
  47. ->buildSql();
  48. // 获取总数
  49. $total = self::table($subQuery . ' t')->count();
  50. // 获取排行榜数据
  51. $list = self::table($subQuery . ' t')
  52. ->order('date', 'desc')
  53. ->page($page, $limit)
  54. ->select()
  55. ->toArray();
  56. // 获取登录用户、注册用户
  57. $merchantsUserStatic = MerchantsUserModel::getPlayerStatistics($merchantId, $filters);
  58. foreach ($list as &$row)
  59. {
  60. $row['login_users'] = $merchantsUserStatic[$row['date']]['login_users'] ?? 0;
  61. $row['register_users'] = $merchantsUserStatic[$row['date']]['register_users'] ?? 0;
  62. }
  63. return [
  64. 'list' => $list,
  65. 'total' => $total,
  66. 'page' => $page,
  67. 'limit' => $limit
  68. ];
  69. }
  70. /**
  71. * 获取游戏输赢汇总数据
  72. */
  73. public static function getGameSummary($merchantId, $page = 1, $limit = 20, $filters = [])
  74. {
  75. $where = [
  76. ['app_id', '=', $merchantId],
  77. ['action_type', '=', 1] // 只统计下注记录
  78. ];
  79. // 时间筛选
  80. $startTime = !empty($filters['start_date']) ? strtotime($filters['start_date']) : strtotime(date('Y-m-d'));
  81. $endTime = !empty($filters['end_date']) ? strtotime($filters['end_date'] . ' 23:59:59') : strtotime(date('Y-m-d 23:59:59'));
  82. $where[] = ['create_time', '>=', $startTime];
  83. $where[] = ['create_time', '<=', $endTime];
  84. // 游戏ID筛选
  85. if (!empty($filters['game_id'])) {
  86. if (strpos($filters['game_id'], 'All') === false) {
  87. $gameId = is_array($filters['game_id']) ? $filters['game_id'] : explode(',', $filters['game_id']);
  88. $where[] = ['game_id', 'in', $gameId];
  89. }
  90. }
  91. // 统计当前时间段的数据
  92. $subQuery = self::field([
  93. 'game_id',
  94. 'COUNT(*) as bet_count', // 注单数
  95. 'COUNT(DISTINCT user_id) as user_count', // 投注用户数
  96. 'SUM(bet) as total_bet', // 总投注
  97. 'SUM(total_win_amount) as total_win', // 总输赢
  98. 'SUM(is_buy_game) as buy_game_amount', // 购买免费游戏金额
  99. 'SUM(winning_score) as total_score', // 总赢奖
  100. ])
  101. ->where($where)
  102. ->group('game_id')
  103. ->buildSql();
  104. // 获取总数
  105. $total = self::table($subQuery . ' t')->count();
  106. // 获取分页数据
  107. $list = self::table($subQuery . ' t')
  108. ->order('total_bet', 'desc')
  109. ->page($page, $limit)
  110. ->select()
  111. ->toArray();
  112. // 获取游戏历史rtp
  113. $historyData = self::table('tp_game_rtp_win_bet')
  114. ->where([
  115. ['app_id', '=', $merchantId],
  116. ])
  117. ->field([
  118. 'game_id',
  119. 'SUM(bet_amount) as total_bet',
  120. 'SUM(winning_score) as total_score',
  121. ])
  122. ->group('game_id')
  123. ->select()
  124. ->toArray();
  125. // 获取统计数据
  126. $summaryData = self::table('tp_game_rtp_win_bet')
  127. ->where([
  128. ['app_id', '=', $merchantId],
  129. ])
  130. ->field([
  131. 'SUM(bet_amount) as total_bet',
  132. 'SUM(bet_count) as total_bet_count',
  133. 'SUM(total_win_amount) as total_win',
  134. 'SUM(winning_score) as total_score',
  135. 'COUNT(DISTINCT user_id) as total_user_count'
  136. ])
  137. ->find()
  138. ->toArray();
  139. return [
  140. 'list' => $list,
  141. 'total' => $total,
  142. 'history' => $historyData ? array_column($historyData, null, 'game_id') : [],
  143. 'summary' => $summaryData,
  144. ];
  145. }
  146. /**
  147. * 获取档位押注数据
  148. */
  149. public static function getBetLevelData($merchantId, $page = 1, $limit = 20, $filters = [])
  150. {
  151. $where = [
  152. ['app_id', '=', $merchantId],
  153. ['action_type', '=', 1] // 只统计下注记录
  154. ];
  155. // 时间筛选
  156. $startTime = !empty($filters['start_date']) ? strtotime($filters['start_date']) : strtotime(date('Y-m-d'));
  157. $endTime = !empty($filters['end_date']) ? strtotime($filters['end_date'] . ' 23:59:59') : strtotime(date('Y-m-d 23:59:59'));
  158. $where[] = ['create_time', '>=', $startTime];
  159. $where[] = ['create_time', '<=', $endTime];
  160. // 游戏ID筛选
  161. if (!empty($filters['game_id'])) {
  162. if (strpos($filters['game_id'], 'All') === false && $filters['game_id'] !== '全部') {
  163. $gameId = is_array($filters['game_id']) ? $filters['game_id'] : explode(',', $filters['game_id']);
  164. $where[] = ['game_id', 'in', $gameId];
  165. }
  166. }
  167. // 使用子查询获取每个用户的统计数据
  168. $subQuery = self::where($where)
  169. ->field([
  170. 'game_id',
  171. 'bet',
  172. 'COUNT(*) as bet_count',
  173. 'SUM(bet) as total_bet',
  174. 'SUM(total_win_amount) as total_win',
  175. 'SUM(winning_score) as total_score'
  176. ])
  177. ->group('game_id, bet')
  178. ->buildSql();
  179. // 获取总数
  180. $total = self::table($subQuery . ' t')->count();
  181. // 获取档位数据
  182. $levelList = self::table($subQuery . ' t')
  183. ->page($page, $limit)
  184. ->select()
  185. ->toArray();
  186. // 获取总体统计数据(用于计算占比)
  187. $totalStats = self::where($where)
  188. ->field([
  189. 'game_id',
  190. 'SUM(bet) as total_bet_all',
  191. 'COUNT(*) as total_count_all',
  192. ])
  193. ->group('game_id')
  194. ->find();
  195. return [
  196. 'list' => $levelList,
  197. 'total_stats' => $totalStats,
  198. 'total' => $total,
  199. 'page' => $page,
  200. 'limit' => $limit
  201. ];
  202. }
  203. }