SysDeptServiceImpl.java 9.4 KB

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