SysDeptServiceImpl.java 8.6 KB

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