GameBetGameModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use app\common\CommonMerchantUserAndBalance;
  5. use think\Model;
  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 = CommonMerchantUserAndBalance::getMerchantUser($userIds);
  115. // 获取用户余额
  116. $userBanlance = CommonMerchantUserAndBalance::getMerchantUserBalance($userIds);
  117. $users = array_column($users, null, 'user_id');
  118. $userBanlance = array_column($userBanlance, null, 'user_id');
  119. // 补充用户信息和计算RTP
  120. foreach ($list as &$item) {
  121. $userId = (int)$item['user_id'];
  122. $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
  123. $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
  124. // 计算RTP (返还率)
  125. $item['rtp'] = $item['total_bet'] > 0 ? round($item['total_win'] / $item['total_bet'] * 100, 2) : 0;
  126. }
  127. }
  128. return [
  129. 'list' => $list,
  130. 'total' => $total,
  131. 'page' => $page,
  132. 'limit' => $limit
  133. ];
  134. }
  135. /**
  136. * 获取输钱榜
  137. */
  138. public static function getLoseRanking($appId, $page = 1, $limit = 20, $filters = [])
  139. {
  140. $wheres = [];
  141. $wheres[] = ['action_type', '=', 1];
  142. $wheres[] = ['app_id', '=', $appId];
  143. // 时间筛选
  144. if (!empty($filters['date'])) {
  145. $startTime = strtotime($filters['date']);
  146. $endTime = strtotime($filters['date'] . ' 23:59:59');
  147. $wheres[] = ['create_time', '>=', $startTime];
  148. $wheres[] = ['create_time', '<=', $endTime];
  149. }
  150. // 使用子查询获取每个用户的统计数据
  151. $subQuery = self::field([
  152. 'user_id',
  153. 'SUM(total_win_amount) as total_lose', // 总输钱
  154. 'COUNT(*) as bet_count', // 注单数
  155. 'SUM(bet) as total_bet', // 总投注
  156. ])
  157. ->where($wheres)
  158. ->group('user_id')
  159. ->having('total_lose < 0')
  160. ->buildSql();
  161. // 获取总数
  162. $total = self::table($subQuery . ' t')->count();
  163. // 获取排行榜数据
  164. $list = self::table($subQuery . ' t')
  165. ->order('total_lose', 'asc')
  166. ->page($page, $limit)
  167. ->select()
  168. ->toArray();
  169. // 获取用户ID列表
  170. $userIds = array_column($list, 'user_id');
  171. if (!empty($userIds)) {
  172. // 获取用户信息
  173. $users = CommonMerchantUserAndBalance::getMerchantUser($userIds);
  174. // 获取用户余额
  175. $userBanlance = CommonMerchantUserAndBalance::getMerchantUserBalance($userIds);
  176. $users = array_column($users, null, 'user_id');
  177. $userBanlance = array_column($userBanlance, null, 'user_id');
  178. // 补充用户信息和计算RTP
  179. foreach ($list as &$item) {
  180. $userId = (int)$item['user_id'];
  181. $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
  182. $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
  183. // 计算RTP (返还率)
  184. $item['rtp'] = $item['total_bet'] > 0 ? round(abs((int)$item['total_lose']) / $item['total_bet'] * 100, 2) : 0;
  185. }
  186. }
  187. return [
  188. 'list' => $list,
  189. 'total' => $total,
  190. 'page' => $page,
  191. 'limit' => $limit
  192. ];
  193. }
  194. /**
  195. * 获取注单金额榜
  196. */
  197. public static function getBetAmountRanking($appId, $page = 1, $limit = 20, $filters = [])
  198. {
  199. $wheres = [];
  200. $wheres[] = ['action_type', '=', 1];
  201. $wheres[] = ['app_id', '=', $appId];
  202. // 时间筛选
  203. if (!empty($filters['date'])) {
  204. $startTime = strtotime($filters['date']);
  205. $endTime = strtotime($filters['date'] . ' 23:59:59');
  206. $wheres[] = ['create_time', '>=', $startTime];
  207. $wheres[] = ['create_time', '<=', $endTime];
  208. }
  209. // 使用子查询获取每个用户的统计数据
  210. $subQuery = self::field([
  211. 'user_id',
  212. 'SUM(total_win_amount) as total_win', // 总输钱
  213. 'COUNT(*) as bet_count', // 注单数
  214. 'SUM(bet) as total_bet', // 总投注
  215. ])
  216. ->where($wheres)
  217. ->group('user_id')
  218. ->buildSql();
  219. // 获取总数
  220. $total = self::table($subQuery . ' t')->count();
  221. // 获取排行榜数据
  222. $list = self::table($subQuery . ' t')
  223. ->order('bet_count', 'desc')
  224. ->page($page, $limit)
  225. ->select()
  226. ->toArray();
  227. // 获取用户ID列表
  228. $userIds = array_column($list, 'user_id');
  229. if (!empty($userIds)) {
  230. // 获取用户信息
  231. $users = CommonMerchantUserAndBalance::getMerchantUser($userIds);
  232. // 获取用户余额
  233. $userBanlance = CommonMerchantUserAndBalance::getMerchantUserBalance($userIds);
  234. $users = array_column($users, null, 'user_id');
  235. $userBanlance = array_column($userBanlance, null, 'user_id');
  236. // 补充用户信息和计算RTP
  237. foreach ($list as &$item) {
  238. $userId = (int)$item['user_id'];
  239. $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
  240. $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
  241. // 计算RTP (返还率)
  242. $item['rtp'] = $item['total_bet'] > 0 ? round(abs((int)$item['total_win']) / $item['total_bet'] * 100, 2) : 0;
  243. }
  244. }
  245. return [
  246. 'list' => $list,
  247. 'total' => $total,
  248. 'page' => $page,
  249. 'limit' => $limit
  250. ];
  251. }
  252. /**
  253. * 获取游戏每日数据
  254. */
  255. public static function getGameDailySummary($merchantId, $filters = [])
  256. {
  257. $where = [
  258. ['app_id', '=', $merchantId],
  259. ['action_type', '=', 1],
  260. ];
  261. // 时间筛选
  262. if (!empty($filters['start_time'])) {
  263. $startTime = strtotime($filters['start_time']);
  264. $where[] = ['create_time', '>=', $startTime . ' 00:00:00'];
  265. }
  266. if (!empty($filters['end_time'])) {
  267. $endTime = strtotime($filters['end_time']);
  268. $where[] = ['create_time', '<=', $endTime . ' 23:59:59'];
  269. }
  270. if (!empty($filters['game_id'])) {
  271. $gameId = is_array($filters['game_id']) ? $filters['game_id'] : explode(',', $filters['game_id']);
  272. $where[] = ['game_id', 'in', $gameId];
  273. }
  274. $list = self::field([
  275. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  276. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  277. 'COUNT(id) as bet_count', // 注单数
  278. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  279. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  280. 'SUM(is_buy_game) as buy_free_bet_count'
  281. ])
  282. ->where($where)
  283. ->group('date')
  284. ->select()
  285. ->toArray();
  286. return $list;
  287. }
  288. public static function getBuyBetGameList($appId, $filters = []) {
  289. $where = [
  290. ['app_id', '=', $appId],
  291. ['is_buy_game', '=' , 1],
  292. ['action_type', '=' , 1]
  293. ];
  294. $where = array_merge($where, $filters);
  295. $query = self::where($where);
  296. return $query;
  297. }
  298. }