SysDeptServiceImpl.java 8.3 KB

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