SysDeptServiceImpl.java 8.8 KB

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