<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\domain\system;

use app\repository\system\ConfigRepository;
use app\service\StorageService;
use Core\service\BaseCoreService;
use app\service\domain\queue\QueueConfigService;
use think\Request;

/**
 *
 * Class CoreConfigService
 * @package app\service\domain\system
 */
/**
 * 系统配置服务
 * 管理系统基础配置：站点信息、短信邮件、队列存储、安全策略等
 * @package app\service\domain\system
 */
class CoreConfigService extends BaseCoreService
{
    /**
     * 获取管理员配置
     * @param Request $request
     * @return array
     */
    public function getAdminConfig(Request $request): array
    {
        return [
            'basic' => [
                'platform_name' => $this->value('platform_name', ''),
                'admin_copyright' => $this->value('admin_copyright', ''),
                'service_phone' => $this->value('service_phone', ''),
                'service_wechat' => $this->value('service_wechat', ''),
                'service_time' => $this->value('service_time', ''),
                'service_qrcode' => $this->asset($request, 'service_qrcode'),
                'logo_rect' => $this->asset($request, 'logo_rect'),
                'logo_square' => $this->asset($request, 'logo_square'),
                'login_bg_image' => $this->asset($request, 'login_bg_image'),
                'maintenance_enabled' => $this->intValue('maintenance_enabled', 0),
                'maintenance_notice' => $this->value('maintenance_notice', ''),
                'auto_cancel_unpaid_enabled' => $this->intValue('auto_cancel_unpaid_enabled', 1),
                'auto_cancel_unpaid_minutes' => $this->intValue('auto_cancel_unpaid_minutes', 15),
                'register_enabled' => $this->intValue('register_enabled', 1),
                'image_compress_threshold' => $this->intValue('image_compress_threshold', 500),
                'image_thumb_sizes' => $this->jsonValue('image_thumb_sizes', '{"small":[150,150],"medium":[400,400],"large":[800,800]}'),
                'version' => $this->coreVersion(),
            ],
            'agreement' => [
                'service_agreement' => $this->value('service_agreement', ''),
                'privacy_agreement' => $this->value('privacy_agreement', ''),
            ],
            'sms' => [
                'sms_verify_enabled' => $this->intValue('sms_verify_enabled', 0),
                'sms_code_ttl_seconds' => $this->intValue('sms_code_ttl_seconds', 300),
                'provider' => $this->value('sms_provider', 'tencent'),
                'tencent' => [
                    'secret_id' => $this->value('sms_tencent_secret_id', ''),
                    'secret_key' => $this->value('sms_tencent_secret_key', ''),
                    'app_id' => $this->value('sms_tencent_app_id', ''),
                    'sign_name' => $this->value('sms_tencent_sign_name', ''),
                ],
                'aliyun' => [
                    'access_key_id' => $this->value('sms_aliyun_access_key_id', ''),
                    'access_key_secret' => $this->value('sms_aliyun_access_key_secret', ''),
                    'sign_name' => $this->value('sms_aliyun_sign_name', ''),
                ],
            ],
            'email' => [
                'smtp_host' => $this->value('email_smtp_host', ''),
                'smtp_port' => $this->value('email_smtp_port', '465'),
                'smtp_user' => $this->value('email_smtp_user', ''),
                'smtp_pass' => $this->value('email_smtp_pass', ''),
                'sender_name' => $this->value('email_sender_name', ''),
            ],
            'queue' => [
                'enabled' => $this->queueConfig()->isEnabled() ? 1 : 0,
                'driver' => $this->queueConfig()->driver(),
                'default_queue' => $this->queueConfig()->defaultQueue(),
                'retry_delay_seconds' => $this->queueConfig()->retryDelaySeconds(),
                'reserve_seconds' => $this->queueConfig()->reserveSeconds(),
                'max_attempts' => $this->queueConfig()->maxAttempts(),
                'work_sleep_seconds' => $this->queueConfig()->workSleepSeconds(),
                'fallback_to_sync' => $this->queueConfig()->fallbackToSync() ? 1 : 0,
                'database_table' => $this->queueConfig()->databaseTable(),
                'redis' => $this->queueConfig()->redisConfig(),
            ],
            'storage' => [
                'driver' => $this->value('storage_driver', 'local'),
                'aliyun_oss' => [
                    'access_key_id' => $this->value('storage_aliyun_oss_access_key_id', ''),
                    'access_key_secret' => $this->value('storage_aliyun_oss_access_key_secret', ''),
                    'endpoint' => $this->value('storage_aliyun_oss_endpoint', ''),
                    'bucket' => $this->value('storage_aliyun_oss_bucket', ''),
                ],
                'tencent_cos' => [
                    'secret_id' => $this->value('storage_tencent_cos_secret_id', ''),
                    'secret_key' => $this->value('storage_tencent_cos_secret_key', ''),
                    'region' => $this->value('storage_tencent_cos_region', ''),
                    'bucket' => $this->value('storage_tencent_cos_bucket', ''),
                ],
                'qiniu_kodo' => [
                    'access_key' => $this->value('storage_qiniu_access_key', ''),
                    'secret_key' => $this->value('storage_qiniu_secret_key', ''),
                    'bucket' => $this->value('storage_qiniu_bucket', ''),
                    'domain' => $this->value('storage_qiniu_domain', ''),
                ],
            ],
            'security' => $this->getSecurityConfig(),
        ];
    }

