SysDeptServiceImpl.java 8.1 KB

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