using Admin.NET.Core.Service;
using Furion.DatabaseAccessor;
using System.Text.RegularExpressions;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.TCBGetPressureTestReportResponse.Types;

namespace WCS.Application;
/// <summary>
/// 系统参数配置服务 🧩
/// </summary>
[ApiDescriptionSettings(Order = 440)]
public class SysConfigService : IDynamicApiController, ITransient
{
    private readonly SysCacheService _sysCacheService;
    private readonly SqlSugarRepository<SysConfig> _sysConfigRep;

    public SysConfigService(SysCacheService sysCacheService,
        SqlSugarRepository<SysConfig> sysConfigRep)
    {
        _sysCacheService = sysCacheService;
        _sysConfigRep = sysConfigRep;
    }

    /// <summary>
    /// 获取上下游系统信息 🔖
    /// </summary>
    /// <returns></returns>
    [SuppressMonitor]
    //[AllowAnonymous]
    [DisplayName("获取上下游系统信息")]
    public async Task<dynamic> GetSystemInfo()
    {
        var serviceAddress = await GetConfigValue<string>(SystemConfigConst.ServiceAddress);
        var servicePort = await GetConfigValue<string>(SystemConfigConst.ServicePort);
        var wmsAddress = await GetConfigValue<string>(SystemConfigConst.WMSAddress);
        var wmsPort = await GetConfigValue<string>(SystemConfigConst.WMSPort);
        var codingAddress = await GetConfigValue<string>(SystemConfigConst.CodingAddress);
        var codingPort = await GetConfigValue<string>(SystemConfigConst.CodingPort);
        var rcsAddress = await GetConfigValue<string>(SystemConfigConst.RCSAddress);
        var rcsPort = await GetConfigValue<string>(SystemConfigConst.RCSPort);

        return new
        {
            ServiceAddress = serviceAddress,
            ServicePort = servicePort,
            WMSAddress = wmsAddress,
            WMSPort = wmsPort,
            CodingAddress = codingAddress,
            CodingPort = codingPort,
            RCSAddress = rcsAddress,
            RCSPort = rcsPort,
        };
    }
    /// <summary>
    /// 保存上下游系统信息 🔖
    /// </summary>
    /// <returns></returns>
    [UnitOfWork]
    [DisplayName("保存上下游系统信息")]
    public async Task SaveSystemInfo(SystemSaveInput input)
    {
        await UpdateConfigValue(SystemConfigConst.ServiceAddress, input.ServiceAddress);
        await UpdateConfigValue(SystemConfigConst.ServicePort, input.ServicePort);
        await UpdateConfigValue(SystemConfigConst.WMSAddress, input.WMSAddress);
        await UpdateConfigValue(SystemConfigConst.WMSPort, input.WMSPort);
        await UpdateConfigValue(SystemConfigConst.CodingAddress, input.CodingAddress);
        await UpdateConfigValue(SystemConfigConst.CodingPort, input.CodingPort);
        await UpdateConfigValue(SystemConfigConst.RCSAddress, input.RCSAddress);
        await UpdateConfigValue(SystemConfigConst.RCSPort, input.RCSPort);
    }

    /// <summary>
    /// 获取参数配置值
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    [NonAction]
    public async Task<T> GetConfigValue<T>(string code)
    {
        if (string.IsNullOrWhiteSpace(code)) return default;

        var value = _sysCacheService.Get<string>($"{CacheConst.KeyConfig}{code}");
        if (string.IsNullOrEmpty(value))
        {
            var config = await _sysConfigRep.CopyNew().GetFirstAsync(u => u.Code == code);
            value = config != null ? config.Value : default;
            _sysCacheService.Set($"{CacheConst.KeyConfig}{code}", value);
        }
        if (string.IsNullOrWhiteSpace(value)) return default;
        return (T)Convert.ChangeType(value, typeof(T));
    }

    /// <summary>
    /// 更新参数配置值
    /// </summary>
    /// <param name="code"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    [NonAction]
    public async Task UpdateConfigValue(string code, string value)
    {
        var config = await _sysConfigRep.GetFirstAsync(u => u.Code == code);
        if (config == null) return;

        config.Value = value;
        await _sysConfigRep.AsUpdateable(config).ExecuteCommandAsync();

        _sysCacheService.Remove($"{CacheConst.KeyConfig}{config.Code}");
    }
    /// <summary>
    /// 更新参数配置值
    /// </summary>
    /// <param name="code"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    [NonAction]
    public async Task UpdateConfigValue(string code, bool value)
    {
        var config = await _sysConfigRep.GetFirstAsync(u => u.Code == code);
        if (config == null) return;

        config.Value = value?"True":"False";
        await _sysConfigRep.AsUpdateable(config).ExecuteCommandAsync();

        _sysCacheService.Remove($"{CacheConst.KeyConfig}{config.Code}");
    }
}