SysDeptServiceImpl.java 8.9 KB

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