    /**
     * 获取管理员配置
     * @param Request $request
     * @return array
     */
    public function getBasicOptions(Request $request): array
    {
        return [
            'basic' => [
                'platform_name' => $this->value('platform_name', ''),
            'admin_copyright' => $this->value('admin_copyright', ''),
            'logo_rect' => $this->asset($request, 'logo_rect'),
            'logo_square' => $this->asset($request, 'logo_square'),
            'login_bg_image' => $this->asset($request, 'login_bg_image'),
            ],
        ];
    }

    /**
     * 获取公共配置
     * @param Request $request
     * @return array
     */
    public function getPublicConfig(Request $request): array
    {
        return [
            'platform_name' => $this->value('platform_name', ''),
            'logo_square' => $this->asset($request, 'logo_square'),
            'logo_rect' => $this->asset($request, 'logo_rect'),
            'login_bg_image' => $this->asset($request, 'login_bg_image'),
            'maintenance_enabled' => $this->intValue('maintenance_enabled', 0),
            'maintenance_notice' => $this->value('maintenance_notice', ''),
            'register_enabled' => $this->intValue('register_enabled', 1) ? 1 : 0,
            'sms_verify_enabled' => $this->intValue('sms_verify_enabled', 0),
            'service_phone' => $this->value('service_phone', ''),
            'service_wechat' => $this->value('service_wechat', ''),
            'service_time' => $this->value('service_time', ''),
            'service_qrcode' => $this->asset($request, 'service_qrcode'),
            'agreement' => [
                'service_agreement' => $this->value('service_agreement', ''),
                'privacy_agreement' => $this->value('privacy_agreement', ''),
            ],
        ];
    }

