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

4 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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);
  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);
  100. var devConfig = input.Adapt<SysConfig>();//实体转换
  101. if (await UpdateAsync(devConfig))//更新数据
  102. await RefreshCache(CateGoryConst.CONFIG_BIZ_DEFINE);//刷新缓存
  103. }
  104. /// <inheritdoc/>
  105. public async Task EditBatch(List<SysConfig> devConfigs)
  106. {
  107. if (devConfigs.Count > 0)
  108. {
  109. //根据分类分组
  110. var configGroups = devConfigs.GroupBy(it => it.Category).ToList();
  111. //遍历分组
  112. foreach (var item in configGroups)
  113. {
  114. var configList = await GetConfigsByCategory(item.Key);//获取系统配置
  115. var configs = item.ToList();//获取分组完的配置列表
  116. var keys = configs.Select(it => it.ConfigKey).ToList();//获取key列表
  117. configList = configList.Where(it => keys.Contains(it.ConfigKey)).ToList();//获取要修改的列表
  118. //遍历配置列表
  119. configList.ForEach(it =>
  120. {
  121. //赋值ConfigValue
  122. it.ConfigValue = configs.Where(c => c.ConfigKey == it.ConfigKey).First().ConfigValue;
  123. });
  124. //更新数据
  125. if (await UpdateRangeAsync(configList))
  126. await RefreshCache(item.Key);//刷新缓存
  127. }
  128. }
  129. }
  130. #endregion
  131. #region 删除
  132. /// <inheritdoc/>
  133. public async Task Delete(ConfigDeleteInput input)
  134. {
  135. //获取所有业务配置
  136. var configs = await GetConfigsByCategory(CateGoryConst.CONFIG_BIZ_DEFINE);
  137. if (configs.Any(it => it.Id == input.Id))//如果有当前配置
  138. {
  139. if (await DeleteByIdAsync(input.Id))//删除配置
  140. await RefreshCache(CateGoryConst.CONFIG_BIZ_DEFINE);//刷新缓存
  141. }
  142. }
  143. #endregion
  144. #region 方法
  145. /// <summary>
  146. /// 刷新缓存
  147. /// </summary>
  148. /// <param name="category">分类</param>
  149. /// <returns></returns>
  150. private async Task RefreshCache(string category)
  151. {
  152. _simpleCacheService.Remove(SystemConst.CACHE_DEV_CONFIG + category);//redis删除
  153. await GetConfigsByCategory(category);//重新获取
  154. }
  155. /// <summary>
  156. /// 检查输入参数
  157. /// </summary>
  158. /// <param name="sysConfig"></param>
  159. private async Task CheckInput(SysConfig sysConfig)
  160. {
  161. var configs = await GetConfigsByCategory(CateGoryConst.CONFIG_BIZ_DEFINE);//获取全部字典
  162. //判断是否从存在重复字典名
  163. var hasSameKey = configs.Any(it => it.ConfigKey == sysConfig.ConfigKey && it.Id != sysConfig.Id);
  164. if (hasSameKey)
  165. {
  166. throw Oops.Bah($"存在重复的配置键:{sysConfig.ConfigKey}");
  167. }
  168. //设置分类为业务
  169. sysConfig.Category = CateGoryConst.CONFIG_BIZ_DEFINE;
  170. }
  171. #endregion 方法
  172. }