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

DictService.cs 8.1 KiB

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