SysDeptServiceImpl.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.ruoyi.system.service.impl;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.lang.tree.Tree;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  6. import com.ruoyi.common.annotation.DataScope;
  7. import com.ruoyi.common.constant.UserConstants;
  8. import com.ruoyi.common.core.domain.entity.SysDept;
  9. import com.ruoyi.common.core.domain.entity.SysRole;
  10. import com.ruoyi.common.core.domain.entity.SysUser;
  11. import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
  12. import com.ruoyi.common.exception.ServiceException;
  13. import com.ruoyi.common.utils.SecurityUtils;
  14. import com.ruoyi.common.utils.StringUtils;
  15. import com.ruoyi.common.utils.TreeBuildUtils;
  16. import com.ruoyi.common.utils.spring.SpringUtils;
  17. import com.ruoyi.system.mapper.SysDeptMapper;
  18. import com.ruoyi.system.mapper.SysRoleMapper;
  19. import com.ruoyi.system.mapper.SysUserMapper;
  20. import com.ruoyi.system.service.ISysDeptService;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. /**
  26. * 部门管理 服务实现
  27. *
  28. * @author Lion Li
  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<Tree<Long>> buildDeptTreeSelect(List<SysDept> depts) {
  55. if (CollUtil.isEmpty(depts)) {
  56. return CollUtil.newArrayList();
  57. }
  58. Long parentId = depts.get(0).getParentId();
  59. return TreeBuildUtils.build(depts, parentId, (dept, tree) ->
  60. tree.setId(dept.getDeptId())
  61. .setParentId(dept.getParentId())
  62. .setName(dept.getDeptName())
  63. .setWeight(dept.getOrderNum()));
  64. }
  65. /**
  66. * 根据角色ID查询部门树信息
  67. *
  68. * @param roleId 角色ID
  69. * @return 选中部门列表
  70. */
  71. @Override
  72. public List<Long> selectDeptListByRoleId(Long roleId) {
  73. SysRole role = roleMapper.selectById(roleId);
  74. return baseMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
  75. }
  76. /**
  77. * 根据部门ID查询信息
  78. *
  79. * @param deptId 部门ID
  80. * @return 部门信息
  81. */
  82. @Override
  83. public SysDept selectDeptById(Long deptId) {
  84. return getById(deptId);
  85. }
  86. /**
  87. * 根据ID查询所有子部门(正常状态)
  88. *
  89. * @param deptId 部门ID
  90. * @return 子部门数
  91. */
  92. @Override
  93. public long selectNormalChildrenDeptById(Long deptId) {
  94. return count(new LambdaQueryWrapper<SysDept>()
  95. .eq(SysDept::getStatus, 0)
  96. .apply("find_in_set({0}, ancestors)", deptId));
  97. }
  98. /**
  99. * 是否存在子节点
  100. *
  101. * @param deptId 部门ID
  102. * @return 结果
  103. */
  104. @Override
  105. public boolean hasChildByDeptId(Long deptId) {
  106. long result = count(new LambdaQueryWrapper<SysDept>()
  107. .eq(SysDept::getParentId, deptId));
  108. return result > 0;
  109. }
  110. /**
  111. * 查询部门是否存在用户
  112. *
  113. * @param deptId 部门ID
  114. * @return 结果 true 存在 false 不存在
  115. */
  116. @Override
  117. public boolean checkDeptExistUser(Long deptId) {
  118. long result = userMapper.selectCount(new LambdaQueryWrapper<SysUser>()
  119. .eq(SysUser::getDeptId, deptId));
  120. return result > 0;
  121. }
  122. /**
  123. * 校验部门名称是否唯一
  124. *
  125. * @param dept 部门信息
  126. * @return 结果
  127. */
  128. @Override
  129. public String checkDeptNameUnique(SysDept dept) {
  130. Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  131. long count = count(new LambdaQueryWrapper<SysDept>()
  132. .eq(SysDept::getDeptName, dept.getDeptName())
  133. .eq(SysDept::getParentId, dept.getParentId())
  134. .ne(SysDept::getDeptId, deptId));
  135. if (count > 0) {
  136. return UserConstants.NOT_UNIQUE;
  137. }
  138. return UserConstants.UNIQUE;
  139. }
  140. /**
  141. * 校验部门是否有数据权限
  142. *
  143. * @param deptId 部门id
  144. */
  145. @Override
  146. public void checkDeptDataScope(Long deptId) {
  147. if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
  148. SysDept dept = new SysDept();
  149. dept.setDeptId(deptId);
  150. List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
  151. if (StringUtils.isEmpty(depts)) {
  152. throw new ServiceException("没有权限访问部门数据!");
  153. }
  154. }
  155. }
  156. /**
  157. * 新增保存部门信息
  158. *
  159. * @param dept 部门信息
  160. * @return 结果
  161. */
  162. @Override
  163. public int insertDept(SysDept dept) {
  164. SysDept info = getById(dept.getParentId());
  165. // 如果父节点不为正常状态,则不允许新增子节点
  166. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
  167. throw new ServiceException("部门停用,不允许新增");
  168. }
  169. dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  170. return baseMapper.insert(dept);
  171. }
  172. /**
  173. * 修改保存部门信息
  174. *
  175. * @param dept 部门信息
  176. * @return 结果
  177. */
  178. @Override
  179. public int updateDept(SysDept dept) {
  180. SysDept newParentDept = getById(dept.getParentId());
  181. SysDept oldDept = getById(dept.getDeptId());
  182. if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) {
  183. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  184. String oldAncestors = oldDept.getAncestors();
  185. dept.setAncestors(newAncestors);
  186. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  187. }
  188. int result = baseMapper.updateById(dept);
  189. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
  190. && !StringUtils.equals("0", dept.getAncestors())) {
  191. // 如果该部门是启用状态,则启用该部门的所有上级部门
  192. updateParentDeptStatusNormal(dept);
  193. }
  194. return result;
  195. }
  196. /**
  197. * 修改该部门的父级部门状态
  198. *
  199. * @param dept 当前部门
  200. */
  201. private void updateParentDeptStatusNormal(SysDept dept) {
  202. String ancestors = dept.getAncestors();
  203. Long[] deptIds = Convert.toLongArray(ancestors);
  204. update(null, new LambdaUpdateWrapper<SysDept>()
  205. .set(SysDept::getStatus, "0")
  206. .in(SysDept::getDeptId, Arrays.asList(deptIds)));
  207. }
  208. /**
  209. * 修改子元素关系
  210. *
  211. * @param deptId 被修改的部门ID
  212. * @param newAncestors 新的父ID集合
  213. * @param oldAncestors 旧的父ID集合
  214. */
  215. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
  216. List<SysDept> children = list(new LambdaQueryWrapper<SysDept>()
  217. .apply("find_in_set({0},ancestors)", deptId));
  218. for (SysDept child : children) {
  219. child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
  220. }
  221. if (children.size() > 0) {
  222. baseMapper.updateDeptChildren(children);
  223. }
  224. }
  225. /**
  226. * 删除部门管理信息
  227. *
  228. * @param deptId 部门ID
  229. * @return 结果
  230. */
  231. @Override
  232. public int deleteDeptById(Long deptId) {
  233. return baseMapper.deleteById(deptId);
  234. }
  235. }