SysDeptServiceImpl.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package com.ruoyi.system.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import com.ruoyi.common.annotation.DataScope;
  9. import com.ruoyi.common.constant.UserConstants;
  10. import com.ruoyi.common.core.domain.TreeSelect;
  11. import com.ruoyi.common.core.domain.entity.SysDept;
  12. import com.ruoyi.common.core.domain.entity.SysRole;
  13. import com.ruoyi.common.core.domain.entity.SysUser;
  14. import com.ruoyi.common.core.text.Convert;
  15. import com.ruoyi.common.exception.ServiceException;
  16. import com.ruoyi.common.utils.SecurityUtils;
  17. import com.ruoyi.common.utils.StringUtils;
  18. import com.ruoyi.common.utils.spring.SpringUtils;
  19. import com.ruoyi.system.mapper.SysDeptMapper;
  20. import com.ruoyi.system.mapper.SysRoleMapper;
  21. import com.ruoyi.system.service.ISysDeptService;
  22. /**
  23. * 部门管理 服务实现
  24. *
  25. * @author ruoyi
  26. */
  27. @Service
  28. public class SysDeptServiceImpl implements ISysDeptService
  29. {
  30. @Autowired
  31. private SysDeptMapper deptMapper;
  32. @Autowired
  33. private SysRoleMapper roleMapper;
  34. /**
  35. * 查询部门管理数据
  36. *
  37. * @param dept 部门信息
  38. * @return 部门信息集合
  39. */
  40. @Override
  41. @DataScope(deptAlias = "d")
  42. public List<SysDept> selectDeptList(SysDept dept)
  43. {
  44. return deptMapper.selectDeptList(dept);
  45. }
  46. /**
  47. * 构建前端所需要树结构
  48. *
  49. * @param depts 部门列表
  50. * @return 树结构列表
  51. */
  52. @Override
  53. public List<SysDept> buildDeptTree(List<SysDept> depts)
  54. {
  55. List<SysDept> returnList = new ArrayList<SysDept>();
  56. List<Long> tempList = new ArrayList<Long>();
  57. for (SysDept dept : depts)
  58. {
  59. tempList.add(dept.getDeptId());
  60. }
  61. for (SysDept dept : depts) {
  62. // 如果是顶级节点, 遍历该父节点的所有子节点
  63. if (!tempList.contains(dept.getParentId())) {
  64. recursionFn(depts, dept);
  65. returnList.add(dept);
  66. }
  67. }
  68. if (returnList.isEmpty())
  69. {
  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. {
  83. List<SysDept> deptTrees = buildDeptTree(depts);
  84. return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
  85. }
  86. /**
  87. * 根据角色ID查询部门树信息
  88. *
  89. * @param roleId 角色ID
  90. * @return 选中部门列表
  91. */
  92. @Override
  93. public List<Long> selectDeptListByRoleId(Long roleId)
  94. {
  95. SysRole role = roleMapper.selectRoleById(roleId);
  96. return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
  97. }
  98. /**
  99. * 根据部门ID查询信息
  100. *
  101. * @param deptId 部门ID
  102. * @return 部门信息
  103. */
  104. @Override
  105. public SysDept selectDeptById(Long deptId)
  106. {
  107. return deptMapper.selectDeptById(deptId);
  108. }
  109. /**
  110. * 根据ID查询所有子部门(正常状态)
  111. *
  112. * @param deptId 部门ID
  113. * @return 子部门数
  114. */
  115. @Override
  116. public int selectNormalChildrenDeptById(Long deptId)
  117. {
  118. return deptMapper.selectNormalChildrenDeptById(deptId);
  119. }
  120. /**
  121. * 是否存在子节点
  122. *
  123. * @param deptId 部门ID
  124. * @return 结果
  125. */
  126. @Override
  127. public boolean hasChildByDeptId(Long deptId)
  128. {
  129. int result = deptMapper.hasChildByDeptId(deptId);
  130. return result > 0;
  131. }
  132. /**
  133. * 查询部门是否存在用户
  134. *
  135. * @param deptId 部门ID
  136. * @return 结果 true 存在 false 不存在
  137. */
  138. @Override
  139. public boolean checkDeptExistUser(Long deptId)
  140. {
  141. int result = deptMapper.checkDeptExistUser(deptId);
  142. return result > 0;
  143. }
  144. /**
  145. * 校验部门名称是否唯一
  146. *
  147. * @param dept 部门信息
  148. * @return 结果
  149. */
  150. @Override
  151. public String checkDeptNameUnique(SysDept dept)
  152. {
  153. Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  154. SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
  155. if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
  156. {
  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. {
  189. SysDept info = deptMapper.selectDeptById(dept.getParentId());
  190. // 如果父节点不为正常状态,则不允许新增子节点
  191. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
  192. {
  193. throw new ServiceException("部门停用,不允许新增");
  194. }
  195. dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  196. return deptMapper.insertDept(dept);
  197. }
  198. /**
  199. * 修改保存部门信息
  200. *
  201. * @param dept 部门信息
  202. * @return 结果
  203. */
  204. @Override
  205. public int updateDept(SysDept dept)
  206. {
  207. SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
  208. SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
  209. if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
  210. {
  211. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  212. String oldAncestors = oldDept.getAncestors();
  213. dept.setAncestors(newAncestors);
  214. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  215. }
  216. int result = deptMapper.updateDept(dept);
  217. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
  218. && !StringUtils.equals("0", dept.getAncestors()))
  219. {
  220. // 如果该部门是启用状态,则启用该部门的所有上级部门
  221. updateParentDeptStatusNormal(dept);
  222. }
  223. return result;
  224. }
  225. /**
  226. * 修改该部门的父级部门状态
  227. *
  228. * @param dept 当前部门
  229. */
  230. private void updateParentDeptStatusNormal(SysDept dept)
  231. {
  232. String ancestors = dept.getAncestors();
  233. Long[] deptIds = Convert.toLongArray(ancestors);
  234. deptMapper.updateDeptStatusNormal(deptIds);
  235. }
  236. /**
  237. * 修改子元素关系
  238. *
  239. * @param deptId 被修改的部门ID
  240. * @param newAncestors 新的父ID集合
  241. * @param oldAncestors 旧的父ID集合
  242. */
  243. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
  244. {
  245. List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
  246. for (SysDept child : children)
  247. {
  248. child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
  249. }
  250. if (children.size() > 0)
  251. {
  252. deptMapper.updateDeptChildren(children);
  253. }
  254. }
  255. /**
  256. * 删除部门管理信息
  257. *
  258. * @param deptId 部门ID
  259. * @return 结果
  260. */
  261. @Override
  262. public int deleteDeptById(Long deptId)
  263. {
  264. return deptMapper.deleteDeptById(deptId);
  265. }
  266. /**
  267. * 递归列表
  268. */
  269. private void recursionFn(List<SysDept> list, SysDept t)
  270. {
  271. // 得到子节点列表
  272. List<SysDept> childList = getChildList(list, t);
  273. t.setChildren(childList);
  274. for (SysDept tChild : childList)
  275. {
  276. if (hasChild(list, tChild))
  277. {
  278. recursionFn(list, tChild);
  279. }
  280. }
  281. }
  282. /**
  283. * 得到子节点列表
  284. */
  285. private List<SysDept> getChildList(List<SysDept> list, SysDept t)
  286. {
  287. List<SysDept> tlist = new ArrayList<SysDept>();
  288. Iterator<SysDept> it = list.iterator();
  289. while (it.hasNext())
  290. {
  291. SysDept n = (SysDept) it.next();
  292. if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
  293. {
  294. tlist.add(n);
  295. }
  296. }
  297. return tlist;
  298. }
  299. /**
  300. * 判断是否有子节点
  301. */
  302. private boolean hasChild(List<SysDept> list, SysDept t)
  303. {
  304. return getChildList(list, t).size() > 0;
  305. }
  306. }