| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- declare (strict_types = 1);
- namespace app\model;
- use think\Model;
- use think\facade\Db;
- /**
- * 游戏记录模型
- */
- class GameRecordModel extends Model
- {
- protected $name = 'game_lottery_prizes';
- protected $connection = 'fortune_tiger';
- protected $pk = 'id';
-
- // 动作类型常量
- const ACTION_TYPE_BET = 1; // 下注
- const ACTION_TYPE_WIN = 2; // 中奖
- const ACTION_TYPE_RETURN = 3; // 退还
-
- // 状态常量
- const STATUS_PENDING = 0; // 待处理
- const STATUS_SUCCESS = 1; // 成功
- const STATUS_FAILED = 2; // 失败
-
- /**
- * 获取游戏记录列表
- */
- public static function getGameRecords($appId, $page = 1, $limit = 20, $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];
- }
-
- // 平台ID筛选
- if (!empty($filters['platform_id'])) {
- $where[] = ['user_id', '=', $filters['platform_id']];
- }
-
- // 游戏筛选
- if (!empty($filters['game_id'])) {
- $where[] = ['game_id', '=', $filters['game_id']];
- }
-
- // 玩家状态筛选
- if ($filters['play_status'] !== '') {
- $where[] = ['status', '=', $filters['play_status']];
- }
-
- // 玩法筛选
- if (!empty($filters['play_method'])) {
- $where[] = ['bet_game_play_type', '=', $filters['play_method']];
- }
-
- // 触发方式筛选
- if (!empty($filters['trigger_method'])) {
- $where[] = ['action_type', '=', $filters['trigger_method']];
- }
-
- // 牌局编号筛选
- if (!empty($filters['round_number'])) {
- $where[] = ['third_round_id', 'like', '%' . $filters['round_number'] . '%'];
- }
-
- $query = self::where($where);
-
- // 统计总数
- $total = $query->count();
-
- // 获取列表数据
- $list = $query->field([
- 'id', 'user_id', 'uname', 'nickname', 'third_gid', 'third_round_id',
- 'game_id', 'game_type', 'bet_game_play_type', 'amount', 'bet_amount',
- 'prev_amount', 'next_amount', 'balance_amount', 'action_type', 'status',
- 'ip', 'create_time'
- ])
- ->order('create_time', 'desc')
- ->page($page, $limit)
- ->select()
- ->toArray();
-
- // 格式化数据
- foreach ($list as &$item) {
- // 格式化金额(除以10000)
- $item['amount'] = bcdiv((string)$item['amount'], '10000', 2);
- $item['bet_amount'] = bcdiv((string)$item['bet_amount'], '10000', 2);
- $item['prev_amount'] = bcdiv((string)$item['prev_amount'], '10000', 2);
- $item['next_amount'] = bcdiv((string)$item['next_amount'], '10000', 2);
- $item['balance_amount'] = bcdiv((string)$item['balance_amount'], '10000', 2);
-
- // 状态文本
- $item['status_text'] = self::getStatusText($item['status']);
-
- // 动作类型文本
- $item['action_type_text'] = self::getActionTypeText($item['action_type']);
- }
-
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ];
- }
-
- /**
- * 获取游戏记录详情
- */
- public static function getGameRecordDetail($id, $appId)
- {
- $record = self::where('id', $id)
- ->where('app_id', $appId)
- ->find();
-
- if (!$record) {
- return null;
- }
-
- $record = $record->toArray();
-
- // 格式化数据
- $record['create_time_text'] = date('Y-m-d H:i:s', $record['create_time']);
- $record['update_time_text'] = date('Y-m-d H:i:s', $record['update_time']);
-
- // 格式化金额
- $record['amount_yuan'] = bcdiv((string)$record['amount'], '10000', 2);
- $record['bet_amount_yuan'] = bcdiv((string)$record['bet_amount'], '10000', 2);
- $record['prev_amount_yuan'] = bcdiv((string)$record['prev_amount'], '10000', 2);
- $record['next_amount_yuan'] = bcdiv((string)$record['next_amount'], '10000', 2);
- $record['balance_amount_yuan'] = bcdiv((string)$record['balance_amount'], '10000', 2);
-
- // 状态文本
- $record['status_text'] = self::getStatusText($record['status']);
-
- // 动作类型文本
- $record['action_type_text'] = self::getActionTypeText($record['action_type']);
-
- // 解析开奖结果
- $record['result_data'] = !empty($record['result']) ? json_decode($record['result'], true) : [];
-
- return $record;
- }
-
- /**
- * 获取游戏统计信息
- */
- public static function getGameStatistics($appId, $filters = [])
- {
- $where = [
- ['app_id', '=', $appId]
- ];
-
- // 时间筛选
- 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(CASE WHEN action_type = 1 THEN bet_amount ELSE 0 END) as total_bet_amount',
- 'SUM(CASE WHEN action_type = 2 THEN amount ELSE 0 END) as total_win_amount',
- 'SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as success_records',
- 'SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as failed_records'
- ])
- ->find()
- ->toArray();
-
- // 格式化金额
- $statistics['total_bet_amount_yuan'] = bcdiv((string)$statistics['total_bet_amount'], '10000', 2);
- $statistics['total_win_amount_yuan'] = bcdiv((string)$statistics['total_win_amount'], '10000', 2);
- $net_amount = bcsub((string)$statistics['total_bet_amount'], (string)$statistics['total_win_amount']);
- $statistics['net_amount_yuan'] = bcdiv($net_amount, '10000', 2);
-
- // 计算成功率
- if ($statistics['total_records'] > 0) {
- $rate = bcdiv((string)$statistics['success_records'], (string)$statistics['total_records'], 4);
- $statistics['success_rate'] = bcmul($rate, '100', 2);
- } else {
- $statistics['success_rate'] = '0.00';
- }
-
- return $statistics;
- }
-
- /**
- * 获取所有游戏ID列表
- */
- public static function getAllGameIds($appId)
- {
- return self::where('app_id', $appId)
- ->distinct(true)
- ->column('game_id');
- }
-
- /**
- * 获取所有游戏玩法列表
- */
- public static function getAllPlayMethods($appId)
- {
- return self::where('app_id', $appId)
- ->distinct(true)
- ->column('bet_game_play_type');
- }
-
- /**
- * 获取状态文本
- */
- public static function getStatusText($status)
- {
- $statusMap = [
- self::STATUS_PENDING => '待处理',
- self::STATUS_SUCCESS => '成功',
- self::STATUS_FAILED => '失败'
- ];
-
- return $statusMap[$status] ?? '未知';
- }
-
- /**
- * 获取动作类型文本
- */
- public static function getActionTypeText($actionType)
- {
- $actionTypeMap = [
- self::ACTION_TYPE_BET => '下注',
- self::ACTION_TYPE_WIN => '中奖',
- self::ACTION_TYPE_RETURN => '退还'
- ];
-
- return $actionTypeMap[$actionType] ?? '其他';
- }
- }
|