平安校园
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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