SysDeptServiceImpl.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext();)
  62. {
  63. SysDept dept = (SysDept) iterator.next();
  64. // 如果是顶级节点, 遍历该父节点的所有子节点
  65. if (!tempList.contains(dept.getParentId()))
  66. {
  67. recursionFn(depts, dept);
  68. returnList.add(dept);
  69. }
  70. }
  71. if (returnList.isEmpty())
  72. {
  73. returnList = depts;
  74. }
  75. return returnList;
  76. }
  77. /**
  78. * 构建前端所需要下拉树结构
  79. *
  80. * @param depts 部门列表
  81. * @return 下拉树结构列表
  82. */
  83. @Override
  84. public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
  85. {
  86. List<SysDept> deptTrees = buildDeptTree(depts);
  87. return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
  88. }
  89. /**
  90. * 根据角色ID查询部门树信息
  91. *
  92. * @param roleId 角色ID
  93. * @return 选中部门列表
  94. */
  95. @Override
  96. public List<Long> selectDeptListByRoleId(Long roleId)
  97. {
  98. SysRole role = roleMapper.selectRoleById(roleId);
  99. return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
  100. }
  101. /**
  102. * 根据部门ID查询信息
  103. *
  104. * @param deptId 部门ID
  105. * @return 部门信息
  106. */
  107. @Override
  108. public SysDept selectDeptById(Long deptId)
  109. {
  110. return deptMapper.selectDeptById(deptId);
  111. }
  112. /**
  113. * 根据ID查询所有子部门(正常状态)
  114. *
  115. * @param deptId 部门ID
  116. * @return 子部门数
  117. */
  118. @Override
  119. public int selectNormalChildrenDeptById(Long deptId)
  120. {
  121. return deptMapper.selectNormalChildrenDeptById(deptId);
  122. }
  123. /**
  124. * 是否存在子节点
  125. *
  126. * @param deptId 部门ID
  127. * @return 结果
  128. */
  129. @Override
  130. public boolean hasChildByDeptId(Long deptId)
  131. {
  132. int result = deptMapper.hasChildByDeptId(deptId);
  133. return result > 0;
  134. }
  135. /**
  136. * 查询部门是否存在用户
  137. *
  138. * @param deptId 部门ID
  139. * @return 结果 true 存在 false 不存在
  140. */
  141. @Override
  142. public boolean checkDeptExistUser(Long deptId)
  143. {
  144. int result = deptMapper.checkDeptExistUser(deptId);
  145. return result > 0;
  146. }
  147. /**
  148. * 校验部门名称是否唯一
  149. *
  150. * @param dept 部门信息
  151. * @return 结果
  152. */
  153. @Override
  154. public String checkDeptNameUnique(SysDept dept)
  155. {
  156. Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  157. SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
  158. if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
  159. {
  160. return UserConstants.NOT_UNIQUE;
  161. }
  162. return UserConstants.UNIQUE;
  163. }
  164. /**
  165. * 校验部门是否有数据权限
  166. *
  167. * @param deptId 部门id
  168. */
  169. @Override
  170. public void checkDeptDataScope(Long deptId)
  171. {
  172. if (!SysUser.isAdmin(SecurityUtils.getUserId()))
  173. {
  174. SysDept dept = new SysDept();
  175. dept.setDeptId(deptId);
  176. List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
  177. if (StringUtils.isEmpty(depts))
  178. {
  179. throw new ServiceException("没有权限访问部门数据!");
  180. }
  181. }
  182. }
  183. /**
  184. * 新增保存部门信息
  185. *
  186. * @param dept 部门信息
  187. * @return 结果
  188. */
  189. @Override
  190. public int insertDept(SysDept dept)
  191. {
  192. SysDept info = deptMapper.selectDeptById(dept.getParentId());
  193. // 如果父节点不为正常状态,则不允许新增子节点
  194. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
  195. {
  196. throw new ServiceException("部门停用,不允许新增");
  197. }
  198. dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  199. return deptMapper.insertDept(dept);
  200. }
  201. /**
  202. * 修改保存部门信息
  203. *
  204. * @param dept 部门信息
  205. * @return 结果
  206. */
  207. @Override
  208. public int updateDept(SysDept dept)
  209. {
  210. SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
  211. SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
  212. if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
  213. {
  214. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  215. String oldAncestors = oldDept.getAncestors();
  216. dept.setAncestors(newAncestors);
  217. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  218. }
  219. int result = deptMapper.updateDept(dept);
  220. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
  221. && !StringUtils.equals("0", dept.getAncestors()))
  222. {
  223. // 如果该部门是启用状态,则启用该部门的所有上级部门
  224. updateParentDeptStatusNormal(dept);
  225. }
  226. return result;
  227. }
  228. /**
  229. * 修改该部门的父级部门状态
  230. *
  231. * @param dept 当前部门
  232. */
  233. private void updateParentDeptStatusNormal(SysDept dept)
  234. {
  235. String ancestors = dept.getAncestors();
  236. Long[] deptIds = Convert.toLongArray(ancestors);
  237. deptMapper.updateDeptStatusNormal(deptIds);
  238. }
  239. /**
  240. * 修改子元素关系
  241. *
  242. * @param deptId 被修改的部门ID
  243. * @param newAncestors 新的父ID集合
  244. * @param oldAncestors 旧的父ID集合
  245. */
  246. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
  247. {
  248. List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
  249. for (SysDept child : children)
  250. {
  251. child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
  252. }
  253. if (children.size() > 0)
  254. {
  255. deptMapper.updateDeptChildren(children);
  256. }
  257. }
  258. /**
  259. * 删除部门管理信息
  260. *
  261. * @param deptId 部门ID
  262. * @return 结果
  263. */
  264. @Override
  265. public int deleteDeptById(Long deptId)
  266. {
  267. return deptMapper.deleteDeptById(deptId);
  268. }
  269. /**
  270. * 递归列表
  271. */
  272. private void recursionFn(List<SysDept> list, SysDept t)
  273. {
  274. // 得到子节点列表
  275. List<SysDept> childList = getChildList(list, t);
  276. t.setChildren(childList);
  277. for (SysDept tChild : childList)
  278. {
  279. if (hasChild(list, tChild))
  280. {
  281. recursionFn(list, tChild);
  282. }
  283. }
  284. }
  285. /**
  286. * 得到子节点列表
  287. */
  288. private List<SysDept> getChildList(List<SysDept> list, SysDept t)
  289. {
  290. List<SysDept> tlist = new ArrayList<SysDept>();
  291. Iterator<SysDept> it = list.iterator();
  292. while (it.hasNext())
  293. {
  294. SysDept n = (SysDept) it.next();
  295. if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
  296. {
  297. tlist.add(n);
  298. }
  299. }
  300. return tlist;
  301. }
  302. /**
  303. * 判断是否有子节点
  304. */
  305. private boolean hasChild(List<SysDept> list, SysDept t)
  306. {
  307. return getChildList(list, t).size() > 0;
  308. }
  309. }