GameBetGameModel.php 13 KB

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