SysDeptServiceImpl.java 11 KB

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