SysDeptServiceImpl.java 9.2 KB

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