SysDeptService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using Infrastructure;
  2. using Infrastructure.Attribute;
  3. using ZR.Common;
  4. using ZR.Model.System;
  5. using ZR.Model.System.Dto;
  6. using ZR.Model.System.Vo;
  7. namespace ZR.ServiceCore.Services
  8. {
  9. /// <summary>
  10. /// 部门管理
  11. /// </summary>
  12. [AppService(ServiceType = typeof(ISysDeptService), ServiceLifetime = LifeTime.Transient)]
  13. public class SysDeptService : BaseService<SysDept>, ISysDeptService
  14. {
  15. public ISysRoleDeptService RoleDeptRepository;
  16. public SysDeptService(ISysRoleDeptService roleDeptRepository)
  17. {
  18. RoleDeptRepository = roleDeptRepository;
  19. }
  20. /// <summary>
  21. /// 查询部门管理数据
  22. /// </summary>
  23. /// <param name="dept"></param>
  24. /// <returns></returns>
  25. public List<SysDeptDto> GetList(SysDeptQueryDto dept)
  26. {
  27. var predicate = Expressionable.Create<SysDept>();
  28. predicate = predicate.And(it => it.DelFlag == 0);
  29. predicate = predicate.AndIF(dept.DeptName.IfNotEmpty(), it => it.DeptName.Contains(dept.DeptName));
  30. predicate = predicate.AndIF(dept.Status != null, it => it.Status == dept.Status);
  31. var response = Queryable()
  32. .Where(predicate.ToExpression())
  33. .Select((it) => new SysDeptDto()
  34. {
  35. UserNum = SqlFunc.Subqueryable<SysUser>().Where(f => f.DeptId == it.DeptId).Count()
  36. }, true)
  37. .ToList();
  38. return response;
  39. }
  40. /// <summary>
  41. /// 查询部门管理数据
  42. /// </summary>
  43. /// <param name="dept"></param>
  44. /// <returns></returns>
  45. public List<SysDept> GetSysDepts(SysDeptQueryDto dept)
  46. {
  47. var predicate = Expressionable.Create<SysDept>();
  48. predicate = predicate.And(it => it.DelFlag == 0);
  49. predicate = predicate.AndIF(dept.DeptName.IfNotEmpty(), it => it.DeptName.Contains(dept.DeptName));
  50. predicate = predicate.AndIF(dept.Status != null, it => it.Status == dept.Status);
  51. var response = Queryable().Where(predicate.ToExpression()).ToList();
  52. return response;
  53. }
  54. /// <summary>
  55. /// 校验部门名称是否唯一
  56. /// </summary>
  57. /// <param name="dept"></param>
  58. /// <returns></returns>
  59. public string CheckDeptNameUnique(SysDept dept)
  60. {
  61. long deptId = dept.DeptId == 0 ? -1L : dept.DeptId;
  62. SysDept info = GetFirst(it => it.DeptName == dept.DeptName && it.ParentId == dept.ParentId);
  63. if (info != null && info.DeptId != deptId)
  64. {
  65. return UserConstants.NOT_UNIQUE;
  66. }
  67. return UserConstants.UNIQUE;
  68. }
  69. /// <summary>
  70. /// 新增保存部门信息
  71. /// </summary>
  72. /// <param name="dept"></param>
  73. /// <returns></returns>
  74. public int InsertDept(SysDept dept)
  75. {
  76. SysDept info = GetFirst(it => it.DeptId == dept.ParentId);
  77. //如果父节点不为正常状态,则不允许新增子节点
  78. if (info != null && UserConstants.DEPT_NORMAL != info?.Status)
  79. {
  80. throw new CustomException("部门停用,不允许新增");
  81. }
  82. dept.Ancestors = "";
  83. if (info != null)
  84. {
  85. dept.Ancestors = info.Ancestors + "," + dept.ParentId;
  86. }
  87. return Add(dept);
  88. }
  89. /// <summary>
  90. /// 修改保存部门信息
  91. /// </summary>
  92. /// <param name="dept"></param>
  93. /// <returns></returns>
  94. public int UpdateDept(SysDept dept)
  95. {
  96. SysDept newParentDept = GetFirst(it => it.DeptId == dept.ParentId);
  97. SysDept oldDept = GetFirst(m => m.DeptId == dept.DeptId);
  98. if (newParentDept != null && oldDept != null)
  99. {
  100. string newAncestors = newParentDept.Ancestors + "," + newParentDept.DeptId;
  101. string oldAncestors = oldDept.Ancestors;
  102. dept.Ancestors = newAncestors;
  103. UpdateDeptChildren(dept.DeptId, newAncestors, oldAncestors);
  104. }
  105. int result = Context.Updateable(dept).ExecuteCommand();
  106. if (UserConstants.DEPT_NORMAL.Equals(dept.Status) && dept.Ancestors.IfNotEmpty()
  107. && !"0".Equals(dept.Ancestors))
  108. {
  109. // 如果该部门是启用状态,则启用该部门的所有上级部门
  110. UpdateParentDeptStatusNormal(dept);
  111. }
  112. return result;
  113. }
  114. /// <summary>
  115. /// 修改该部门的父级部门状态
  116. /// </summary>
  117. /// <param name="dept">当前部门</param>
  118. private void UpdateParentDeptStatusNormal(SysDept dept)
  119. {
  120. long[] depts = Tools.SpitLongArrary(dept.Ancestors);
  121. dept.Status = 0;
  122. dept.Update_time = DateTime.Now;
  123. Update(dept, it => new { it.Update_by, it.Update_time, it.Status }, f => depts.Contains(f.DeptId));
  124. }
  125. /// <summary>
  126. /// 修改子元素关系
  127. /// </summary>
  128. /// <param name="deptId">被修改的部门ID</param>
  129. /// <param name="newAncestors">新的父ID集合</param>
  130. /// <param name="oldAncestors">旧的父ID集合</param>
  131. public void UpdateDeptChildren(long deptId, string newAncestors, string oldAncestors)
  132. {
  133. List<SysDept> children = GetChildrenDepts(GetSysDepts(new SysDeptQueryDto()), deptId);
  134. foreach (var child in children)
  135. {
  136. string ancestors = child.Ancestors.ReplaceFirst(oldAncestors, newAncestors);
  137. long[] ancestorsArr = Tools.SpitLongArrary(ancestors).Distinct().ToArray();
  138. child.Ancestors = string.Join(",", ancestorsArr);
  139. }
  140. if (children.Any())
  141. {
  142. Context.Updateable(children).WhereColumns(f => new { f.DeptId })
  143. .UpdateColumns(it => new { it.Ancestors }).ExecuteCommand();
  144. }
  145. }
  146. /// <summary>
  147. /// 获取所有子部门
  148. /// </summary>
  149. /// <param name="depts"></param>
  150. /// <param name="deptId"></param>
  151. /// <returns></returns>
  152. public List<SysDept> GetChildrenDepts(List<SysDept> depts, long deptId)
  153. {
  154. return depts.FindAll(delegate (SysDept item)
  155. {
  156. long[] pid = Tools.SpitLongArrary(item.Ancestors);
  157. return pid.Contains(deptId);
  158. });
  159. }
  160. /// <summary>
  161. /// 构建前端所需要树结构
  162. /// </summary>
  163. /// <param name="depts">部门列表</param>
  164. /// <returns></returns>
  165. public List<SysDept> BuildDeptTree(List<SysDept> depts)
  166. {
  167. List<SysDept> returnList = new List<SysDept>();
  168. List<long> tempList = depts.Select(f => f.DeptId).ToList();
  169. foreach (var dept in depts)
  170. {
  171. // 如果是顶级节点, 遍历该父节点的所有子节点
  172. if (!tempList.Contains(dept.ParentId))
  173. {
  174. RecursionFn(depts, dept);
  175. returnList.Add(dept);
  176. }
  177. }
  178. if (!returnList.Any())
  179. {
  180. returnList = depts;
  181. }
  182. return returnList;
  183. }
  184. /// <summary>
  185. /// 构建前端所需下拉树结构
  186. /// </summary>
  187. /// <param name="depts"></param>
  188. /// <returns></returns>
  189. public List<TreeSelectVo> BuildDeptTreeSelect(List<SysDept> depts)
  190. {
  191. List<SysDept> menuTrees = BuildDeptTree(depts);
  192. List<TreeSelectVo> treeMenuVos = new List<TreeSelectVo>();
  193. foreach (var item in menuTrees)
  194. {
  195. treeMenuVos.Add(new TreeSelectVo(item));
  196. }
  197. return treeMenuVos;
  198. }
  199. /// <summary>
  200. /// 递归列表
  201. /// </summary>
  202. /// <param name="list"></param>
  203. /// <param name="t"></param>
  204. private void RecursionFn(List<SysDept> list, SysDept t)
  205. {
  206. //得到子节点列表
  207. List<SysDept> childList = GetChildList(list, t);
  208. t.children = childList;
  209. foreach (var item in childList)
  210. {
  211. if (GetChildList(list, item).Count() > 0)
  212. {
  213. RecursionFn(list, item);
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// 递归获取子菜单
  219. /// </summary>
  220. /// <param name="list">所有菜单</param>
  221. /// <param name="dept"></param>
  222. /// <returns></returns>
  223. private List<SysDept> GetChildList(List<SysDept> list, SysDept dept)
  224. {
  225. return list.Where(p => p.ParentId == dept.DeptId).ToList();
  226. }
  227. #region 角色部门
  228. /// <summary>
  229. /// 根据角色获取菜单id
  230. /// </summary>
  231. /// <param name="roleId"></param>
  232. /// <returns></returns>
  233. public List<SysRoleDept> SelectRoleDeptByRoleId(long roleId)
  234. {
  235. return RoleDeptRepository.SelectRoleDeptByRoleId(roleId);
  236. }
  237. /// <summary>
  238. /// 获取角色部门id集合
  239. /// </summary>
  240. /// <param name="roleId"></param>
  241. /// <returns></returns>
  242. public List<long> SelectRoleDepts(long roleId)
  243. {
  244. var list = SelectRoleDeptByRoleId(roleId);
  245. return list.Select(x => x.DeptId).Distinct().ToList();
  246. }
  247. /// <summary>
  248. /// 删除角色部门数据
  249. /// </summary>
  250. /// <param name="roleId"></param>
  251. /// <returns></returns>
  252. public bool DeleteRoleDeptByRoleId(long roleId)
  253. {
  254. return RoleDeptRepository.Delete(f => f.RoleId == roleId);
  255. }
  256. /// <summary>
  257. /// 批量插入角色部门
  258. /// </summary>
  259. /// <param name="role"></param>
  260. /// <returns></returns>
  261. public int InsertRoleDepts(SysRole role)
  262. {
  263. int rows = 1;
  264. List<SysRoleDept> list = new();
  265. foreach (var item in role.DeptIds)
  266. {
  267. list.Add(new SysRoleDept() { DeptId = item, RoleId = role.RoleId });
  268. }
  269. if (list.Count > 0)
  270. {
  271. rows = RoleDeptRepository.Insert(list);
  272. }
  273. return rows;
  274. }
  275. #endregion
  276. }
  277. /// <summary>
  278. /// 角色部门
  279. /// </summary>
  280. [AppService(ServiceType = typeof(ISysRoleDeptService), ServiceLifetime = LifeTime.Transient)]
  281. public class SysRoleDeptService : BaseService<SysRoleDept>, ISysRoleDeptService
  282. {
  283. public List<SysRoleDept> SelectRoleDeptByRoleId(long roleId)
  284. {
  285. return GetList(it => it.RoleId == roleId).ToList();
  286. }
  287. }
  288. }