    /**
     * 保存配置配置
     * @param array $payload
     * @return array
     */
    public function save(array $payload): array
    {
        $basic = is_array($payload['basic'] ?? null) ? $payload['basic'] : [];
        $agreement = is_array($payload['agreement'] ?? null) ? $payload['agreement'] : [];
        $sms = is_array($payload['sms'] ?? null) ? $payload['sms'] : [];
        $smsTencent = is_array($sms['tencent'] ?? null) ? $sms['tencent'] : [];
        $smsAliyun = is_array($sms['aliyun'] ?? null) ? $sms['aliyun'] : [];
        $email = is_array($payload['email'] ?? null) ? $payload['email'] : [];
        $queue = is_array($payload['queue'] ?? null) ? $payload['queue'] : [];

        $this->configRepository()->setValue('platform_name', (string)($basic['platform_name'] ?? ''), '');
        $this->configRepository()->setValue('admin_copyright', (string)($basic['admin_copyright'] ?? ''), '');
        $this->configRepository()->setValue('service_phone', (string)($basic['service_phone'] ?? ''), '');
        $this->configRepository()->setValue('service_wechat', (string)($basic['service_wechat'] ?? ''), '');
        $this->configRepository()->setValue('service_time', (string)($basic['service_time'] ?? ''), '');
        $this->configRepository()->setValue('service_qrcode', StorageService::normalizeAttachmentValue((string)($basic['service_qrcode'] ?? '')), '');
        $this->configRepository()->setValue('logo_rect', StorageService::normalizeAttachmentValue((string)($basic['logo_rect'] ?? '')), 'ogo');
        $this->configRepository()->setValue('logo_square', StorageService::normalizeAttachmentValue((string)($basic['logo_square'] ?? '')), 'go');
        $this->configRepository()->setValue('login_bg_image', StorageService::normalizeAttachmentValue((string)($basic['login_bg_image'] ?? '')), '');
        $this->configRepository()->setValue('maintenance_enabled', (string)((int)($basic['maintenance_enabled'] ?? 0)), '');
        $this->configRepository()->setValue('maintenance_notice', (string)($basic['maintenance_notice'] ?? ''), '');

        $autoCancelEnabled = (int)($basic['auto_cancel_unpaid_enabled'] ?? 0) ? 1 : 0;
        $this->configRepository()->setValue('auto_cancel_unpaid_enabled', (string)$autoCancelEnabled, '');

        $autoCancelMinutes = $basic['auto_cancel_unpaid_minutes'] ?? null;
        if (!is_numeric($autoCancelMinutes)) {
            $autoCancelMinutes = 15;
        }
        $autoCancelMinutes = (int)$autoCancelMinutes;
        if ($autoCancelMinutes < 1) {
            $autoCancelMinutes = 1;
        }
        if ($autoCancelMinutes > 1440) {
            $autoCancelMinutes = 1440;
        }
        $this->configRepository()->setValue('auto_cancel_unpaid_minutes', (string)$autoCancelMinutes, '');

        $registerEnabled = (int)($basic['register_enabled'] ?? 0) ? 1 : 0;
        $this->configRepository()->setValue('register_enabled', (string)$registerEnabled, '');

        $smsVerifyEnabled = (int)($sms['sms_verify_enabled'] ?? ($basic['sms_verify_enabled'] ?? 0)) ? 1 : 0;
        $smsCodeTtlSeconds = (int)($sms['sms_code_ttl_seconds'] ?? ($basic['sms_code_ttl_seconds'] ?? 300));
        if ($smsCodeTtlSeconds < 60) {
            $smsCodeTtlSeconds = 60;
        }
        if ($smsCodeTtlSeconds > 1800) {
            $smsCodeTtlSeconds = 1800;
        }
        $this->configRepository()->setValue('sms_verify_enabled', (string)$smsVerifyEnabled, '');
        $this->configRepository()->setValue('sms_code_ttl_seconds', (string)$smsCodeTtlSeconds);

        $imageCompressThreshold = $basic['image_compress_threshold'] ?? null;
        if (!is_numeric($imageCompressThreshold)) {
            $imageCompressThreshold = 500;
        }
        $imageCompressThreshold = (int)$imageCompressThreshold;
        if ($imageCompressThreshold < 0) {
            $imageCompressThreshold = 0;
        }
        $this->configRepository()->setValue('image_compress_threshold', (string)$imageCompressThreshold);

        $imageThumbSizes = $basic['image_thumb_sizes'] ?? null;
        if (is_array($imageThumbSizes)) {
            $sizes = [];
            foreach ($imageThumbSizes as $label => $dim) {
                if (is_array($dim) && count($dim) >= 2) {
                    $w = (int)$dim[0];
                    $h = (int)$dim[1];
                    if ($w > 0 && $h > 0 && $w <= 4096 && $h <= 4096) {
                        $sizes[(string)$label] = [$w, $h];
                    }
                }
            }
            $this->configRepository()->setValue('image_thumb_sizes', $sizes ? json_encode($sizes) : '', '');
        } else {
            $this->configRepository()->setValue('image_thumb_sizes', '', '');
        }

        $this->configRepository()->setValue('service_agreement', (string)($agreement['service_agreement'] ?? ''), '');
        $this->configRepository()->setValue('privacy_agreement', (string)($agreement['privacy_agreement'] ?? ''), '');

        $provider = (string)($sms['provider'] ?? '');
        if ($provider !== 'tencent' && $provider !== 'aliyun') {
            $provider = 'tencent';
        }
        $this->configRepository()->setValue('sms_provider', $provider, '');

        $this->configRepository()->setValue('sms_tencent_secret_id', (string)($smsTencent['secret_id'] ?? ''));
        $this->configRepository()->setValue('sms_tencent_secret_key', (string)($smsTencent['secret_key'] ?? ''));
        $this->configRepository()->setValue('sms_tencent_app_id', (string)($smsTencent['app_id'] ?? ''));
        $this->configRepository()->setValue('sms_tencent_sign_name', (string)($smsTencent['sign_name'] ?? ''), '');

        $this->configRepository()->setValue('sms_aliyun_access_key_id', (string)($smsAliyun['access_key_id'] ?? ''));
        $this->configRepository()->setValue('sms_aliyun_access_key_secret', (string)($smsAliyun['access_key_secret'] ?? ''));
        $this->configRepository()->setValue('sms_aliyun_sign_name', (string)($smsAliyun['sign_name'] ?? ''), '');

        $this->configRepository()->setValue('email_smtp_host', (string)($email['smtp_host'] ?? ''), '');
        $this->configRepository()->setValue('email_smtp_port', (string)($email['smtp_port'] ?? '465'), '');
        $this->configRepository()->setValue('email_smtp_user', (string)($email['smtp_user'] ?? ''), '');
        $this->configRepository()->setValue('email_smtp_pass', (string)($email['smtp_pass'] ?? ''), '');
        $this->configRepository()->setValue('email_sender_name', (string)($email['sender_name'] ?? ''), '');

        $queueEnabled = (int)($queue['enabled'] ?? 0) ? 1 : 0;
        $this->configRepository()->setValue('queue_enabled', (string)$queueEnabled, '');

        $queueDriver = (string)($queue['driver'] ?? 'sync');
        if (!in_array($queueDriver, ['sync', 'database', 'redis'], true)) {
            $queueDriver = 'sync';
        }
        $this->configRepository()->setValue('queue_driver', $queueDriver, '');

        $defaultQueue = trim((string)($queue['default_queue'] ?? 'default'));
        if ($defaultQueue === '') {
            $defaultQueue = 'default';
        }
        $this->configRepository()->setValue('queue_default_queue', $defaultQueue, '');

        $retryDelaySeconds = $this->boundedInt($queue['retry_delay_seconds'] ?? 60, 1, 86400, 60);
        $reserveSeconds = $this->boundedInt($queue['reserve_seconds'] ?? 120, 5, 86400, 120);
        $maxAttempts = $this->boundedInt($queue['max_attempts'] ?? 5, 1, 20, 5);
        $workSleepSeconds = $this->boundedInt($queue['work_sleep_seconds'] ?? 3, 1, 60, 3);
        $fallbackToSync = (int)($queue['fallback_to_sync'] ?? 1) ? 1 : 0;

        $this->configRepository()->setValue('queue_retry_delay_seconds', (string)$retryDelaySeconds, '');
        $this->configRepository()->setValue('queue_reserve_seconds', (string)$reserveSeconds, '');
        $this->configRepository()->setValue('queue_max_attempts', (string)$maxAttempts, '');
        $this->configRepository()->setValue('queue_work_sleep_seconds', (string)$workSleepSeconds, '');
        $this->configRepository()->setValue('queue_fallback_to_sync', (string)$fallbackToSync, '');

        $queueRedis = is_array($queue['redis'] ?? null) ? $queue['redis'] : [];
        $queueRedisHost = trim((string)($queueRedis['host'] ?? '127.0.0.1'));
        if ($queueRedisHost === '') {
            $queueRedisHost = '127.0.0.1';
        }
        $queueRedisPort = $this->boundedInt($queueRedis['port'] ?? 6379, 1, 65535, 6379);
        $queueRedisSelect = $this->boundedInt($queueRedis['select'] ?? 0, 0, 15, 0);
        $queueRedisTimeout = $this->boundedInt($queueRedis['timeout'] ?? 0, 0, 30, 0);
        $queueRedisPersistent = (int)($queueRedis['persistent'] ?? 0) ? 1 : 0;
        $queueRedisPrefix = trim((string)($queueRedis['prefix'] ?? 'async_queue:'));
        if ($queueRedisPrefix === '') {
            $queueRedisPrefix = 'async_queue:';
        }

        $this->configRepository()->setValue('queue_redis_host', $queueRedisHost, '');
        $this->configRepository()->setValue('queue_redis_port', (string)$queueRedisPort, '');
        $this->configRepository()->setValue('queue_redis_password', (string)($queueRedis['password'] ?? ''), '');
        $this->configRepository()->setValue('queue_redis_select', (string)$queueRedisSelect, '');
        $this->configRepository()->setValue('queue_redis_timeout', (string)$queueRedisTimeout, '');
        $this->configRepository()->setValue('queue_redis_persistent', (string)$queueRedisPersistent, '');
        $this->configRepository()->setValue('queue_redis_prefix', $queueRedisPrefix, '');

        $storage = is_array($payload['storage'] ?? null) ? $payload['storage'] : [];
        $storageDriver = (string)($storage['driver'] ?? 'local');
        if (!in_array($storageDriver, ['local', 'aliyun_oss', 'tencent_cos', 'qiniu_kodo'], true)) {
            $storageDriver = 'local';
        }
        $this->configRepository()->setValue('storage_driver', $storageDriver, '');

        $oss = is_array($storage['aliyun_oss'] ?? null) ? $storage['aliyun_oss'] : [];
        $this->configRepository()->setValue('storage_aliyun_oss_access_key_id', (string)($oss['access_key_id'] ?? ''), 'S AccessKeyId');
        $this->configRepository()->setValue('storage_aliyun_oss_access_key_secret', (string)($oss['access_key_secret'] ?? ''), 'S AccessKeySecret');
        $this->configRepository()->setValue('storage_aliyun_oss_endpoint', (string)($oss['endpoint'] ?? ''), 'S Endpoint');
        $this->configRepository()->setValue('storage_aliyun_oss_bucket', (string)($oss['bucket'] ?? ''), 'S Bucket');

        $cos = is_array($storage['tencent_cos'] ?? null) ? $storage['tencent_cos'] : [];
        $this->configRepository()->setValue('storage_tencent_cos_secret_id', (string)($cos['secret_id'] ?? ''), 'S SecretId');
        $this->configRepository()->setValue('storage_tencent_cos_secret_key', (string)($cos['secret_key'] ?? ''), 'S SecretKey');
        $this->configRepository()->setValue('storage_tencent_cos_region', (string)($cos['region'] ?? ''), 'S Region');
        $this->configRepository()->setValue('storage_tencent_cos_bucket', (string)($cos['bucket'] ?? ''), 'S Bucket');

        $qiniu = is_array($storage['qiniu_kodo'] ?? null) ? $storage['qiniu_kodo'] : [];
        $this->configRepository()->setValue('storage_qiniu_access_key', (string)($qiniu['access_key'] ?? ''), 'do AccessKey');
        $this->configRepository()->setValue('storage_qiniu_secret_key', (string)($qiniu['secret_key'] ?? ''), 'do SecretKey');
        $this->configRepository()->setValue('storage_qiniu_bucket', (string)($qiniu['bucket'] ?? ''), 'do Bucket');
        $this->configRepository()->setValue('storage_qiniu_domain', (string)($qiniu['domain'] ?? ''), 'do CDN');

        // ----  ----
        $security = is_array($payload['security'] ?? null) ? $payload['security'] : [];

        $rateLimitEnabled = (int)($security['rate_limit_enabled'] ?? 0) ? 1 : 0;
        $this->configRepository()->setValue('security_rate_limit_enabled', (string)$rateLimitEnabled, '');
        $this->configRepository()->setValue('security_rate_limit_requests', (string)$this->boundedInt($security['rate_limit_requests'] ?? 60, 1, 6000, 60), '');
        $this->configRepository()->setValue('security_rate_limit_window', (string)$this->boundedInt($security['rate_limit_window'] ?? 60, 10, 3600, 60), '');

        $apiRateEnabled = (int)($security['api_rate_enabled'] ?? 0) ? 1 : 0;
        $this->configRepository()->setValue('security_api_rate_enabled', (string)$apiRateEnabled, '');
        $this->configRepository()->setValue('security_api_rate_requests', (string)$this->boundedInt($security['api_rate_requests'] ?? 30, 1, 6000, 30), '');
        $this->configRepository()->setValue('security_api_rate_window', (string)$this->boundedInt($security['api_rate_window'] ?? 60, 10, 3600, 60), '');

        $smsDailyEnabled = (int)($security['sms_daily_enabled'] ?? 0) ? 1 : 0;
        $this->configRepository()->setValue('security_sms_daily_enabled', (string)$smsDailyEnabled, '');
        $this->configRepository()->setValue('security_sms_daily_max', (string)$this->boundedInt($security['sms_daily_max'] ?? 10, 1, 1000, 10), '');

        $smsIpRateEnabled = (int)($security['sms_ip_rate_enabled'] ?? 0) ? 1 : 0;
        $this->configRepository()->setValue('security_sms_ip_rate_enabled', (string)$smsIpRateEnabled, '');
        $this->configRepository()->setValue('security_sms_ip_rate_max', (string)$this->boundedInt($security['sms_ip_rate_max'] ?? 5, 1, 100, 5), '');

        $loginFailEnabled = (int)($security['login_fail_enabled'] ?? 0) ? 1 : 0;
        $this->configRepository()->setValue('security_login_fail_enabled', (string)$loginFailEnabled, '');
        $this->configRepository()->setValue('security_login_fail_max', (string)$this->boundedInt($security['login_fail_max'] ?? 5, 1, 20, 5), '');
        $this->configRepository()->setValue('security_login_fail_lock', (string)$this->boundedInt($security['login_fail_lock'] ?? 15, 1, 1440, 15), '');

        return ['code' => 0, 'msg' => ''];
    }

