SysDeptServiceImpl.java 9.4 KB

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