gao.qiang 6 днів тому
батько
коміт
c1e228bb98

+ 49 - 13
base-system/src/main/java/com/ozs/system/service/impl/SysDeptServiceImpl.java

@@ -1,23 +1,12 @@
 package com.ozs.system.service.impl;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import com.ozs.common.core.domain.vo.SysDeptVo;
-import com.ozs.system.domain.SysConfig;
-import com.ozs.system.mapper.SysConfigMapper;
-import com.ozs.system.service.ISysConfigService;
-import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
 import com.ozs.common.annotation.DataScope;
 import com.ozs.common.constant.UserConstants;
 import com.ozs.common.core.domain.TreeSelect;
 import com.ozs.common.core.domain.entity.SysDept;
 import com.ozs.common.core.domain.entity.SysRole;
 import com.ozs.common.core.domain.entity.SysUser;
+import com.ozs.common.core.domain.vo.SysDeptVo;
 import com.ozs.common.core.text.Convert;
 import com.ozs.common.exception.ServiceException;
 import com.ozs.common.utils.SecurityUtils;
@@ -26,8 +15,14 @@ import com.ozs.common.utils.spring.SpringUtils;
 import com.ozs.system.mapper.SysDeptMapper;
 import com.ozs.system.mapper.SysRoleMapper;
 import com.ozs.system.service.ISysDeptService;
+import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 
+import java.util.*;
+import java.util.stream.Collectors;
+
 /**
  * 部门管理 服务实现
  *
@@ -74,9 +69,50 @@ public class SysDeptServiceImpl extends JoinServiceImpl<SysDeptMapper, SysDept>
     @Override
     public List<TreeSelect> selectDeptTreeList(SysDeptVo sysDeptVo) {
         List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(sysDeptVo);
-        return buildDeptTreeSelect(depts);
+        // 创建临时集合存储所有部门(原部门 + 所有父级部门)
+        Set<SysDept> allDeptsSet = new LinkedHashSet<>(depts); // 用LinkedHashSet保证顺序和去重
+        boolean hasTopLevelDept = depts.stream()
+                .anyMatch(dept -> dept.getParentId() != null && dept.getParentId() == 0);
+        System.out.println("-----------" + hasTopLevelDept);
+        if (!hasTopLevelDept) {
+            // 循环原部门,查找所有父级并加入临时集合
+            for (SysDept dept : depts) {
+                findAllParents(dept.getParentId(), (Set<SysDept>) allDeptsSet);
+            }
+        }
+
+
+        // 转换为List,用于构建树
+        List<SysDept> finalDepts = new ArrayList<>(allDeptsSet);
+        return buildDeptTreeSelect(finalDepts);
+    }
+
+
+    /**
+     * 递归查找所有父级部门
+     *
+     * @param parentId 当前需要查找的父级ID
+     */
+    private void findAllParents(Long parentId, Set<SysDept> allDeptsSet) {
+        // 终止条件:父级ID为0或不存在
+        if (parentId == null || parentId == 0) {
+            return;
+        }
+
+        // 获取父级部门
+        SysDept parentDept = deptMapper.selectDeptById(parentId);
+        if (parentDept == null) {
+            return;
+        }
+
+        // 添加到临时集合(Set自动去重,避免重复添加)
+        allDeptsSet.add(parentDept);
+
+        // 递归查找父级的父级
+        findAllParents(parentDept.getParentId(), allDeptsSet);
     }
 
+
     /**
      * 构建前端所需要树结构
      *