ZQN
2024-06-20 3467fa64f4be6efc9b742913419e7c3a501c541b
project-system/src/main/java/com/project/system/service/impl/SysDeptServiceImpl.java
@@ -1,5 +1,7 @@
package com.project.system.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.annotation.DataScope;
import com.project.common.constant.UserConstants;
@@ -19,6 +21,7 @@
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
@@ -320,6 +323,18 @@
        return deptMapper.getApplyDeptIdsByLoginUserId(userId);
    }
    @Override
    public String getDeptAllName(Long deptId)
    {
        List<String> names = new ArrayList<>();
        names = getAncestorsNames(deptId, names);
        if (CollectionUtil.isEmpty(names)){
            return "";
        }
        Collections.reverse(names);
        return StrUtil.join("-", names);
    }
    /**
     * 递归列表
     */
@@ -362,4 +377,26 @@
    {
        return getChildList(list, t).size() > 0;
    }
    /**
     * 获取祖籍names列表
     *
     * @param deptId 当前部门id
     * @param names  祖籍id列表
     * @return  names列表
     */
    public List<String> getAncestorsNames(long deptId, List<String> names) {
        SysDept dept = this.getById(deptId);
        if (dept == null) {
            return new ArrayList<>();
        }
        if (dept.getParentId() != null && dept.getParentId() == 100) {
            names.add(dept.getDeptName());
            return names;
        } else {
            names.add(dept.getDeptName());
            return getAncestorsNames(dept.getParentId(), names);
        }
    }
}