    /**
     * 转换资产URL
     * @param Request $request
     * @param string $key 资产键
     * @return string 资产URL
     */
    private function asset(Request $request, string $key): string
    {
        return StorageService::toFullAssetUrl($request, $this->value($key, ''));
    }

    /**
     * 获取配置项值
     * @param string $key 配置项键
     * @param string $default 默认值
     * @return string 配置项值
     */
    private function value(string $key, string $default = ''): string
    {
        return $this->configRepository()->getString($key, $default);
    }

    /**
     * 获取配置项整数值值
     * @param string $key 配置项键
     * @param int $default 默认值
     * @return int 配置项整数值值
     */
    private function intValue(string $key, int $default = 0): int
    {
        return $this->configRepository()->getInt($key, $default);
    }

    /**
     * 获取配置项JSON值
     * @param string $key 配置项键
     * @param string $default 默认值
     * @return array 配置项JSON值
     */
    private function jsonValue(string $key, string $default = '{}'): array
    {
        $raw = $this->configRepository()->getString($key, '');
        if ($raw === '') {
            $raw = $default;
        }
        $decoded = json_decode($raw, true);
        return is_array($decoded) ? $decoded : (json_decode($default, true) ?: []);
    }

    /**
     * 限制整数值在指定范围内
     * @param mixed $value
     * @param int $min
     * @param int $max
     * @param int $default 默认值
     * @return int
     */
    private function boundedInt($value, int $min, int $max, int $default): int
    {
        if (!is_numeric($value)) {
            $value = $default;
        }

        $value = (int)$value;
        if ($value < $min) {
            $value = $min;
        }
        if ($value > $max) {
            $value = $max;
        }

        return $value;
    }

