GameStatisModel.php 7.8 KB

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