平安校园
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

237 Zeilen
8.1 KiB

  1. //
  2. namespace SafeCampus.System;
  3. /// <summary>
  4. /// <inheritdoc cref="IDictService"/>
  5. /// </summary>
  6. public class DictService : DbRepository<SysDict>, IDictService
  7. {
  8. private readonly ISimpleCacheService _simpleCacheService;
  9. public DictService(ISimpleCacheService simpleCacheService)
  10. {
  11. _simpleCacheService = simpleCacheService;
  12. }
  13. /// <inheritdoc />
  14. public async Task<SqlSugarPagedList<SysDict>> Page(DictPageInput input)
  15. {
  16. var query = Context.Queryable<SysDict>().Where(it => it.Category == input.Category)//根据分类查询
  17. .Where(it => it.ParentId == input.ParentId)//根据父ID查询
  18. .WhereIF(!string.IsNullOrEmpty(input.SearchKey),
  19. it => it.DictLabel.Contains(input.SearchKey) || it.DictValue.Contains(input.SearchKey))//根据关键字查询
  20. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}")
  21. .OrderBy(it => it.SortCode)//排序
  22. .OrderBy(it => it.CreateTime);//排序
  23. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  24. return pageInfo;
  25. }
  26. /// <inheritdoc />
  27. public async Task Add(DictAddInput input)
  28. {
  29. await CheckInput(input);//检查参数
  30. var devDict = input.Adapt<SysDict>();//实体转换
  31. if (await InsertAsync(devDict))//插入数据
  32. await RefreshCache();//刷新缓存
  33. }
  34. /// <inheritdoc />
  35. public async Task Edit(DictAddInput input)
  36. {
  37. await CheckInput(input);//检查参数
  38. var devDict = input.Adapt<SysDict>();//实体转换
  39. if (await UpdateAsync(devDict))//更新数据
  40. await RefreshCache();//刷新缓存
  41. }
  42. /// <inheritdoc />
  43. public async Task Delete(DictDeleteInput input)
  44. {
  45. //获取所有ID
  46. var ids = input.Ids;
  47. if (ids.Count > 0)
  48. {
  49. //获取所有字典
  50. var dictList = await GetListAsync();
  51. //判断是否有系统字典
  52. var frm = dictList.Any(it => ids.Contains(it.Id) && it.Category == CateGoryConst.DICT_FRM);
  53. //如果是系统字典提示不可删除
  54. if (frm) throw Oops.Bah("不可删除系统内置字典");
  55. var deleteIds = new List<long>();//要删除的id列表
  56. deleteIds.AddRange(ids);//
  57. ids.ForEach(it =>
  58. {
  59. //获取子节点
  60. var children = GetDevDictChildren(dictList, it);
  61. //提取ID
  62. var childrenIds = children.Select(c => c.Id).ToList();
  63. deleteIds.AddRange(childrenIds);
  64. });
  65. //删除数据
  66. if (await DeleteByIdsAsync(deleteIds.Cast<object>().ToArray()))
  67. await RefreshCache();//刷新缓存
  68. }
  69. }
  70. /// <summary>
  71. /// 获取全部
  72. /// </summary>
  73. /// <returns></returns>
  74. public override async Task<List<SysDict>> GetListAsync()
  75. {
  76. //先从redis拿
  77. var sysDictList = _simpleCacheService.Get<List<SysDict>>(SystemConst.CACHE_DEV_DICT);
  78. if (sysDictList == null)
  79. {
  80. sysDictList = await base.GetListAsync();//去数据库拿
  81. if (sysDictList.Count > 0)
  82. {
  83. _simpleCacheService.Set(SystemConst.CACHE_DEV_DICT, sysDictList);//如果数据库有数,更新redis
  84. return sysDictList;
  85. }
  86. }
  87. return sysDictList;
  88. }
  89. /// <inheritdoc />
  90. public async Task<SysDict> GetDict(string dictValue)
  91. {
  92. var sysDictList = await GetListAsync();
  93. var devDict = sysDictList.Where(it => it.DictValue == dictValue).FirstOrDefault();
  94. return devDict;
  95. }
  96. /// <inheritdoc />
  97. public async Task<List<SysDict>> GetChildrenByDictValue(string dictValue)
  98. {
  99. var sysDictList = await GetListAsync();
  100. var devDict = sysDictList.Where(it => it.DictValue == dictValue).FirstOrDefault();
  101. if (devDict != null)
  102. {
  103. var children = GetDevDictChildren(sysDictList, devDict.Id);
  104. return children;
  105. }
  106. return new List<SysDict>();
  107. }
  108. /// <inheritdoc />
  109. public async Task<List<string>> GetValuesByDictValue(string dictValue, List<SysDict> devDictList = null)
  110. {
  111. var sysDictList = devDictList == null ? await GetListAsync() : devDictList;//获取全部
  112. var id = sysDictList.Where(it => it.DictValue == dictValue).Select(it => it.Id).FirstOrDefault();//根据value找到父节点
  113. if (id > 0)
  114. return sysDictList.Where(it => it.ParentId == id).Select(it => it.DictValue).ToList();//拿到字典值
  115. return new List<string>();
  116. }
  117. /// <inheritdoc />
  118. public async Task<Dictionary<string, List<string>>> GetValuesByDictValue(string[] dictValues)
  119. {
  120. var result = new Dictionary<string, List<string>>();
  121. var sysDictList = await GetListAsync();//获取全部
  122. foreach (var dictValue in dictValues)
  123. {
  124. var data = await GetValuesByDictValue(dictValue, sysDictList);
  125. result.Add(dictValue, data);
  126. }
  127. return result;
  128. }
  129. /// <inheritdoc />
  130. public async Task<List<SysDict>> Tree(DictTreeInput input)
  131. {
  132. var sysDictList = await GetListAsync();//获取字典列表
  133. var devList = sysDictList.WhereIF(!string.IsNullOrEmpty(input.Category), it => it.Category == input.Category).OrderBy(it => it.SortCode)
  134. .ToList();
  135. return ConstructResourceTrees(devList);
  136. }
  137. /// <inheritdoc />
  138. public List<SysDict> ConstructResourceTrees(List<SysDict> dictList, long parentId = 0)
  139. {
  140. //找下级字典ID列表
  141. var resources = dictList.Where(it => it.ParentId == parentId).OrderBy(it => it.SortCode).ToList();
  142. if (resources.Count > 0)//如果数量大于0
  143. {
  144. var data = new List<SysDict>();
  145. foreach (var item in resources)//遍历字典
  146. {
  147. item.Children = ConstructResourceTrees(dictList, item.Id);//添加子节点
  148. data.Add(item);//添加到列表
  149. }
  150. return data;//返回结果
  151. }
  152. return new List<SysDict>();
  153. }
  154. #region 方法
  155. /// <summary>
  156. /// 刷新缓存
  157. /// </summary>
  158. private async Task RefreshCache()
  159. {
  160. _simpleCacheService.Remove(SystemConst.CACHE_DEV_DICT);
  161. await GetListAsync();
  162. }
  163. /// <summary>
  164. /// 检查输入参数
  165. /// </summary>
  166. /// <param name="sysDict"></param>
  167. private async Task CheckInput(SysDict sysDict)
  168. {
  169. var dictList = await GetListAsync();//获取全部字典
  170. //判断是否从存在重复字典名
  171. var hasSameLabel = dictList.Any(it =>
  172. it.ParentId == sysDict.ParentId && it.Category == sysDict.Category && it.DictLabel == sysDict.DictLabel && it.Id != sysDict.Id);
  173. if (hasSameLabel)
  174. {
  175. throw Oops.Bah($"存在重复的字典名称:{sysDict.DictLabel}");
  176. }
  177. //判断是否存在重复字典值
  178. var hasSameValue = dictList.Any(it => it.ParentId == sysDict.ParentId && it.DictValue == sysDict.DictValue && it.Id != sysDict.Id);
  179. if (hasSameValue)
  180. {
  181. throw Oops.Bah($"存在重复的字典值:{sysDict.DictValue}");
  182. }
  183. }
  184. /// <summary>
  185. /// 获取字典所有下级
  186. /// </summary>
  187. /// <param name="dictList">字典列表</param>
  188. /// <param name="parentId">父ID</param>
  189. /// <returns></returns>
  190. public List<SysDict> GetDevDictChildren(List<SysDict> dictList, long parentId)
  191. {
  192. //找下级ID列表
  193. var dicts = dictList.Where(it => it.ParentId == parentId).ToList();
  194. if (dicts.Count > 0)//如果数量大于0
  195. {
  196. var data = new List<SysDict>();
  197. foreach (var item in dicts)//遍历机构
  198. {
  199. var devDictChildren = GetDevDictChildren(dictList, item.Id);
  200. data.AddRange(devDictChildren);//添加子节点);
  201. data.Add(item);////添加到列表
  202. }
  203. return data;//返回结果
  204. }
  205. return new List<SysDict>();
  206. }
  207. #endregion 方法
  208. }