SysDeptService.cs 10 KB

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