ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
@@ -2,10 +2,12 @@
import java.util.*;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.*;
import org.springframework.stereotype.Service;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
@@ -57,6 +59,12 @@
    public List<TreeSelect> selectDeptTreeList(SysDept dept)
    {
        List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
        return buildDeptTreeSelect(depts);
    }
    @Override
    public List<TreeSelect> selectDeptTreeListNoDataScope(SysDept dept) {
        List<SysDept> depts = deptMapper.selectDeptListNoDataScope(dept);
        return buildDeptTreeSelect(depts);
    }
@@ -335,5 +343,165 @@
        return getChildList(list, t).size() > 0;
    }
    @Override
    public List<Long> deptIdBySub(Long deptId) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("dept_id").apply("string_to_array( ancestors, ',' ) @> ARRAY [ '"+deptId+"']");
//                in("cast(ancestors as bigint)",deptId);
        List<SysDept> list = deptMapper.selectList(queryWrapper);
        List<Long> deptList = new ArrayList<>();
        deptList.add(deptId);
        for (int i = 0; i < list.size(); i++) {
            deptList.add(list.get(i).getDeptId());
        }
        return deptList;
    }
    @Override
    public List<SysDept> all() {
        return deptMapper.selectList(null);
    }
    @Override
    public List<SysDept> allByUser(List<Long> deptList) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper();
        queryWrapper.in("dept_id",deptList).eq("del_flag","0");
        return deptMapper.selectList(queryWrapper);
    }
    @Override
    public List<TreeDeptWell> wellTree(List<TreeDeptWell> depts) {
        List<TreeDeptWell> deptTrees = buildDeptWellTree(depts);
        return deptTrees;
//        return deptTrees.stream().map(TreeSelectWell::new).collect(Collectors.toList());
    }
    @Override
    public List<TreeDeptWell> buildDeptWellTree(List<TreeDeptWell> depts)
    {
        List<TreeDeptWell> returnList = new ArrayList<TreeDeptWell>();
        List<Long> tempList = depts.stream().map(TreeDeptWell::getDeptId).collect(Collectors.toList());
        for (TreeDeptWell dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFnWell(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }
    private void recursionFnWell(List<TreeDeptWell> list, TreeDeptWell t)
    {
        // 得到子节点列表
        if(t.getId() == null){
            List<TreeDeptWell> childList = getChildListWell(list, t);
            t.setChildren(childList);
            for (TreeDeptWell tChild : childList)
            {
                if (hasChildWell(list, tChild))
                {
                    recursionFnWell(list, tChild);
                }
            }
        }
    }
    private List<TreeDeptWell> getChildListWell(List<TreeDeptWell> list, TreeDeptWell t)
    {
        List<TreeDeptWell> tlist = new ArrayList<TreeDeptWell>();
        Iterator<TreeDeptWell> it = list.iterator();
        while (it.hasNext())
        {
            TreeDeptWell n = (TreeDeptWell) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    private boolean hasChildWell(List<TreeDeptWell> list, TreeDeptWell t)
    {
        return getChildListWell(list, t).size() > 0;
    }
    @Override
    public List<DeptUserTree> deptUserTree(List<DeptUserTree> depts)
    {
        List<DeptUserTree> returnList = new ArrayList<>();
        List<Long> tempList = depts.stream().map(DeptUserTree::getDeptId).collect(Collectors.toList());
        for (DeptUserTree dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFnDeptUserTree(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }
    @Override
    public List<Long> selectDeptIdBySubAndUserId(Long deptId, String usersId) {
        List<Long> deptIdList = new ArrayList();
        //本级及下属部门
        List<Long> ownAndSubDeptIdList = deptMapper.selectDeptIdBySub(deptId);
        //自定义
        List<Long> roleDeptIdList = deptMapper.selectRoleDeptIdByUsersId(usersId);
        //去重
        Set<Long> deptIdSet = new HashSet();
        deptIdSet.addAll(ownAndSubDeptIdList);
        deptIdSet.addAll(roleDeptIdList);
        deptIdList.addAll(deptIdSet);
        return deptIdList;
    }
    private void recursionFnDeptUserTree(List<DeptUserTree> list, DeptUserTree t)
    {
        // 得到子节点列表
        if(t.getUserId()==null){
            List<DeptUserTree> childList = getChildListDeptUserTree(list, t);
            t.setChildren(childList);
            for (DeptUserTree tChild : childList) {
                if (hasChildDeptUserTree(list, tChild)) {
                    recursionFnDeptUserTree(list, tChild);
                }
            }
        }
    }
    private List<DeptUserTree> getChildListDeptUserTree(List<DeptUserTree> list, DeptUserTree t)
    {
        List<DeptUserTree> tlist = new ArrayList<>();
        Iterator<DeptUserTree> it = list.iterator();
        while (it.hasNext())
        {
            DeptUserTree n = (DeptUserTree) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    private boolean hasChildDeptUserTree(List<DeptUserTree> list, DeptUserTree t)
    {
        return getChildListDeptUserTree(list, t).size() > 0;
    }
}