SysDeptServiceImpl.java 8.6 KB

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