平安校园
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.

ConfigService.cs 7.0 KiB

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