using System.Collections.Concurrent; using System.ComponentModel; using Autofac; using Common.Enum; using Entity.Base; using Entity.DbModel.System; using Entity.Dto.Req; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Redis; using Mapster; using Microsoft.AspNetCore.Mvc; using Repository.System; using SqlSugar; namespace Service.System { [Scope("SingleInstance")] public class SysConfigService : BaseServices { private static readonly ConcurrentDictionary Dictionary = new ConcurrentDictionary(); private readonly SysConfigRepository _sysConfigRep; public SysConfigService(SysConfigRepository sysConfigRep) { _sysConfigRep = sysConfigRep; } RedisHelper redisHelper = AppInfo.Container.Resolve(); /// /// 获取参数配置分页列表 🔖 /// /// /// public async Task> Page(PageConfigReq input) { RefAsync total = 0; var items = await _sysConfigRep.QueryPageAsync( !string.IsNullOrEmpty(input.Name), u => u.Name.Contains(input.Name), !string.IsNullOrEmpty(input.Code), u => u.Code.Contains(input.Code), !string.IsNullOrEmpty(input.GroupCode), u => u.GroupCode.Equals(input.GroupCode), input.Page, input.PageSize, total, input); return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize); } /// /// 获取参数配置列表 🔖 /// /// [DisplayName("获取参数配置列表")] public async Task> GetList() { return await _sysConfigRep.QueryAsync(); } /// /// 获取分组列表 🔖 /// /// public async Task> GetGroupList() { return await _sysConfigRep.QueryByGroupByAsync(u => u.GroupCode, u => u.GroupCode); } /// /// 增加参数配置 🔖 /// /// /// //[ApiDescriptionSettings(Name = "Add"), HttpPost] public async Task AddConfig(AddConfigReq input) { string result = ""; var isExist = await _sysConfigRep.QueryByClauseAsync(u => u.Name == input.Name || u.Code == input.Code); if (isExist != null) result = "已存在同名或同编码参数配置"; int insertSesult = await _sysConfigRep.InsertAsync(input.Adapt()); if (insertSesult > 0) result = "增加参数配置成功"; return result; } /// /// 更新参数配置 🔖 /// /// /// // [ApiDescriptionSettings(Name = "Update"), HttpPost] [DisplayName("更新参数配置")] public async Task UpdateConfig(UpdateConfigReq input) { string result = ""; var isExist = await _sysConfigRep.QueryByClauseAsync(u => (u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id); if (isExist != null) result = "已存在同名或同编码参数配置"; var config = input.Adapt(); int updateResult = await _sysConfigRep.UpdateAsync(config, true); if (updateResult > 0) result = "更新参数配置成功"; redisHelper.Remove(config.Code); return result; } /// /// 删除参数配置 🔖 /// /// /// //[ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除参数配置")] public async Task DeleteConfig(DeleteConfigReq input) { string result = ""; var config = await _sysConfigRep.QueryByClauseAsync(u => u.Id == input.Id); if (config.SysFlag == YesNoEnum.Y) // 禁止删除系统参数 result = "禁止删除系统参数"; bool deleteResult = await _sysConfigRep.DeleteAsync(config); if (deleteResult) result = "删除参数配置成功"; redisHelper.Remove(config.Code); return result; } /// /// 批量删除参数配置 🔖 /// /// /// // [ApiDescriptionSettings(Name = "BatchDelete"), HttpPost] [DisplayName("批量删除参数配置")] public async Task BatchDeleteConfig(List ids) { foreach (var id in ids) { var config = await _sysConfigRep.QueryByClauseAsync(u => u.Id == id); if (config.SysFlag == YesNoEnum.Y) // 禁止删除系统参数 continue; await _sysConfigRep.DeleteAsync(config); redisHelper.Remove(config.Code); } } /// /// 获取参数配置详情 🔖 /// /// /// [DisplayName("获取参数配置详情")] public async Task GetDetail([FromQuery] ConfigReq input) { return await _sysConfigRep.QueryByClauseAsync(u => u.Id == input.Id); } /// /// /// /// GroupCode.code /// public string? Get(string key) { string[] keys = key.Split('.'); if (keys.Length != 2) { throw new ArgumentException("配置数据key格式错误"); } string? strValue = redisHelper.GetStrValue(key); if (strValue != null) { return strValue; } if (Dictionary.TryGetValue(key, out string? value)) { return value; } SysConfig sysConfig = _sysConfigRep.QueryByClause(i => i.GroupCode == keys[0] && i.Code == keys[1]); if (sysConfig == null) { return null; } return sysConfig.Value; } /// /// /// /// GroupCode.code /// public void Set(string key, object value) { string[] keys = key.Split('.'); if (keys.Length != 2) { throw new ArgumentException("配置数据key格式错误"); } string newValue = Convert.ToString(value); Dictionary.AddOrUpdate(key, newValue, (s, s1) => newValue); redisHelper.SetKeyValueStr(key, newValue); SysConfig sysConfig = _sysConfigRep.QueryByClause(i => i.GroupCode == keys[0] && i.Code == keys[1]); if (sysConfig == null) { _sysConfigRep.Insert(new SysConfig() { GroupCode = keys[0], Code = keys[1], Value = newValue, SysFlag = YesNoEnum.N, Name = key }); } else { sysConfig.Value = newValue; _sysConfigRep.Update(sysConfig); } } } }