SysDeptServiceImpl.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package com.ruoyi.system.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import com.ruoyi.common.annotation.DataScope;
  9. import com.ruoyi.common.constant.UserConstants;
  10. import com.ruoyi.common.core.domain.TreeSelect;
  11. import com.ruoyi.common.core.domain.entity.SysDept;
  12. import com.ruoyi.common.exception.CustomException;
  13. import com.ruoyi.common.utils.StringUtils;
  14. import com.ruoyi.system.mapper.SysDeptMapper;
  15. import com.ruoyi.system.service.ISysDeptService;
  16. /**
  17. * 部门管理 服务实现
  18. *
  19. * @author ruoyi
  20. */
  21. @Service
  22. public class SysDeptServiceImpl implements ISysDeptService
  23. {
  24. @Autowired
  25. private SysDeptMapper deptMapper;
  26. /**
  27. * 查询部门管理数据
  28. *
  29. * @param dept 部门信息
  30. * @return 部门信息集合
  31. */
  32. @Override
  33. @DataScope(deptAlias = "d")
  34. public List<SysDept> selectDeptList(SysDept dept)
  35. {
  36. return deptMapper.selectDeptList(dept);
  37. }
  38. /**
  39. * 构建前端所需要树结构
  40. *
  41. * @param depts 部门列表
  42. * @return 树结构列表
  43. */
  44. @Override
  45. public List<SysDept> buildDeptTree(List<SysDept> depts)
  46. {
  47. List<SysDept> returnList = new ArrayList<SysDept>();
  48. List<Long> tempList = new ArrayList<Long>();
  49. for (SysDept dept : depts)
  50. {
  51. tempList.add(dept.getDeptId());
  52. }
  53. for (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext();)
  54. {
  55. SysDept dept = (SysDept) iterator.next();
  56. // 如果是顶级节点, 遍历该父节点的所有子节点
  57. if (!tempList.contains(dept.getParentId()))
  58. {
  59. recursionFn(depts, dept);
  60. returnList.add(dept);
  61. }
  62. }
  63. if (returnList.isEmpty())
  64. {
  65. returnList = depts;
  66. }
  67. return returnList;
  68. }
  69. /**
  70. * 构建前端所需要下拉树结构
  71. *
  72. * @param depts 部门列表
  73. * @return 下拉树结构列表
  74. */
  75. @Override
  76. public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
  77. {
  78. List<SysDept> deptTrees = buildDeptTree(depts);
  79. return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
  80. }
  81. /**
  82. * 根据角色ID查询部门树信息
  83. *
  84. * @param roleId 角色ID
  85. * @return 选中部门列表
  86. */
  87. @Override
  88. public List<Integer> selectDeptListByRoleId(Long roleId)
  89. {
  90. return deptMapper.selectDeptListByRoleId(roleId);
  91. }
  92. /**
  93. * 根据部门ID查询信息
  94. *
  95. * @param deptId 部门ID
  96. * @return 部门信息
  97. */
  98. @Override
  99. public SysDept selectDeptById(Long deptId)
  100. {
  101. return deptMapper.selectDeptById(deptId);
  102. }
  103. /**
  104. * 根据ID查询所有子部门(正常状态)
  105. *
  106. * @param deptId 部门ID
  107. * @return 子部门数
  108. */
  109. @Override
  110. public int selectNormalChildrenDeptById(Long deptId)
  111. {
  112. return deptMapper.selectNormalChildrenDeptById(deptId);
  113. }
  114. /**
  115. * 是否存在子节点
  116. *
  117. * @param deptId 部门ID
  118. * @return 结果
  119. */
  120. @Override
  121. public boolean hasChildByDeptId(Long deptId)
  122. {
  123. int result = deptMapper.hasChildByDeptId(deptId);
  124. return result > 0 ? true : false;
  125. }
  126. /**
  127. * 查询部门是否存在用户
  128. *
  129. * @param deptId 部门ID
  130. * @return 结果 true 存在 false 不存在
  131. */
  132. @Override
  133. public boolean checkDeptExistUser(Long deptId)
  134. {
  135. int result = deptMapper.checkDeptExistUser(deptId);
  136. return result > 0 ? true : false;
  137. }
  138. /**
  139. * 校验部门名称是否唯一
  140. *
  141. * @param dept 部门信息
  142. * @return 结果
  143. */
  144. @Override
  145. public String checkDeptNameUnique(SysDept dept)
  146. {
  147. Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  148. SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
  149. if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
  150. {
  151. return UserConstants.NOT_UNIQUE;
  152. }
  153. return UserConstants.UNIQUE;
  154. }
  155. /**
  156. * 新增保存部门信息
  157. *
  158. * @param dept 部门信息
  159. * @return 结果
  160. */
  161. @Override
  162. public int insertDept(SysDept dept)
  163. {
  164. SysDept info = deptMapper.selectDeptById(dept.getParentId());
  165. // 如果父节点不为正常状态,则不允许新增子节点
  166. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
  167. {
  168. throw new CustomException("部门停用,不允许新增");
  169. }
  170. dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  171. return deptMapper.insertDept(dept);
  172. }
  173. /**
  174. * 修改保存部门信息
  175. *
  176. * @param dept 部门信息
  177. * @return 结果
  178. */
  179. @Override
  180. public int updateDept(SysDept dept)
  181. {
  182. SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
  183. SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
  184. if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
  185. {
  186. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  187. String oldAncestors = oldDept.getAncestors();
  188. dept.setAncestors(newAncestors);
  189. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  190. }
  191. int result = deptMapper.updateDept(dept);
  192. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
  193. {
  194. // 如果该部门是启用状态,则启用该部门的所有上级部门
  195. updateParentDeptStatus(dept);
  196. }
  197. return result;
  198. }
  199. /**
  200. * 修改该部门的父级部门状态
  201. *
  202. * @param dept 当前部门
  203. */
  204. private void updateParentDeptStatus(SysDept dept)
  205. {
  206. String updateBy = dept.getUpdateBy();
  207. dept = deptMapper.selectDeptById(dept.getDeptId());
  208. dept.setUpdateBy(updateBy);
  209. deptMapper.updateDeptStatus(dept);
  210. }
  211. /**
  212. * 修改子元素关系
  213. *
  214. * @param deptId 被修改的部门ID
  215. * @param newAncestors 新的父ID集合
  216. * @param oldAncestors 旧的父ID集合
  217. */
  218. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
  219. {
  220. List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
  221. for (SysDept child : children)
  222. {
  223. child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
  224. }
  225. if (children.size() > 0)
  226. {
  227. deptMapper.updateDeptChildren(children);
  228. }
  229. }
  230. /**
  231. * 删除部门管理信息
  232. *
  233. * @param deptId 部门ID
  234. * @return 结果
  235. */
  236. @Override
  237. public int deleteDeptById(Long deptId)
  238. {
  239. return deptMapper.deleteDeptById(deptId);
  240. }
  241. /**
  242. * 递归列表
  243. */
  244. private void recursionFn(List<SysDept> list, SysDept t)
  245. {
  246. // 得到子节点列表
  247. List<SysDept> childList = getChildList(list, t);
  248. t.setChildren(childList);
  249. for (SysDept tChild : childList)
  250. {
  251. if (hasChild(list, tChild))
  252. {
  253. recursionFn(list, tChild);
  254. }
  255. }
  256. }
  257. /**
  258. * 得到子节点列表
  259. */
  260. private List<SysDept> getChildList(List<SysDept> list, SysDept t)
  261. {
  262. List<SysDept> tlist = new ArrayList<SysDept>();
  263. Iterator<SysDept> it = list.iterator();
  264. while (it.hasNext())
  265. {
  266. SysDept n = (SysDept) it.next();
  267. if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
  268. {
  269. tlist.add(n);
  270. }
  271. }
  272. return tlist;
  273. }
  274. /**
  275. * 判断是否有子节点
  276. */
  277. private boolean hasChild(List<SysDept> list, SysDept t)
  278. {
  279. return getChildList(list, t).size() > 0 ? true : false;
  280. }
  281. }