<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * 玩家登录日志模型
 */
class PlayerLoginLogModel extends Model
{
    // 设置数据库连接
    protected $connection = 'fortue_tiger';    
    // 设置表名
    protected $name = 'player_login_log';

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

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

    // 设置字段类型
    protected $type = [
        'merchant_id' => 'int',
        'player_id' => 'int',
        'game_id' => 'int',
        'game_platform' => 'string',
        'login_ip' => 'string',
        'login_time' => 'int',
        'login_status' => 'int',
    ];

    /**
     * 获取查询条件
     */
    private static function _getWhere($query, $filter) {
        // 处理可选过滤条件
        if (!empty($filter['game_id'])) {
            $query->where('game_id', $filter['game_id']);
        }
        
        if (!empty($filter['game_platform'])) {
            $query->where('game_platform', $filter['game_platform']);
        }
        
        if (!empty($filter['login_status'])) {
            $query->where('login_status', $filter['login_status']);
        }
        
        if (!empty($filter['start_time'])) {
            $startTime = strtotime($filter['start_time']);
            $query->where('login_time', '>=', $startTime);
        }
        
        if (!empty($filter['end_time'])) {
            $endTime = strtotime($filter['end_time']);
            $query->where('login_time', '<=', $endTime);
        }
        
        if (!empty($filter['login_ip'])) {
            $query->where('login_ip', $filter['login_ip']);
        }
        return $query;
    }

    /**
     * 获取每日登录用户数
     */
    public static function getLoginDateCount($merchant_id, $filter = []) {
        $query = self::where('merchant_id', $merchant_id);
        // 按player_id分组统计
        $result = self::_getWhere($query, $filter)->field("FROM_UNIXTIME(login_time, '%Y-%m-%d') as date, COUNT(*) as count")
                       ->group('date')
                       ->select()
                       ->toArray();
        return array_column($result, null, 'date');
    }

    /**
     * 获取每小时登录用户数
     */
    public static function getLoginHourCount($merchant_id, $filter = []) {
        $query = self::where('merchant_id', $merchant_id);
        // 按player_id分组统计
        $result = self::_getWhere($query, $filter)->field("FROM_UNIXTIME(login_time, '%Y%m%d%H') as hour, COUNT(*) as count")
                       ->group('hour')
                       ->select()
                       ->toArray();
        return $result;
    }    

    /**
     * 获取玩家登录次数
     */
    public static function getLoginPlayerCount($merchant_id, $player_ids, $filter = []) {
        // 支持单个或多个player_id
        $player_ids = is_array($player_ids) ? $player_ids : [$player_ids];
        $query = self::where('merchant_id', $merchant_id)
                     ->whereIn('player_id', $player_ids);
        
        // 按player_id分组统计
        $result = self::_getWhere($query, $filter)->field('player_id, COUNT(*) as count')
                       ->group('player_id')
                       ->select()
                       ->toArray();
        return array_column($result, null, 'player_id');
    }
}