平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

209 regels
7.0 KiB

  1. //
  2. namespace SafeCampus.System;
  3. /// <inheritdoc cref="IConfigService"/>
  4. public class ConfigService : DbRepository<SysConfig>, IConfigService
  5. {
  6. private readonly ISimpleCacheService _simpleCacheService;
  7. public ConfigService(ISimpleCacheService simpleCacheService)
  8. {
  9. _simpleCacheService = simpleCacheService;
  10. }
  11. #region 查询
  12. /// <inheritdoc/>
  13. public async Task<List<SysConfig>> GetConfigsByCategory(string category)
  14. {
  15. var key = SystemConst.CACHE_DEV_CONFIG + category;//系统配置key
  16. //先从redis拿配置
  17. var configList = _simpleCacheService.HashGetAll<SysConfig>(key).Values.ToList();
  18. if (configList.Count == 0)
  19. {
  20. //redis没有再去数据可拿
  21. var configs = await GetListAsync(it => it.Category == category);//获取系统配置列表
  22. if (configs.Count > 0)
  23. {
  24. configList = configs;
  25. _simpleCacheService.HashSet(key, configList.ToDictionary(it => it.ConfigKey));//如果不为空,插入redis
  26. }
  27. }
  28. return configList.ToList();
  29. }
  30. /// <inheritdoc/>
  31. public async Task<List<SysConfig>> GetSysConfigList()
  32. {
  33. //这里不返回系统配置
  34. return await GetListAsync(it => it.Category != CateGoryConst.CONFIG_BIZ_DEFINE);
  35. }
  36. /// <inheritdoc/>
  37. public async Task<SysConfig> GetByConfigKey(string category, string configKey)
  38. {
  39. var key = SystemConst.CACHE_DEV_CONFIG + category;//系统配置key
  40. //先从redis拿配置
  41. var config = _simpleCacheService.HashGetOne<SysConfig>(key, configKey);
  42. if (config == null)
  43. {
  44. //redis没有再去数据可拿
  45. config = await GetFirstAsync(it => it.Category == category && it.ConfigKey == configKey);//获取系统配置
  46. if (config != null)
  47. {
  48. await GetConfigsByCategory(category);//获取全部字典,并插入redis
  49. }
  50. }
  51. return config;
  52. }
  53. /// <inheritdoc/>
  54. public async Task<SqlSugarPagedList<SysConfig>> Page(ConfigPageInput input)
  55. {
  56. var query = Context.Queryable<SysConfig>().Where(it => it.Category == CateGoryConst.CONFIG_BIZ_DEFINE)//自定义配置
  57. .WhereIF(!string.IsNullOrEmpty(input.SearchKey),
  58. it => it.ConfigKey.Contains(input.SearchKey) || it.ConfigValue.Contains(input.SearchKey))//根据关键字查询
  59. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}")//排序
  60. .OrderBy(it => it.SortCode)
  61. .OrderBy(it => it.CreateTime);//排序
  62. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  63. return pageInfo;
  64. }
  65. /// <inheritdoc/>
  66. public async Task<FileStreamResult> GetIco()
  67. {
  68. var config = await GetByConfigKey(CateGoryConst.CONFIG_SYS_BASE, SysConfigConst.SYS_ICO);//获取系统配置 ico
  69. var icoBase64 = config.ConfigValue.Split(",")[1];//获取ico的base64
  70. //转为byte数组
  71. var bytes = Convert.FromBase64String(icoBase64);
  72. //将byte数组转为流
  73. var stream = new MemoryStream(bytes);
  74. return new FileStreamResult(stream, "image/x-icon");
  75. }
  76. /// <inheritdoc/>
  77. public async Task<bool> IsTenant()
  78. {
  79. var tenantOption = await GetByConfigKey(CateGoryConst.CONFIG_SYS_BASE, SysConfigConst.SYS_TENANT_OPTIONS);
  80. var isTenant = tenantOption.ConfigValue != SysDictConst.TENANT_OPTIONS_CLOSE;//是否开启多租户
  81. return isTenant;
  82. }
  83. #endregion
  84. #region 添加
  85. /// <inheritdoc/>
  86. public async Task Add(ConfigAddInput input)
  87. {
  88. await CheckInput(input,true);
  89. var devConfig = input.Adapt<SysConfig>();//实体转换
  90. if (await InsertAsync(devConfig))//插入数据)
  91. await RefreshCache(CateGoryConst.CONFIG_BIZ_DEFINE);//刷新缓存
  92. }
  93. #endregion
  94. #region 编辑
  95. /// <inheritdoc/>
  96. public async Task Edit(ConfigEditInput input)
  97. {
  98. await CheckInput(input,false);
  99. //var devConfig = input.Adapt<SysConfig>();//实体转换
  100. var model = await GetByConfigKey(input.Category, input.ConfigKey);
  101. model.ConfigValue=input.ConfigValue;
  102. if (await UpdateAsync(model))//更新数据
  103. await RefreshCache(CateGoryConst.CONFIG_BIZ_DEFINE);//刷新缓存
  104. }
  105. /// <inheritdoc/>
  106. public async Task EditBatch(List<SysConfig> devConfigs)
  107. {
  108. if (devConfigs.Count > 0)
  109. {
  110. //根据分类分组
  111. var configGroups = devConfigs.GroupBy(it => it.Category).ToList();
  112. //遍历分组
  113. foreach (var item in configGroups)
  114. {
  115. var configList = await GetConfigsByCategory(item.Key);//获取系统配置
  116. var configs = item.ToList();//获取分组完的配置列表
  117. var keys = configs.Select(it => it.ConfigKey).ToList();//获取key列表
  118. configList = configList.Where(it => keys.Contains(it.ConfigKey)).ToList();//获取要修改的列表
  119. //遍历配置列表
  120. configList.ForEach(it =>
  121. {
  122. //赋值ConfigValue
  123. it.ConfigValue = configs.Where(c => c.ConfigKey == it.ConfigKey).First().ConfigValue;
  124. });
  125. //更新数据
  126. if (await UpdateRangeAsync(configList))
  127. await RefreshCache(item.Key);//刷新缓存
  128. }
  129. }
  130. }
  131. #endregion
  132. #region 删除
  133. /// <inheritdoc/>
  134. public async Task Delete(ConfigDeleteInput input)
  135. {
  136. //获取所有业务配置
  137. var configs = await GetConfigsByCategory(CateGoryConst.CONFIG_BIZ_DEFINE);
  138. if (configs.Any(it => it.Id == input.Id))//如果有当前配置
  139. {
  140. if (await DeleteByIdAsync(input.Id))//删除配置
  141. await RefreshCache(CateGoryConst.CONFIG_BIZ_DEFINE);//刷新缓存
  142. }
  143. }
  144. #endregion
  145. #region 方法
  146. /// <summary>
  147. /// 刷新缓存
  148. /// </summary>
  149. /// <param name="category">分类</param>
  150. /// <returns></returns>
  151. private async Task RefreshCache(string category)
  152. {
  153. _simpleCacheService.Remove(SystemConst.CACHE_DEV_CONFIG + category);//redis删除
  154. await GetConfigsByCategory(category);//重新获取
  155. }
  156. /// <summary>
  157. /// 检查输入参数
  158. /// </summary>
  159. /// <param name="sysConfig"></param>
  160. private async Task CheckInput(SysConfig sysConfig,bool isAdd)
  161. {
  162. var configs = await GetConfigsByCategory(CateGoryConst.CONFIG_BIZ_DEFINE);//获取全部字典
  163. //判断是否从存在重复字典名
  164. var hasSameKey = configs.Any(it => it.ConfigKey == sysConfig.ConfigKey && it.Id != sysConfig.Id);
  165. if (hasSameKey== isAdd)
  166. {
  167. throw Oops.Bah($"存在重复的配置键:{sysConfig.ConfigKey}");
  168. }
  169. //设置分类为业务
  170. sysConfig.Category = CateGoryConst.CONFIG_BIZ_DEFINE;
  171. }
  172. #endregion 方法
  173. }