`
hwh
2024-08-28 6cae8c43ff858d1814ada3d8d691029449293f83
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 
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}");
    }
}