GameBetGameModel.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. use think\facade\Db;
  6. /**
  7. * 游戏记录模型 - 基于tp_game_bet_game表
  8. */
  9. class GameBetGameModel extends Model
  10. {
  11. protected $name = 'game_bet_game';
  12. protected $connection = 'fortue_tiger';
  13. protected $pk = 'id';
  14. /**
  15. * 获取游戏记录列表(参考apiAdminBetGameList方法)
  16. */
  17. public static function getBetGameList($appId, $page = 1, $limit = 20, $filters = [])
  18. {
  19. $wheres = [];
  20. $wheres[] = ['action_type', '=', 1]; // 只查询下注记录
  21. $wheres[] = ['app_id', '=', $appId];
  22. // 时间筛选
  23. if (!empty($filters['start_time'])) {
  24. $startTime = strtotime($filters['start_time']);
  25. $wheres[] = ['create_time', '>=', $startTime];
  26. }
  27. if (!empty($filters['end_time'])) {
  28. $endTime = strtotime($filters['end_time']);
  29. $wheres[] = ['create_time', '<=', $endTime];
  30. }
  31. // 游戏ID筛选
  32. if (!empty($filters['game_id'])) {
  33. $wheres[] = ['game_id', '=', $filters['game_id']];
  34. }
  35. // 牌局编号筛选
  36. if (!empty($filters['third_round_id'])) {
  37. $wheres[] = ['third_round_id', '=', $filters['third_round_id']];
  38. }
  39. // 用户ID筛选
  40. if (!empty($filters['player_id'])) {
  41. $wheres[] = ['user_id', '=', $filters['user_id']];
  42. }
  43. // 游戏玩法类型筛选
  44. if (!empty($filters['bet_game_play_type'])) {
  45. if ($filters['bet_game_play_type'] == 2) {
  46. $wheres[] = ['bet_game_play_type', 'in', [1, 2]];
  47. } else {
  48. $wheres[] = ['bet_game_play_type', '=', $filters['bet_game_play_type']];
  49. }
  50. }
  51. $query = self::where($wheres);
  52. // 统计总数
  53. $total = $query->count();
  54. // 获取列表数据(不包含result字段)
  55. $list = $query->withoutField('result')
  56. ->order('id', 'desc')
  57. ->page($page, $limit)
  58. ->select()
  59. ->toArray();
  60. if (empty($list)) {
  61. return [
  62. 'list' => [],
  63. 'total' => $total,
  64. 'page' => $page,
  65. 'limit' => $limit
  66. ];
  67. }
  68. return [
  69. 'list' => $list,
  70. 'total' => $total,
  71. 'page' => $page,
  72. 'limit' => $limit
  73. ];
  74. }
  75. /**
  76. * 获取赢钱榜
  77. */
  78. public static function getWinRanking($appId, $page = 1, $limit = 20, $filters = [])
  79. {
  80. $wheres = [];
  81. $wheres[] = ['action_type', '=', 1];
  82. $wheres[] = ['app_id', '=', $appId];
  83. // 时间筛选
  84. if (!empty($filters['date'])) {
  85. $startTime = strtotime($filters['date']);
  86. $endTime = strtotime($filters['date'] . ' 23:59:59');
  87. $wheres[] = ['create_time', '>=', $startTime];
  88. $wheres[] = ['create_time', '<=', $endTime];
  89. }
  90. // 使用子查询获取每个用户的统计数据
  91. $subQuery = self::field([
  92. 'user_id',
  93. 'SUM(total_win_amount) as total_win', // 总赢钱
  94. 'COUNT(*) as bet_count', // 注单数
  95. 'SUM(bet) as total_bet', // 总投注
  96. ])
  97. ->where($wheres)
  98. ->group('user_id')
  99. ->having('total_win > 0')
  100. ->buildSql();
  101. // echo $subQuery;
  102. // 获取总数
  103. $total = self::table($subQuery . ' t')->count();
  104. // 获取排行榜数据
  105. $list = self::table($subQuery . ' t')
  106. ->order('total_win', 'desc')
  107. ->page($page, $limit)
  108. ->select()
  109. ->toArray();
  110. // 获取用户ID列表
  111. $userIds = array_column($list, 'user_id');
  112. if (!empty($userIds)) {
  113. // 获取用户信息
  114. $users = self::table('tp_merchants_user')
  115. ->whereIn('user_id', $userIds)
  116. ->field(['user_id', 'nickname'])
  117. ->select()
  118. ->toArray();
  119. // 获取用户余额
  120. $userBanlance = self::table('tp_merchants_user_balance')
  121. ->whereIn('user_id', $userIds)
  122. ->field(['user_id', 'balance'])
  123. ->select()
  124. ->toArray();
  125. $users = array_column($users, null, 'user_id');
  126. $userBanlance = array_column($userBanlance, null, 'user_id');
  127. // 补充用户信息和计算RTP
  128. foreach ($list as &$item) {
  129. $userId = (int)$item['user_id'];
  130. $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
  131. $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
  132. // 计算RTP (返还率)
  133. $item['rtp'] = $item['total_bet'] > 0 ? round($item['total_win'] / $item['total_bet'] * 100, 2) : 0;
  134. }
  135. }
  136. return [
  137. 'list' => $list,
  138. 'total' => $total,
  139. 'page' => $page,
  140. 'limit' => $limit
  141. ];
  142. }
  143. /**
  144. * 获取输钱榜
  145. */
  146. public static function getLoseRanking($appId, $page = 1, $limit = 20, $filters = [])
  147. {
  148. $wheres = [];
  149. $wheres[] = ['action_type', '=', 1];
  150. $wheres[] = ['app_id', '=', $appId];
  151. // 时间筛选
  152. if (!empty($filters['date'])) {
  153. $startTime = strtotime($filters['date']);
  154. $endTime = strtotime($filters['date'] . ' 23:59:59');
  155. $wheres[] = ['create_time', '>=', $startTime];
  156. $wheres[] = ['create_time', '<=', $endTime];
  157. }
  158. // 使用子查询获取每个用户的统计数据
  159. $subQuery = self::field([
  160. 'user_id',
  161. 'SUM(total_win_amount) as total_lose', // 总输钱
  162. 'COUNT(*) as bet_count', // 注单数
  163. 'SUM(bet) as total_bet', // 总投注
  164. ])
  165. ->where($wheres)
  166. ->group('user_id')
  167. ->having('total_lose < 0')
  168. ->buildSql();
  169. // 获取总数
  170. $total = self::table($subQuery . ' t')->count();
  171. // 获取排行榜数据
  172. $list = self::table($subQuery . ' t')
  173. ->order('total_lose', 'asc')
  174. ->page($page, $limit)
  175. ->select()
  176. ->toArray();
  177. // 获取用户ID列表
  178. $userIds = array_column($list, 'user_id');
  179. if (!empty($userIds)) {
  180. // 获取用户信息
  181. $users = self::table('tp_merchants_user')
  182. ->whereIn('user_id', $userIds)
  183. ->field(['user_id', 'nickname'])
  184. ->select()
  185. ->toArray();
  186. // 获取用户余额
  187. $userBanlance = self::table('tp_merchants_user_balance')
  188. ->whereIn('user_id', $userIds)
  189. ->field(['user_id', 'balance'])
  190. ->select()
  191. ->toArray();
  192. $users = array_column($users, null, 'user_id');
  193. $userBanlance = array_column($userBanlance, null, 'user_id');
  194. // 补充用户信息和计算RTP
  195. foreach ($list as &$item) {
  196. $userId = (int)$item['user_id'];
  197. $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
  198. $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
  199. // 计算RTP (返还率)
  200. $item['rtp'] = $item['total_bet'] > 0 ? round(abs((int)$item['total_lose']) / $item['total_bet'] * 100, 2) : 0;
  201. }
  202. }
  203. return [
  204. 'list' => $list,
  205. 'total' => $total,
  206. 'page' => $page,
  207. 'limit' => $limit
  208. ];
  209. }
  210. /**
  211. * 获取注单金额榜
  212. */
  213. public static function getBetAmountRanking($appId, $page = 1, $limit = 20, $filters = [])
  214. {
  215. $wheres = [];
  216. $wheres[] = ['action_type', '=', 1];
  217. $wheres[] = ['app_id', '=', $appId];
  218. // 时间筛选
  219. if (!empty($filters['date'])) {
  220. $startTime = strtotime($filters['date']);
  221. $endTime = strtotime($filters['date'] . ' 23:59:59');
  222. $wheres[] = ['create_time', '>=', $startTime];
  223. $wheres[] = ['create_time', '<=', $endTime];
  224. }
  225. // 使用子查询获取每个用户的统计数据
  226. $subQuery = self::field([
  227. 'user_id',
  228. 'SUM(total_win_amount) as total_win', // 总输钱
  229. 'COUNT(*) as bet_count', // 注单数
  230. 'SUM(bet) as total_bet', // 总投注
  231. ])
  232. ->where($wheres)
  233. ->group('user_id')
  234. ->buildSql();
  235. // 获取总数
  236. $total = self::table($subQuery . ' t')->count();
  237. // 获取排行榜数据
  238. $list = self::table($subQuery . ' t')
  239. ->order('bet_count', 'desc')
  240. ->page($page, $limit)
  241. ->select()
  242. ->toArray();
  243. // 获取用户ID列表
  244. $userIds = array_column($list, 'user_id');
  245. if (!empty($userIds)) {
  246. // 获取用户信息
  247. $users = self::table('tp_merchants_user')
  248. ->whereIn('user_id', $userIds)
  249. ->field(['user_id', 'nickname'])
  250. ->select()
  251. ->toArray();
  252. // 获取用户余额
  253. $userBanlance = self::table('tp_merchants_user_balance')
  254. ->whereIn('user_id', $userIds)
  255. ->field(['user_id', 'balance'])
  256. ->select()
  257. ->toArray();
  258. $users = array_column($users, null, 'user_id');
  259. $userBanlance = array_column($userBanlance, null, 'user_id');
  260. // 补充用户信息和计算RTP
  261. foreach ($list as &$item) {
  262. $userId = (int)$item['user_id'];
  263. $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
  264. $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
  265. // 计算RTP (返还率)
  266. $item['rtp'] = $item['total_bet'] > 0 ? round(abs((int)$item['total_win']) / $item['total_bet'] * 100, 2) : 0;
  267. }
  268. }
  269. return [
  270. 'list' => $list,
  271. 'total' => $total,
  272. 'page' => $page,
  273. 'limit' => $limit
  274. ];
  275. }
  276. }