| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- declare (strict_types = 1);
- namespace app\model;
- use think\Model;
- use think\facade\Db;
- /**
- * 游戏记录模型 - 基于tp_game_bet_game表
- */
- class GameRecordModel extends Model
- {
- protected $name = 'game_bet_game';
- protected $connection = 'fortue_tiger';
- protected $pk = 'id';
-
- // 动作类型常量
- const ACTION_TYPE_BET = 1; // 下注
- const ACTION_TYPE_RESULT = 2; // 结算
-
- // 状态常量
- const STATUS_PENDING = 0; // 待处理
- const STATUS_SUCCESS = 1; // 成功
- const STATUS_FAILED = 2; // 失败
-
- // 游戏类型常量
- const GAME_TYPE_SLOT = 1; // 老虎机
- const GAME_TYPE_TABLE = 2; // 桌面游戏
- const GAME_TYPE_LIVE = 3; // 真人游戏
-
- /**
- * 获取游戏记录列表(参考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'] . ' 00:00:00');
- $wheres[] = ['create_time', '>=', $startTime];
- }
-
- if (!empty($filters['end_time'])) {
- $endTime = strtotime($filters['end_time'] . ' 23:59:59');
- $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
- ];
- }
-
- /**
- * 获取子订单列表(action_type = 2的记录)
- */
- public static function getChildBetGameOrders($thirdGids, $appId)
- {
- if (empty($thirdGids)) {
- return [];
- }
-
- $childOrders = self::where([
- ['third_gid', 'in', $thirdGids],
- ['action_type', '=', 2],
- ['app_id', '=', $appId]
- ])
- ->withoutField('result')
- ->select()
- ->toArray();
-
- return $childOrders;
- }
-
- /**
- * 获取商户信息映射
- */
- private static function getMerchantsInfoMap($appIds)
- {
- if (empty($appIds)) {
- return [];
- }
-
- // 这里可以从商户表获取信息,暂时返回基本信息
- $map = [];
- foreach ($appIds as $appId) {
- $map[$appId] = [
- 'app_id' => $appId,
- 'nickname' => '商户' . $appId
- ];
- }
-
- return $map;
- }
-
- /**
- * 获取游戏记录详情
- */
- public static function getGameRecordDetail($id, $appId)
- {
- $record = self::where('id', $id)
- ->where('app_id', $appId)
- ->find();
-
- if (!$record) {
- return null;
- }
-
- $record = $record->toArray();
-
- // 获取商户信息
- $merchantsMap = self::getMerchantsInfoMap([$record['app_id']]);
- $record = self::formatBetGameData($record, $merchantsMap);
-
- // 获取相关的子订单
- if (!empty($record['third_gid'])) {
- $childOrders = self::getChildBetGameOrders([$record['third_gid']], $appId);
- $record['children'] = $childOrders;
- }
-
- return $record;
- }
-
- /**
- * 获取游戏统计信息
- */
- public static function getGameStatistics($appId, $filters = [])
- {
- $where = [
- ['app_id', '=', $appId],
- ['action_type', '=', 1]
- ];
-
- // 时间筛选
- if (!empty($filters['start_time'])) {
- $startTime = strtotime($filters['start_time'] . ' 00:00:00');
- $where[] = ['create_time', '>=', $startTime];
- }
-
- if (!empty($filters['end_time'])) {
- $endTime = strtotime($filters['end_time'] . ' 23:59:59');
- $where[] = ['create_time', '<=', $endTime];
- }
-
- $statistics = self::where($where)
- ->field([
- 'COUNT(*) as total_records',
- 'COUNT(DISTINCT uname) as total_players',
- 'SUM(bet) as total_bet_amount',
- 'SUM(total_win_amount) as total_win_amount',
- 'COUNT(DISTINCT game_id) as total_games'
- ])
- ->find()
- ->toArray();
-
- // 格式化金额
- $statistics['total_bet_amount_yuan'] = self::convertBalance($statistics['total_bet_amount']);
- $statistics['total_win_amount_yuan'] = self::convertBalance($statistics['total_win_amount']);
- $net_amount = $statistics['total_bet_amount'] - $statistics['total_win_amount'];
- $statistics['net_amount_yuan'] = self::convertBalance($net_amount);
-
- // 计算RTP
- if ($statistics['total_bet_amount'] > 0) {
- $statistics['rtp'] = round(($statistics['total_win_amount'] / $statistics['total_bet_amount']) * 100, 2);
- } else {
- $statistics['rtp'] = 0;
- }
-
- return $statistics;
- }
-
- /**
- * 获取所有游戏ID列表
- */
- public static function getAllGameIds($appId)
- {
- return self::where('app_id', $appId)
- ->where('action_type', 1)
- ->distinct(true)
- ->column('game_id');
- }
-
- /**
- * 获取所有游戏玩法列表
- */
- public static function getAllPlayMethods($appId)
- {
- return self::where('app_id', $appId)
- ->where('action_type', 1)
- ->distinct(true)
- ->column('bet_game_play_type');
- }
- }
|