<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * 商户玩家模型
 */
class PlayerModel extends Model
{
    // 设置表名
    protected $name = 'player';

    // 设置主键
    protected $pk = 'player_id';

    // 设置自动时间戳
    protected $autoWriteTimestamp = 'int';

    // 设置字段类型
    protected $type = [
        'player_id' => 'int',
        'nickname' => 'string',
        'merchant_id' => 'int',
        'reg_ip' => 'string',
        'login_ip' => 'string',
        'today_win_amount' => 'float',
        'history_win_amount' => 'float',
        'today_bet_amount' => 'float',
        'balance' => 'float',
        'today_login_count' => 'int',
        'history_login_count' => 'int',
        'adjust_status' => 'int',
        'status' => 'int',
        'login_time' => 'int',
        'create_time' => 'int',
        'update_time' => 'int',
    ];

    // 状态常量
    const STATUS_NORMAL = 0;    // 正常
    const STATUS_FROZEN = 1;    // 冻结
    const STATUS_DELETED = 2;   // 删除

    // 调控状态常量
    const ADJUST_STATUS_NORMAL = 0;     // 正常
    const ADJUST_STATUS_WIN = 1;        // 放水(赢)
    const ADJUST_STATUS_LOSE = 2;       // 收割(输)

    /**
     * 获取状态文本
     */
    public static function getStatusText($status): string
    {
        $statusMap = [
            self::STATUS_NORMAL => '正常',
            self::STATUS_FROZEN => '冻结',
            self::STATUS_DELETED => '删除',
        ];
        return $statusMap[$status] ?? '未知';
    }

    /**
     * 获取调控状态文本
     */
    public static function getAdjustStatusText($adjustStatus): string
    {
        $adjustStatusMap = [
            self::ADJUST_STATUS_NORMAL => '正常',
            self::ADJUST_STATUS_WIN => '放水',
            self::ADJUST_STATUS_LOSE => '收割',
        ];
        return $adjustStatusMap[$adjustStatus] ?? '未知';
    }

    /**
     * 根据商户ID获取玩家列表
     */
    public static function getPlayerListByMerchant($merchantId, $page = 1, $limit = 10, $filters = [])
    {
        $query = self::where('merchant_id', $merchantId);

        // 应用过滤条件
        if (!empty($filters['nickname'])) {
            $query->where('nickname', 'like', '%' . $filters['nickname'] . '%');
        }

        if (!empty($filters['player_id'])) {
            $query->where('player_id', $filters['player_id']);
        }

        if (isset($filters['status']) && $filters['status'] !== '') {
            $query->where('status', $filters['status']);
        }

        if (isset($filters['adjust_status']) && $filters['adjust_status'] !== '') {
            $query->where('adjust_status', $filters['adjust_status']);
        }

        if (!empty($filters['login_ip'])) {
            $query->where('login_ip', 'like', '%' . $filters['login_ip'] . '%');
        }

        if (!empty($filters['reg_ip'])) {
            $query->where('reg_ip', 'like', '%' . $filters['reg_ip'] . '%');
        }

        // 登录时间范围
        if (!empty($filters['login_time_start'])) {
            $query->where('login_time', '>=', strtotime($filters['login_time_start']));
        }
        if (!empty($filters['login_time_end'])) {
            $query->where('login_time', '<=', strtotime($filters['login_time_end']));
        }

        // 排序
        $order = $filters['order'] ?? 'player_id';
        $sort = $filters['sort'] ?? 'desc';
        $query->order($order, $sort);

        // 获取总数
        $total = $query->count();

        // 获取列表
        $list = $query->page($page, $limit)->select();

        return [
            'list' => $list,
            'total' => $total,
            'page' => $page,
            'limit' => $limit
        ];
    }

    /**
     * 获取玩家详情
     */
    public static function getPlayerDetail($playerId, $merchantId)
    {
        return self::where('player_id', $playerId)
            ->where('merchant_id', $merchantId)
            ->find();
    }

    /**
     * 更新玩家状态
     */
    public static function updatePlayerStatus($playerIds, $merchantId, $status)
    {
        return self::where('merchant_id', $merchantId)
            ->whereIn('player_id', $playerIds)
            ->update(['status' => $status]);
    }

    /**
     * 更新玩家调控状态
     */
    public static function updatePlayerAdjustStatus($playerIds, $merchantId, $adjustStatus)
    {
        return self::where('merchant_id', $merchantId)
            ->whereIn('player_id', $playerIds)
            ->update(['adjust_status' => $adjustStatus]);
    }

    /**
     * 获取玩家统计信息
     */
    public static function getPlayerStatistics($merchantId)
    {
        $today = strtotime(date('Y-m-d'));

        return [
            'total_players' => self::where('merchant_id', $merchantId)->count(),
            'active_players' => self::where('merchant_id', $merchantId)
                ->where('status', self::STATUS_NORMAL)
                ->count(),
            'today_new_players' => self::where('merchant_id', $merchantId)
                ->where('create_time', '>=', $today)
                ->count(),
            'today_login_players' => self::where('merchant_id', $merchantId)
                ->where('login_time', '>=', $today)
                ->count(),
            'total_balance' => self::where('merchant_id', $merchantId)->sum('balance'),
            'today_bet_amount' => self::where('merchant_id', $merchantId)->sum('today_bet_amount'),
            'today_win_amount' => self::where('merchant_id', $merchantId)->sum('today_win_amount'),
            'history_win_amount' => self::where('merchant_id', $merchantId)->sum('history_win_amount'),
        ];
    }
}