using Admin.NET.Core.Service;
using Furion.DatabaseAccessor;
using System.Text.RegularExpressions;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.TCBGetPressureTestReportResponse.Types;
namespace WCS.Application;
///
/// 系统参数配置服务 🧩
///
[ApiDescriptionSettings(Order = 440)]
public class SysConfigService : IDynamicApiController, ITransient
{
private readonly SysCacheService _sysCacheService;
private readonly SqlSugarRepository _sysConfigRep;
public SysConfigService(SysCacheService sysCacheService,
SqlSugarRepository sysConfigRep)
{
_sysCacheService = sysCacheService;
_sysConfigRep = sysConfigRep;
}
///
/// 获取上下游系统信息 🔖
///
///
[SuppressMonitor]
//[AllowAnonymous]
[DisplayName("获取上下游系统信息")]
public async Task GetSystemInfo()
{
var serviceAddress = await GetConfigValue(SystemConfigConst.ServiceAddress);
var servicePort = await GetConfigValue(SystemConfigConst.ServicePort);
var wmsAddress = await GetConfigValue(SystemConfigConst.WMSAddress);
var wmsPort = await GetConfigValue(SystemConfigConst.WMSPort);
var codingAddress = await GetConfigValue(SystemConfigConst.CodingAddress);
var codingPort = await GetConfigValue(SystemConfigConst.CodingPort);
var rcsAddress = await GetConfigValue(SystemConfigConst.RCSAddress);
var rcsPort = await GetConfigValue(SystemConfigConst.RCSPort);
var agvAddress = await GetConfigValue(SystemConfigConst.AGVAddress);
var agvPort = await GetConfigValue(SystemConfigConst.AGVPort);
return new
{
ServiceAddress = serviceAddress,
ServicePort = servicePort,
WMSAddress = wmsAddress,
WMSPort = wmsPort,
CodingAddress = codingAddress,
CodingPort = codingPort,
RCSAddress = rcsAddress,
RCSPort = rcsPort,
AGVAddress = agvAddress,
AGVPort = agvPort
};
}
///
/// 保存上下游系统信息 🔖
///
///
[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);
await UpdateConfigValue(SystemConfigConst.AGVAddress, input.AGVAddress);
await UpdateConfigValue(SystemConfigConst.AGVPort, input.AGVPort);
}
///
/// 获取参数配置值
///
///
///
[NonAction]
public async Task GetConfigValue(string code)
{
if (string.IsNullOrWhiteSpace(code)) return default;
var value = _sysCacheService.Get($"{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));
}
///
/// 更新参数配置值
///
///
///
///
[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}");
}
///
/// 更新参数配置值
///
///
///
///
[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}");
}
}