西藏巴青项目

SysDeptServiceImpl.java 11KB

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