SysDeptServiceImpl.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package com.ruoyi.system.service.impl;
  2. import cn.hutool.core.lang.Validator;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  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.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.Iterator;
  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 ServiceImpl<SysDeptMapper, 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 (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext(); ) {
  61. SysDept dept = (SysDept) iterator.next();
  62. // 如果是顶级节点, 遍历该父节点的所有子节点
  63. if (!tempList.contains(dept.getParentId())) {
  64. recursionFn(depts, dept);
  65. returnList.add(dept);
  66. }
  67. }
  68. if (returnList.isEmpty()) {
  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. List<SysDept> deptTrees = buildDeptTree(depts);
  82. return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
  83. }
  84. /**
  85. * 根据角色ID查询部门树信息
  86. *
  87. * @param roleId 角色ID
  88. * @return 选中部门列表
  89. */
  90. @Override
  91. public List<Integer> selectDeptListByRoleId(Long roleId) {
  92. SysRole role = roleMapper.selectById(roleId);
  93. return baseMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
  94. }
  95. /**
  96. * 根据部门ID查询信息
  97. *
  98. * @param deptId 部门ID
  99. * @return 部门信息
  100. */
  101. @Override
  102. public SysDept selectDeptById(Long deptId) {
  103. return getById(deptId);
  104. }
  105. /**
  106. * 根据ID查询所有子部门(正常状态)
  107. *
  108. * @param deptId 部门ID
  109. * @return 子部门数
  110. */
  111. @Override
  112. public int selectNormalChildrenDeptById(Long deptId) {
  113. return count(new LambdaQueryWrapper<SysDept>()
  114. .eq(SysDept::getStatus, 0)
  115. .apply("find_in_set({0}, ancestors)", deptId));
  116. }
  117. /**
  118. * 是否存在子节点
  119. *
  120. * @param deptId 部门ID
  121. * @return 结果
  122. */
  123. @Override
  124. public boolean hasChildByDeptId(Long deptId) {
  125. int result = count(new LambdaQueryWrapper<SysDept>()
  126. .eq(SysDept::getParentId, deptId)
  127. .last("limit 1"));
  128. return result > 0 ? true : false;
  129. }
  130. /**
  131. * 查询部门是否存在用户
  132. *
  133. * @param deptId 部门ID
  134. * @return 结果 true 存在 false 不存在
  135. */
  136. @Override
  137. public boolean checkDeptExistUser(Long deptId) {
  138. int result = userMapper.selectCount(new LambdaQueryWrapper<SysUser>()
  139. .eq(SysUser::getDeptId, deptId));
  140. return result > 0 ? true : false;
  141. }
  142. /**
  143. * 校验部门名称是否唯一
  144. *
  145. * @param dept 部门信息
  146. * @return 结果
  147. */
  148. @Override
  149. public String checkDeptNameUnique(SysDept dept) {
  150. Long deptId = Validator.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  151. SysDept info = getOne(new LambdaQueryWrapper<SysDept>()
  152. .eq(SysDept::getDeptName, dept.getDeptName())
  153. .eq(SysDept::getParentId, dept.getParentId())
  154. .last("limit 1"));
  155. if (Validator.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) {
  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. SysDept info = getById(dept.getParentId());
  169. // 如果父节点不为正常状态,则不允许新增子节点
  170. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
  171. throw new CustomException("部门停用,不允许新增");
  172. }
  173. dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  174. return baseMapper.insert(dept);
  175. }
  176. /**
  177. * 修改保存部门信息
  178. *
  179. * @param dept 部门信息
  180. * @return 结果
  181. */
  182. @Override
  183. public int updateDept(SysDept dept) {
  184. SysDept newParentDept = getById(dept.getParentId());
  185. SysDept oldDept = getById(dept.getDeptId());
  186. if (Validator.isNotNull(newParentDept) && Validator.isNotNull(oldDept)) {
  187. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  188. String oldAncestors = oldDept.getAncestors();
  189. dept.setAncestors(newAncestors);
  190. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  191. }
  192. int result = baseMapper.updateById(dept);
  193. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus())) {
  194. // 如果该部门是启用状态,则启用该部门的所有上级部门
  195. updateParentDeptStatus(dept);
  196. }
  197. return result;
  198. }
  199. /**
  200. * 修改该部门的父级部门状态
  201. *
  202. * @param dept 当前部门
  203. */
  204. private void updateParentDeptStatus(SysDept dept) {
  205. String updateBy = dept.getUpdateBy();
  206. dept = getById(dept.getDeptId());
  207. dept.setUpdateBy(updateBy);
  208. update(null,new LambdaUpdateWrapper<SysDept>()
  209. .set(StrUtil.isNotBlank(dept.getStatus()),
  210. SysDept::getStatus,dept.getStatus())
  211. .set(StrUtil.isNotBlank(dept.getUpdateBy()),
  212. SysDept::getUpdateBy,dept.getUpdateBy())
  213. .in(SysDept::getDeptId, Arrays.asList(dept.getAncestors().split(","))));
  214. }
  215. /**
  216. * 修改子元素关系
  217. *
  218. * @param deptId 被修改的部门ID
  219. * @param newAncestors 新的父ID集合
  220. * @param oldAncestors 旧的父ID集合
  221. */
  222. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
  223. List<SysDept> children = list(new LambdaQueryWrapper<SysDept>()
  224. .apply("find_in_set({0},ancestors)",deptId));
  225. for (SysDept child : children) {
  226. child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
  227. }
  228. if (children.size() > 0) {
  229. updateBatchById(children);
  230. }
  231. }
  232. /**
  233. * 删除部门管理信息
  234. *
  235. * @param deptId 部门ID
  236. * @return 结果
  237. */
  238. @Override
  239. public int deleteDeptById(Long deptId) {
  240. return baseMapper.deleteById(deptId);
  241. }
  242. /**
  243. * 递归列表
  244. */
  245. private void recursionFn(List<SysDept> list, SysDept t) {
  246. // 得到子节点列表
  247. List<SysDept> childList = getChildList(list, t);
  248. t.setChildren(childList);
  249. for (SysDept tChild : childList) {
  250. if (hasChild(list, tChild)) {
  251. recursionFn(list, tChild);
  252. }
  253. }
  254. }
  255. /**
  256. * 得到子节点列表
  257. */
  258. private List<SysDept> getChildList(List<SysDept> list, SysDept t) {
  259. List<SysDept> tlist = new ArrayList<SysDept>();
  260. Iterator<SysDept> it = list.iterator();
  261. while (it.hasNext()) {
  262. SysDept n = (SysDept) it.next();
  263. if (Validator.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) {
  264. tlist.add(n);
  265. }
  266. }
  267. return tlist;
  268. }
  269. /**
  270. * 判断是否有子节点
  271. */
  272. private boolean hasChild(List<SysDept> list, SysDept t) {
  273. return getChildList(list, t).size() > 0 ? true : false;
  274. }
  275. }