SysDeptService.cs 10 KB

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