SysDeptInternalAgentServiceImpl.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.ydtech.modules.admin.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.ydtech.modules.admin.dao.SysDeptInternalAgentMapper;
  4. import com.ydtech.modules.admin.model.dto.SysDeptInternalAgentDto;
  5. import com.ydtech.modules.admin.model.po.SysDeptInternalAgent;
  6. import com.ydtech.modules.admin.model.vo.SysDeptInternalAgentAddVo;
  7. import com.ydtech.modules.admin.model.vo.SysUserLinkAreaVo;
  8. import com.ydtech.modules.admin.service.SysDeptInternalAgentService;
  9. import com.ydtech.modules.order.entity.BasePageResult;
  10. import com.ydtech.utils.HeadUtils;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.BeanUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.List;
  18. /**
  19. * @author Administrator
  20. * @description 针对表【sys_dept_internal_agent(内部机构管理)】的数据库操作Service实现
  21. * @createDate 2024-06-26 17:28:34
  22. */
  23. @Service
  24. public class SysDeptInternalAgentServiceImpl extends ServiceImpl<SysDeptInternalAgentMapper, SysDeptInternalAgent>
  25. implements SysDeptInternalAgentService {
  26. @Autowired
  27. private SysDeptInternalAgentMapper sysDeptInternalAgentMapper;
  28. /**
  29. * @version
  30. * @author: hxl
  31. * @Date: 2024/6/26 18:53
  32. * @Description: 分页
  33. */
  34. @Override
  35. public BasePageResult<SysDeptInternalAgentDto> queryPage(SysUserLinkAreaVo sysUserLinkAreaVo) {
  36. return null;
  37. }
  38. /**
  39. * @version
  40. * @author: hxl
  41. * @Date: 2024/6/26 18:53
  42. * @Description: 添加或修改
  43. */
  44. @Override
  45. public void addOrUpdate(SysDeptInternalAgentAddVo sysDeptInternalAgentAddVo) {
  46. String parentId = sysDeptInternalAgentAddVo.getParentId();
  47. String id = sysDeptInternalAgentAddVo.getId();
  48. SysDeptInternalAgent sysDept = new SysDeptInternalAgent();
  49. BeanUtils.copyProperties(sysDeptInternalAgentAddVo, sysDept);
  50. String uuid = HeadUtils.get16UUID();
  51. SysDeptInternalAgent sysDeptInternalAgentParent = sysDeptInternalAgentMapper.selectById(parentId);
  52. if(StringUtils.isNotBlank(id)){
  53. //更新操作
  54. sysDeptInternalAgentMapper.updateById(sysDept);
  55. }else{
  56. //插入操作
  57. sysDept.setId(uuid);
  58. sysDept.setParentName(sysDeptInternalAgentParent.getName());
  59. sysDept.setParentIds(sysDeptInternalAgentParent.getParentIds()+","+uuid);
  60. sysDept.setComlevel(sysDeptInternalAgentParent.getComlevel()+1);
  61. sysDeptInternalAgentMapper.insert(sysDept);
  62. }
  63. }
  64. /**
  65. * @version
  66. * @author: hxl
  67. * @Date: 2024/6/26 18:53
  68. * @Description: 删除
  69. */
  70. @Override
  71. public void deleteByIds(List<String> ids) {
  72. sysDeptInternalAgentMapper.deleteBatchIds(ids);
  73. }
  74. /**
  75. * @version
  76. * @author: hxl
  77. * @Date: 2024/6/26 18:54
  78. * @Description: 查询对象
  79. */
  80. @Override
  81. public SysDeptInternalAgent findSysDeptInternalAgentById(String id) {
  82. return sysDeptInternalAgentMapper.selectById(id);
  83. }
  84. @Override
  85. public List<SysDeptInternalAgent> findTree(String deptId, String comtype) {
  86. String parentIds =null;
  87. if(StringUtils.isNotBlank(deptId)){
  88. SysDeptInternalAgent sysDeptInternalAgent = sysDeptInternalAgentMapper.selectById(deptId);
  89. parentIds=sysDeptInternalAgent.getParentIds();
  90. }
  91. List<SysDeptInternalAgent> sysDeptAll =sysDeptInternalAgentMapper.queryDeptList(parentIds,comtype);
  92. List<SysDeptInternalAgent> resultList =new ArrayList<SysDeptInternalAgent>();
  93. List<SysDeptInternalAgent> sysDeptTop = getTopDeptList(sysDeptAll);
  94. for (SysDeptInternalAgent dept : sysDeptTop) {
  95. if(StringUtils.isNotBlank(comtype)){
  96. resultList.add(buildDeptTree(dept,sysDeptAll));
  97. }else{
  98. resultList.add(buildDeptTreeOne(dept,sysDeptAll));
  99. }
  100. }
  101. return resultList;
  102. }
  103. /**
  104. * @version
  105. * @author: hxl
  106. * @Date: 2024/6/28 19:19
  107. * @Description: 处理上下级关系
  108. */
  109. private SysDeptInternalAgent buildDeptTreeOne(SysDeptInternalAgent dept, List<SysDeptInternalAgent> deptList){
  110. for (SysDeptInternalAgent d : deptList) {
  111. if (dept.getId().equals(d.getParentId())) {
  112. if (null == dept.getChildren()) {
  113. dept.setChildren(new ArrayList<SysDeptInternalAgent>());
  114. }
  115. //递归调用
  116. dept.getChildren().add(buildDeptTreeOne(d, deptList));
  117. }
  118. }
  119. return dept;
  120. }
  121. /**
  122. * @version
  123. * @author: hxl
  124. * @Date: 2024/6/28 18:17
  125. * @Description: 获取顶级机构
  126. */
  127. private static List<SysDeptInternalAgent> getTopDeptList(List<SysDeptInternalAgent> sysDeptAll) {
  128. Integer min;
  129. List<Integer> levelList =new ArrayList<>();
  130. List<SysDeptInternalAgent> sysDeptTop=new ArrayList<>();
  131. if(sysDeptAll !=null && sysDeptAll.size()>0){
  132. for(SysDeptInternalAgent sysDeptInternalAgent: sysDeptAll){
  133. levelList.add(Integer.parseInt(sysDeptInternalAgent.getComlevel()));
  134. }
  135. min = Collections.min(levelList);
  136. for(SysDeptInternalAgent sysDeptInternalAgent: sysDeptAll){
  137. if(Integer.parseInt(sysDeptInternalAgent.getComlevel())==min){
  138. sysDeptTop.add(sysDeptInternalAgent);
  139. }
  140. }
  141. }
  142. return sysDeptTop;
  143. }
  144. /**
  145. * @version
  146. * @author: hxl
  147. * @Date: 2024/6/27 8:42
  148. * @Description: 处理爷孙关系
  149. */
  150. private SysDeptInternalAgent buildDeptTree(SysDeptInternalAgent dept, List<SysDeptInternalAgent> deptList){
  151. for (SysDeptInternalAgent d : deptList) {
  152. if (d.getParentIds().contains(dept.getParentIds()) && !d.getParentIds().equals(dept.getParentIds()) ) {
  153. if (null == dept.getChildren()) {
  154. dept.setChildren(new ArrayList<SysDeptInternalAgent>());
  155. }
  156. //递归调用
  157. dept.getChildren().add(buildDeptTree(d, deptList));
  158. }
  159. }
  160. return dept;
  161. }
  162. }