    /**
     * 获取队列配置服务
     * @return QueueConfigService
     */
    private function queueConfig(): QueueConfigService
    {
        return $this->app->make(QueueConfigService::class);
    }

    /**
     * 获取核心版本号
     * @return string 核心版本号
     */
    private function coreVersion(): string
    {
        $manifestPath = $this->app->getRootPath() . 'core' . DIRECTORY_SEPARATOR . 'manifest' . DIRECTORY_SEPARATOR . 'manifest.json';
        if (is_file($manifestPath)) {
            $manifest = json_decode(file_get_contents($manifestPath), true);
            if (is_array($manifest) && !empty($manifest['version'])) {
                return (string)$manifest['version'];
            }
        }
        return '1.0.0';
    }

    /**
     * 获取安全配置
     * @return array 安全配置
     */
    public function getSecurityConfig(): array
    {
        return [
            'security_enabled' => $this->intValue('security_enabled', 0),
            'api_rate_enabled' => $this->intValue('security_api_rate_enabled', 0),
            'api_rate_requests' => $this->intValue('security_api_rate_requests', 30),
            'api_rate_window' => $this->intValue('security_api_rate_window', 60),
            'sms_daily_enabled' => $this->intValue('security_sms_daily_enabled', 0),
            'sms_daily_max' => $this->intValue('security_sms_daily_max', 10),
            'sms_ip_rate_enabled' => $this->intValue('security_sms_ip_rate_enabled', 0),
            'sms_ip_rate_max' => $this->intValue('security_sms_ip_rate_max', 5),
            'login_fail_enabled' => $this->intValue('security_login_fail_enabled', 0),
            'login_fail_max' => $this->intValue('security_login_fail_max', 5),
            'login_fail_lock' => $this->intValue('security_login_fail_lock', 15),
        ];
    }

    /**
     * 获取配置仓库
     * @return ConfigRepository
     */
    private function configRepository(): ConfigRepository
    {
        return $this->app->make(ConfigRepository::class);
    }
}