GameStatisModel.php 7.9 KB

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