| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <?php
- declare (strict_types = 1);
- namespace app\model;
- use think\Model;
- use think\facade\Db;
- /**
- * 游戏记录模型 - 基于tp_game_bet_game表
- */
- class GameBetGameModel extends Model
- {
- protected $name = 'game_bet_game';
- protected $connection = 'fortue_tiger';
- protected $pk = 'id';
-
- /**
- * 获取游戏记录列表(参考apiAdminBetGameList方法)
- */
- public static function getBetGameList($appId, $page = 1, $limit = 20, $filters = [])
- {
- $wheres = [];
- $wheres[] = ['action_type', '=', 1]; // 只查询下注记录
- $wheres[] = ['app_id', '=', $appId];
-
- // 时间筛选
- if (!empty($filters['start_time'])) {
- $startTime = strtotime($filters['start_time']);
- $wheres[] = ['create_time', '>=', $startTime];
- }
-
- if (!empty($filters['end_time'])) {
- $endTime = strtotime($filters['end_time']);
- $wheres[] = ['create_time', '<=', $endTime];
- }
-
- // 游戏ID筛选
- if (!empty($filters['game_id'])) {
- $wheres[] = ['game_id', '=', $filters['game_id']];
- }
-
- // 牌局编号筛选
- if (!empty($filters['third_round_id'])) {
- $wheres[] = ['third_round_id', '=', $filters['third_round_id']];
- }
-
- // 用户ID筛选
- if (!empty($filters['player_id'])) {
- $wheres[] = ['user_id', '=', $filters['user_id']];
- }
-
- // 游戏玩法类型筛选
- if (!empty($filters['bet_game_play_type'])) {
- if ($filters['bet_game_play_type'] == 2) {
- $wheres[] = ['bet_game_play_type', 'in', [1, 2]];
- } else {
- $wheres[] = ['bet_game_play_type', '=', $filters['bet_game_play_type']];
- }
- }
-
- $query = self::where($wheres);
-
- // 统计总数
- $total = $query->count();
-
- // 获取列表数据(不包含result字段)
- $list = $query->withoutField('result')
- ->order('id', 'desc')
- ->page($page, $limit)
- ->select()
- ->toArray();
-
- if (empty($list)) {
- return [
- 'list' => [],
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ];
- }
-
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ];
- }
-
- /**
- * 获取赢钱榜
- */
- public static function getWinRanking($appId, $page = 1, $limit = 20, $filters = [])
- {
- $wheres = [];
- $wheres[] = ['action_type', '=', 1];
- $wheres[] = ['app_id', '=', $appId];
-
- // 时间筛选
- if (!empty($filters['date'])) {
- $startTime = strtotime($filters['date']);
- $endTime = strtotime($filters['date'] . ' 23:59:59');
- $wheres[] = ['create_time', '>=', $startTime];
- $wheres[] = ['create_time', '<=', $endTime];
- }
-
- // 使用子查询获取每个用户的统计数据
- $subQuery = self::field([
- 'user_id',
- 'SUM(total_win_amount) as total_win', // 总赢钱
- 'COUNT(*) as bet_count', // 注单数
- 'SUM(bet) as total_bet', // 总投注
- ])
- ->where($wheres)
- ->group('user_id')
- ->having('total_win > 0')
- ->buildSql();
-
- // echo $subQuery;
- // 获取总数
- $total = self::table($subQuery . ' t')->count();
- // 获取排行榜数据
- $list = self::table($subQuery . ' t')
- ->order('total_win', 'desc')
- ->page($page, $limit)
- ->select()
- ->toArray();
-
- // 获取用户ID列表
- $userIds = array_column($list, 'user_id');
-
- if (!empty($userIds)) {
- // 获取用户信息
- $users = MerchantUserAndBalanceModel::getMerchantUser($userIds);
- // 获取用户余额
- $userBanlance = MerchantUserAndBalanceModel::getMerchantUserBalance($userIds);
- $users = array_column($users, null, 'user_id');
- $userBanlance = array_column($userBanlance, null, 'user_id');
-
- // 补充用户信息和计算RTP
- foreach ($list as &$item) {
- $userId = (int)$item['user_id'];
- $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
- $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
- // 计算RTP (返还率)
- $item['rtp'] = $item['total_bet'] > 0 ? round($item['total_win'] / $item['total_bet'] * 100, 2) : 0;
- }
- }
-
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ];
- }
-
- /**
- * 获取输钱榜
- */
- public static function getLoseRanking($appId, $page = 1, $limit = 20, $filters = [])
- {
- $wheres = [];
- $wheres[] = ['action_type', '=', 1];
- $wheres[] = ['app_id', '=', $appId];
-
- // 时间筛选
- if (!empty($filters['date'])) {
- $startTime = strtotime($filters['date']);
- $endTime = strtotime($filters['date'] . ' 23:59:59');
- $wheres[] = ['create_time', '>=', $startTime];
- $wheres[] = ['create_time', '<=', $endTime];
- }
-
- // 使用子查询获取每个用户的统计数据
- $subQuery = self::field([
- 'user_id',
- 'SUM(total_win_amount) as total_lose', // 总输钱
- 'COUNT(*) as bet_count', // 注单数
- 'SUM(bet) as total_bet', // 总投注
- ])
- ->where($wheres)
- ->group('user_id')
- ->having('total_lose < 0')
- ->buildSql();
-
- // 获取总数
- $total = self::table($subQuery . ' t')->count();
- // 获取排行榜数据
- $list = self::table($subQuery . ' t')
- ->order('total_lose', 'asc')
- ->page($page, $limit)
- ->select()
- ->toArray();
-
- // 获取用户ID列表
- $userIds = array_column($list, 'user_id');
-
- if (!empty($userIds)) {
- // 获取用户信息
- $users = MerchantUserAndBalanceModel::getMerchantUser($userIds);
- // 获取用户余额
- $userBanlance = MerchantUserAndBalanceModel::getMerchantUserBalance($userIds);
- $users = array_column($users, null, 'user_id');
- $userBanlance = array_column($userBanlance, null, 'user_id');
-
- // 补充用户信息和计算RTP
- foreach ($list as &$item) {
- $userId = (int)$item['user_id'];
- $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
- $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
- // 计算RTP (返还率)
- $item['rtp'] = $item['total_bet'] > 0 ? round(abs((int)$item['total_lose']) / $item['total_bet'] * 100, 2) : 0;
- }
- }
-
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ];
- }
-
- /**
- * 获取注单金额榜
- */
- public static function getBetAmountRanking($appId, $page = 1, $limit = 20, $filters = [])
- {
- $wheres = [];
- $wheres[] = ['action_type', '=', 1];
- $wheres[] = ['app_id', '=', $appId];
-
- // 时间筛选
- if (!empty($filters['date'])) {
- $startTime = strtotime($filters['date']);
- $endTime = strtotime($filters['date'] . ' 23:59:59');
- $wheres[] = ['create_time', '>=', $startTime];
- $wheres[] = ['create_time', '<=', $endTime];
- }
-
- // 使用子查询获取每个用户的统计数据
- $subQuery = self::field([
- 'user_id',
- 'SUM(total_win_amount) as total_win', // 总输钱
- 'COUNT(*) as bet_count', // 注单数
- 'SUM(bet) as total_bet', // 总投注
- ])
- ->where($wheres)
- ->group('user_id')
- ->buildSql();
-
- // 获取总数
- $total = self::table($subQuery . ' t')->count();
- // 获取排行榜数据
- $list = self::table($subQuery . ' t')
- ->order('bet_count', 'desc')
- ->page($page, $limit)
- ->select()
- ->toArray();
-
- // 获取用户ID列表
- $userIds = array_column($list, 'user_id');
-
- if (!empty($userIds)) {
- // 获取用户信息
- $users = MerchantUserAndBalanceModel::getMerchantUser($userIds);
- // 获取用户余额
- $userBanlance = MerchantUserAndBalanceModel::getMerchantUserBalance($userIds);
- $users = array_column($users, null, 'user_id');
- $userBanlance = array_column($userBanlance, null, 'user_id');
-
- // 补充用户信息和计算RTP
- foreach ($list as &$item) {
- $userId = (int)$item['user_id'];
- $item['nickname'] = $users[$userId]['nickname'] ?? '未知用户';
- $item['balance'] = $userBanlance[$userId]['balance'] ?? 0;
- // 计算RTP (返还率)
- $item['rtp'] = $item['total_bet'] > 0 ? round(abs((int)$item['total_win']) / $item['total_bet'] * 100, 2) : 0;
- }
- }
-
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ];
- }
- }
|