SysDeptServiceImpl.java 9.6 KB

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