SysDeptServiceImpl.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package com.ruoyi.system.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import com.ruoyi.common.annotation.DataScope;
  8. import com.ruoyi.common.constant.UserConstants;
  9. import com.ruoyi.common.core.domain.Ztree;
  10. import com.ruoyi.common.exception.BusinessException;
  11. import com.ruoyi.common.utils.StringUtils;
  12. import com.ruoyi.system.domain.SysDept;
  13. import com.ruoyi.system.domain.SysRole;
  14. import com.ruoyi.system.mapper.SysDeptMapper;
  15. import com.ruoyi.system.service.ISysDeptService;
  16. /**
  17. * 部门管理 服务实现
  18. *
  19. * @author ruoyi
  20. */
  21. @Service
  22. public class SysDeptServiceImpl implements ISysDeptService
  23. {
  24. @Autowired
  25. private SysDeptMapper deptMapper;
  26. /**
  27. * 查询部门管理数据
  28. *
  29. * @param dept 部门信息
  30. * @return 部门信息集合
  31. */
  32. @Override
  33. @DataScope(deptAlias = "d")
  34. public List<SysDept> selectDeptList(SysDept dept)
  35. {
  36. return deptMapper.selectDeptList(dept);
  37. }
  38. /**
  39. * 查询部门管理树
  40. *
  41. * @param dept 部门信息
  42. * @return 所有部门信息
  43. */
  44. @Override
  45. @DataScope(deptAlias = "d")
  46. public List<Ztree> selectDeptTree(SysDept dept)
  47. {
  48. List<SysDept> deptList = deptMapper.selectDeptList(dept);
  49. List<Ztree> ztrees = initZtree(deptList);
  50. return ztrees;
  51. }
  52. /**
  53. * 根据角色ID查询部门(数据权限)
  54. *
  55. * @param role 角色对象
  56. * @return 部门列表(数据权限)
  57. */
  58. @Override
  59. public List<Ztree> roleDeptTreeData(SysRole role)
  60. {
  61. Long roleId = role.getRoleId();
  62. List<Ztree> ztrees = new ArrayList<Ztree>();
  63. List<SysDept> deptList = selectDeptList(new SysDept());
  64. if (StringUtils.isNotNull(roleId))
  65. {
  66. List<String> roleDeptList = deptMapper.selectRoleDeptTree(roleId);
  67. ztrees = initZtree(deptList, roleDeptList);
  68. }
  69. else
  70. {
  71. ztrees = initZtree(deptList);
  72. }
  73. return ztrees;
  74. }
  75. /**
  76. * 对象转部门树
  77. *
  78. * @param deptList 部门列表
  79. * @return 树结构列表
  80. */
  81. public List<Ztree> initZtree(List<SysDept> deptList)
  82. {
  83. return initZtree(deptList, null);
  84. }
  85. /**
  86. * 对象转部门树
  87. *
  88. * @param deptList 部门列表
  89. * @param roleDeptList 角色已存在菜单列表
  90. * @return 树结构列表
  91. */
  92. public List<Ztree> initZtree(List<SysDept> deptList, List<String> roleDeptList)
  93. {
  94. List<Ztree> ztrees = new ArrayList<Ztree>();
  95. boolean isCheck = StringUtils.isNotNull(roleDeptList);
  96. for (SysDept dept : deptList)
  97. {
  98. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
  99. {
  100. Ztree ztree = new Ztree();
  101. ztree.setId(dept.getDeptId());
  102. ztree.setpId(dept.getParentId());
  103. ztree.setName(dept.getDeptName());
  104. ztree.setTitle(dept.getDeptName());
  105. if (isCheck)
  106. {
  107. ztree.setChecked(roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
  108. }
  109. ztrees.add(ztree);
  110. }
  111. }
  112. return ztrees;
  113. }
  114. /**
  115. * 查询部门人数
  116. *
  117. * @param parentId 部门ID
  118. * @return 结果
  119. */
  120. @Override
  121. public int selectDeptCount(Long parentId)
  122. {
  123. SysDept dept = new SysDept();
  124. dept.setParentId(parentId);
  125. return deptMapper.selectDeptCount(dept);
  126. }
  127. /**
  128. * 查询部门是否存在用户
  129. *
  130. * @param deptId 部门ID
  131. * @return 结果 true 存在 false 不存在
  132. */
  133. @Override
  134. public boolean checkDeptExistUser(Long deptId)
  135. {
  136. int result = deptMapper.checkDeptExistUser(deptId);
  137. return result > 0 ? true : false;
  138. }
  139. /**
  140. * 删除部门管理信息
  141. *
  142. * @param deptId 部门ID
  143. * @return 结果
  144. */
  145. @Override
  146. public int deleteDeptById(Long deptId)
  147. {
  148. return deptMapper.deleteDeptById(deptId);
  149. }
  150. /**
  151. * 新增保存部门信息
  152. *
  153. * @param dept 部门信息
  154. * @return 结果
  155. */
  156. @Override
  157. public int insertDept(SysDept dept)
  158. {
  159. SysDept info = deptMapper.selectDeptById(dept.getParentId());
  160. // 如果父节点不为"正常"状态,则不允许新增子节点
  161. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
  162. {
  163. throw new BusinessException("部门停用,不允许新增");
  164. }
  165. dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  166. return deptMapper.insertDept(dept);
  167. }
  168. /**
  169. * 修改保存部门信息
  170. *
  171. * @param dept 部门信息
  172. * @return 结果
  173. */
  174. @Override
  175. @Transactional
  176. public int updateDept(SysDept dept)
  177. {
  178. SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
  179. SysDept oldDept = selectDeptById(dept.getDeptId());
  180. if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
  181. {
  182. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  183. String oldAncestors = oldDept.getAncestors();
  184. dept.setAncestors(newAncestors);
  185. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  186. }
  187. int result = deptMapper.updateDept(dept);
  188. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
  189. {
  190. // 如果该部门是启用状态,则启用该部门的所有上级部门
  191. updateParentDeptStatus(dept);
  192. }
  193. return result;
  194. }
  195. /**
  196. * 修改该部门的父级部门状态
  197. *
  198. * @param dept 当前部门
  199. */
  200. private void updateParentDeptStatus(SysDept dept)
  201. {
  202. String updateBy = dept.getUpdateBy();
  203. dept = deptMapper.selectDeptById(dept.getDeptId());
  204. dept.setUpdateBy(updateBy);
  205. deptMapper.updateDeptStatus(dept);
  206. }
  207. /**
  208. * 修改子元素关系
  209. *
  210. * @param deptId 被修改的部门ID
  211. * @param newAncestors 新的父ID集合
  212. * @param oldAncestors 旧的父ID集合
  213. */
  214. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
  215. {
  216. List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
  217. for (SysDept child : children)
  218. {
  219. child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
  220. }
  221. if (children.size() > 0)
  222. {
  223. deptMapper.updateDeptChildren(children);
  224. }
  225. }
  226. /**
  227. * 根据部门ID查询信息
  228. *
  229. * @param deptId 部门ID
  230. * @return 部门信息
  231. */
  232. @Override
  233. public SysDept selectDeptById(Long deptId)
  234. {
  235. return deptMapper.selectDeptById(deptId);
  236. }
  237. /**
  238. * 校验部门名称是否唯一
  239. *
  240. * @param dept 部门信息
  241. * @return 结果
  242. */
  243. @Override
  244. public String checkDeptNameUnique(SysDept dept)
  245. {
  246. Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  247. SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
  248. if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
  249. {
  250. return UserConstants.DEPT_NAME_NOT_UNIQUE;
  251. }
  252. return UserConstants.DEPT_NAME_UNIQUE;
  253. }
  254. }