平安校园
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SysPositionService.cs 12 KiB

il y a 4 mois
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. 
  2. //
  3. namespace SafeCampus.System;
  4. /// <summary>
  5. /// <inheritdoc cref="ISysPositionService"/>
  6. /// </summary>
  7. public class SysPositionService : DbRepository<SysPosition>, ISysPositionService
  8. {
  9. private readonly ISimpleCacheService _simpleCacheService;
  10. private readonly ISysOrgService _sysOrgService;
  11. private readonly IDictService _dictService;
  12. public SysPositionService(ISimpleCacheService simpleCacheService, ISysOrgService sysOrgService, IDictService dictService)
  13. {
  14. _simpleCacheService = simpleCacheService;
  15. _sysOrgService = sysOrgService;
  16. _dictService = dictService;
  17. }
  18. #region 查询
  19. /// <summary>
  20. /// 获取全部
  21. /// </summary>
  22. /// <returns></returns>
  23. public override async Task<List<SysPosition>> GetListAsync()
  24. {
  25. //先从Redis拿
  26. var sysPositions = _simpleCacheService.Get<List<SysPosition>>(SystemConst.CACHE_SYS_POSITION);
  27. if (sysPositions == null)
  28. {
  29. //redis没有就去数据库拿
  30. sysPositions = await base.GetListAsync();
  31. if (sysPositions.Count > 0)
  32. {
  33. //插入Redis
  34. _simpleCacheService.Set(SystemConst.CACHE_SYS_POSITION, sysPositions);
  35. }
  36. }
  37. return sysPositions;
  38. }
  39. /// <inheritdoc />
  40. public async Task<List<SysPosition>> GetPositionListByIdList(IdListInput input)
  41. {
  42. var positions = await GetListAsync();
  43. var positionList = positions.Where(it => input.IdList.Contains(it.Id)).ToList();// 获取指定ID的岗位列表
  44. return positionList;
  45. }
  46. /// <inheritdoc/>
  47. public async Task<List<PositionSelectorOutput>> Selector(PositionSelectorInput input)
  48. {
  49. var sysOrgList = await _sysOrgService.GetListAsync(false);//获取所有组织
  50. var sysPositions = await GetListAsync();//获取所有职位
  51. if (input.OrgIds != null)//根据数据范围查
  52. {
  53. sysOrgList = sysOrgList.Where(it => input.OrgIds.Contains(it.Id)).ToList();//在指定组织列表查询
  54. sysPositions = sysPositions.Where(it => input.OrgIds.Contains(it.OrgId)).ToList();//在指定职位列表查询
  55. }
  56. var result = await ConstructPositionSelector(sysOrgList, sysPositions);//构造树
  57. return result;
  58. }
  59. /// <inheritdoc />
  60. public async Task<SysPosition> GetSysPositionById(long id)
  61. {
  62. var sysPositions = await GetListAsync();
  63. var result = sysPositions.Where(it => it.Id == id).FirstOrDefault();
  64. return result;
  65. }
  66. /// <inheritdoc/>
  67. public async Task<SqlSugarPagedList<SysPosition>> Page(PositionPageInput input)
  68. {
  69. var orgIds = await _sysOrgService.GetOrgChildIds(input.OrgId);//获取下级组织
  70. var query = Context.Queryable<SysPosition>().WhereIF(input.OrgId > 0, it => orgIds.Contains(it.OrgId))//根据组织ID查询
  71. .WhereIF(input.OrgIds != null, it => input.OrgIds.Contains(it.OrgId))//在指定组织列表查询
  72. .WhereIF(!string.IsNullOrEmpty(input.Category), it => it.Category == input.Category)//根据分类
  73. .WhereIF(!string.IsNullOrEmpty(input.Status), it => it.Status == input.Status)//根据状态
  74. .WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Name.Contains(input.SearchKey))//根据关键字查询
  75. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}")
  76. .OrderBy(it => it.SortCode)//排序
  77. .OrderBy(it => it.CreateTime);//排序
  78. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  79. return pageInfo;
  80. }
  81. /// <inheritdoc/>
  82. public async Task<List<PositionTreeOutput>> Tree(PositionTreeInput input)
  83. {
  84. var result = new List<PositionTreeOutput>();//返回结果
  85. var sysOrgList = await _sysOrgService.GetListAsync(false);//获取所有组织
  86. var sysPositions = await GetListAsync();//获取所有职位
  87. if (input.OrgIds != null)//根据数据范围查
  88. {
  89. sysOrgList = sysOrgList.Where(it => input.OrgIds.Contains(it.Id)).ToList();//在指定组织列表查询
  90. sysPositions = sysPositions.Where(it => input.OrgIds.Contains(it.OrgId)).ToList();//在指定职位列表查询
  91. }
  92. var posCategory = await _dictService.GetChildrenByDictValue(SysDictConst.POSITION_CATEGORY);//获取职位分类
  93. var topOrgList = sysOrgList.Where(it => it.ParentId == 0).ToList();//获取顶级组织
  94. //遍历顶级组织
  95. foreach (var org in topOrgList)
  96. {
  97. var childIds = await _sysOrgService.GetOrgChildIds(org.Id, true, sysOrgList);//获取组织下的所有子级ID
  98. var orgPositions = sysPositions.Where(it => childIds.Contains(it.OrgId)).ToList();//获取组织下的职位
  99. if (orgPositions.Count == 0) continue;
  100. var positionTreeOutput = new PositionTreeOutput
  101. {
  102. Id = org.Id,
  103. Name = org.Name,
  104. IsPosition = false
  105. };//实例化组织树
  106. //获取组织下的职位职位分类
  107. foreach (var category in posCategory)
  108. {
  109. var id = CommonUtils.GetSingleId();//生成唯一ID临时用,因为前端需要ID
  110. var categoryTreeOutput = new PositionTreeOutput
  111. {
  112. Id = id,
  113. Name = category.DictLabel,
  114. IsPosition = false
  115. };//实例化职位分类树
  116. var positions = orgPositions.Where(it => it.Category == category.DictValue).ToList();//获取职位分类下的职位
  117. //遍历职位,实例化职位树
  118. positions.ForEach(it =>
  119. {
  120. categoryTreeOutput.Children.Add(new PositionTreeOutput()
  121. {
  122. Id = it.Id,
  123. Name = it.Name,
  124. IsPosition = true
  125. });//添加职位
  126. });
  127. positionTreeOutput.Children.Add(categoryTreeOutput);
  128. }
  129. result.Add(positionTreeOutput);
  130. }
  131. return result;
  132. }
  133. /// <inheritdoc />
  134. public async Task<SysPosition> Detail(BaseIdInput input)
  135. {
  136. return await GetSysPositionById(input.Id);
  137. }
  138. #endregion
  139. #region 编辑
  140. /// <inheritdoc />
  141. public async Task Edit(PositionEditInput input, string name = SystemConst.SYS_POS)
  142. {
  143. await CheckInput(input, name);//检查参数
  144. var sysPosition = input.Adapt<SysPosition>();//实体转换
  145. if (await UpdateAsync(sysPosition))//更新数据
  146. await RefreshCache();//刷新缓存
  147. }
  148. /// <inheritdoc />
  149. public async Task RefreshCache()
  150. {
  151. _simpleCacheService.Remove(SystemConst.CACHE_SYS_POSITION);//删除缓存
  152. await GetListAsync();//重新写入缓存
  153. }
  154. #endregion
  155. #region 新增
  156. /// <inheritdoc />
  157. public async Task Add(PositionAddInput input, string name = SystemConst.SYS_POS)
  158. {
  159. await CheckInput(input, name);//检查参数
  160. var sysPosition = input.Adapt<SysPosition>();//实体转换
  161. if (await InsertAsync(sysPosition))//插入数据
  162. await RefreshCache();//刷新缓存
  163. }
  164. #endregion
  165. #region 删除
  166. /// <inheritdoc />
  167. public async Task Delete(BaseIdListInput input, string name = SystemConst.SYS_POS)
  168. {
  169. //获取所有ID
  170. var ids = input.Ids;
  171. if (ids.Count > 0)
  172. {
  173. //如果组织下有用户则不能删除
  174. if (await Context.Queryable<SysUser>().AnyAsync(it => ids.Contains(it.PositionId)))
  175. {
  176. throw Oops.Bah($"请先删除{name}下的用户");
  177. }
  178. //删除职位
  179. if (await DeleteByIdsAsync(ids.Cast<object>().ToArray()))
  180. await RefreshCache();//刷新缓存
  181. }
  182. }
  183. #endregion
  184. #region 方法
  185. /// <summary>
  186. /// 检查输入参数
  187. /// </summary>
  188. /// <param name="sysPosition"></param>
  189. /// <param name="name">名称</param>
  190. private async Task CheckInput(SysPosition sysPosition, string name)
  191. {
  192. //所有分类放一个列表
  193. var posCategoryList = new List<string>
  194. {
  195. CateGoryConst.POSITION_HIGH, CateGoryConst.POSITION_LOW, CateGoryConst.POSITION_MIDDLE
  196. };
  197. if (!posCategoryList.Contains(sysPosition.Category))
  198. throw Oops.Bah($"{name}所属分类错误:{sysPosition.Category}");
  199. var sysPositions = await GetListAsync();//获取全部
  200. if (sysPositions.Any(it => it.OrgId == sysPosition.OrgId && it.Name == sysPosition.Name && it.Id != sysPosition.Id))//判断同级是否有名称重复的
  201. throw Oops.Bah($"存在重复的{name}:{sysPosition.Name}");
  202. if (sysPosition.Id > 0)//如果ID大于0表示编辑
  203. {
  204. var position = sysPositions.Where(it => it.Id == sysPosition.Id).FirstOrDefault();//获取当前职位
  205. if (position == null)
  206. throw Oops.Bah($"{name}不存在");
  207. }
  208. //如果code没填
  209. if (string.IsNullOrEmpty(sysPosition.Code))
  210. {
  211. sysPosition.Code = RandomHelper.CreateRandomString(10);//赋值Code
  212. }
  213. else
  214. {
  215. //判断是否有相同的Code
  216. if (sysPositions.Any(it => it.Code == sysPosition.Code && it.Id != sysPosition.Id))
  217. throw Oops.Bah($"存在重复的编码:{sysPosition.Code}");
  218. }
  219. }
  220. /// <summary>
  221. /// 构建职位选择器
  222. /// </summary>
  223. /// <param name="orgList">组织列表</param>
  224. /// <param name="sysPositions">职位列表</param>
  225. /// <param name="parentId">父Id</param>
  226. /// <returns></returns>
  227. public async Task<List<PositionSelectorOutput>> ConstructPositionSelector(List<SysOrg> orgList, List<SysPosition> sysPositions,
  228. long parentId = SafeCampusConst.ZERO)
  229. {
  230. //找下级组织列表
  231. var orgInfos = orgList.Where(it => it.ParentId == parentId).OrderBy(it => it.SortCode).ToList();
  232. var data = new List<PositionSelectorOutput>();
  233. if (orgInfos.Count > 0)//如果数量大于0
  234. {
  235. foreach (var item in orgInfos)//遍历组织
  236. {
  237. var childIds = await _sysOrgService.GetOrgChildIds(item.Id, true, orgList);//获取组织下的所有子级ID
  238. var orgPositions = sysPositions.Where(it => childIds.Contains(it.OrgId)).ToList();//获取组织下的职位
  239. if (orgPositions.Count > 0)//如果组织和组织下级有职位
  240. {
  241. var positionSelectorOutput = new PositionSelectorOutput
  242. {
  243. Id = item.Id,
  244. Name = item.Name,
  245. Children = await ConstructPositionSelector(orgList, sysPositions, item.Id)//递归
  246. };//实例化职位树
  247. var positions = orgPositions.Where(it => it.OrgId == item.Id).ToList();//获取组织下的职位
  248. if (positions.Count > 0)//如果数量大于0
  249. {
  250. foreach (var position in positions)
  251. {
  252. positionSelectorOutput.Children.Add(new PositionSelectorOutput
  253. {
  254. Id = position.Id,
  255. Name = position.Name
  256. });//添加职位
  257. }
  258. }
  259. data.Add(positionSelectorOutput);//添加到列表
  260. }
  261. }
  262. return data;//返回结果
  263. }
  264. return new List<PositionSelectorOutput>();
  265. }
  266. #endregion 方法
  267. }