package com.gkhy.safePlatform.account.utils;
|
|
import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfo;
|
import com.gkhy.safePlatform.account.enums.DepartmentLevelEnum;
|
import com.gkhy.safePlatform.account.enums.DepartmentStatusEnum;
|
import com.gkhy.safePlatform.account.model.dto.resp.DepartmentRespDTO;
|
import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfoDO;
|
import org.springframework.beans.BeanUtils;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Objects;
|
|
public class DepartmentUtil {
|
|
|
/**
|
* @Description: 生成树
|
*/
|
public static List<DepartmentRespDTO> generateMenuTree(List<DepartmentInfoDO> allDepartment) {
|
|
// 区分父子节点
|
List<DepartmentRespDTO> rootDepartment = new ArrayList<>();
|
List<DepartmentRespDTO> childDepartment = new ArrayList<>();
|
DepartmentRespDTO tmp;
|
DepartmentStatusEnum statusEnum;
|
DepartmentLevelEnum levelEnum;
|
for (DepartmentInfoDO departmentDo : allDepartment) {
|
// 转换dto
|
tmp = new DepartmentRespDTO();
|
tmp.setDepId(departmentDo.getId());
|
tmp.setDepName(departmentDo.getName());
|
tmp.setDepInfo(departmentDo.getInfo());
|
tmp.setDepLevel(departmentDo.getLevel());
|
tmp.setDepCode(departmentDo.getCode());
|
levelEnum = DepartmentLevelEnum.parse(departmentDo.getLevel());
|
if (levelEnum != null) {
|
tmp.setDepLevelDesc(levelEnum.value);
|
}
|
tmp.setStatus(departmentDo.getStatus());
|
statusEnum = DepartmentStatusEnum.parse(departmentDo.getStatus());
|
assert statusEnum != null;
|
tmp.setStatusDesc(statusEnum.getValue());
|
tmp.setParentDepId(departmentDo.getParentId());
|
if (departmentDo.getParentId() == null) {
|
rootDepartment.add(tmp);
|
}else{
|
childDepartment.add(tmp);
|
}
|
}
|
// 遍历父节点
|
if (rootDepartment.size() > 0) {
|
for (DepartmentRespDTO root : rootDepartment) {
|
// 当前父节点的子节点
|
List<DepartmentRespDTO> children = getChildren(root, childDepartment);
|
root.setChildren(children);
|
}
|
}
|
|
return rootDepartment;
|
}
|
|
private static List<DepartmentRespDTO> getChildren(DepartmentRespDTO parent, List<DepartmentRespDTO> childDepartment) {
|
List<DepartmentRespDTO> children = new ArrayList<>();
|
if (childDepartment.size() > 0) {
|
for (DepartmentRespDTO nav : childDepartment) {
|
if (nav.getParentDepId().equals(parent.getDepId())) {
|
children.add(nav);
|
}
|
}
|
}
|
if (children.size() > 0) {
|
for (DepartmentRespDTO nav:children) {
|
List<DepartmentRespDTO> subChildren = getChildren(nav, childDepartment);
|
nav.setChildren(subChildren);
|
}
|
}
|
return children;
|
}
|
|
|
public static DepartmentInfoDO getDepartment(Long depId, List<DepartmentInfoDO> allDeps) {
|
if (depId == null) return null;
|
for (DepartmentInfoDO departmentInfoDO : allDeps) {
|
if (departmentInfoDO.getId().equals(depId)) {
|
return departmentInfoDO;
|
}
|
}
|
return null;
|
|
}
|
|
public static boolean isUnderDepartment(Long depId,Long depId1, List<DepartmentInfoDO> allDeps) {
|
DepartmentInfoDO department = getDepartment(depId1, allDeps);
|
if (department.getParentId() != null) {
|
if (department.getParentId().equals(depId)) {
|
return true;
|
}else{
|
return isUnderDepartment(depId, department.getParentId(), allDeps);
|
}
|
}else{
|
return false;
|
}
|
}
|
|
|
public static List<DepartmentInfoDO> generateDepListByDepId(Long depId, List<DepartmentInfoDO> allDeps) {
|
List<DepartmentInfoDO> result = new ArrayList<>();
|
for (DepartmentInfoDO departmentInfoDO : allDeps) {
|
if (departmentInfoDO.getId().equals(depId)) {
|
result.add(departmentInfoDO);
|
}
|
}
|
if (result.size() == 1) {
|
// 递归寻找子部门
|
addChildren(result.get(0), result, allDeps);
|
|
}
|
return result;
|
}
|
|
private static void addChildren(DepartmentInfoDO root, List<DepartmentInfoDO> result, List<DepartmentInfoDO> allDeps) {
|
List<DepartmentInfoDO> children = new ArrayList<>();
|
for (DepartmentInfoDO departmentInfo : allDeps) {
|
if (root.getId().equals(departmentInfo.getParentId())) {
|
children.add(departmentInfo);
|
result.add(departmentInfo);
|
}
|
}
|
if (children.size() > 0) {
|
for (DepartmentInfoDO child : children) {
|
addChildren(child, result, allDeps);
|
}
|
}
|
}
|
|
|
public static void main(String[] args) {
|
|
}
|
|
public static List<Long> generateDepIdListByDepId(Long depId, List<DepartmentInfoDO> allDeps) {
|
List<Long> result = new ArrayList<>();
|
for (DepartmentInfoDO departmentInfoDO : allDeps) {
|
if (departmentInfoDO.getId().equals(depId)) {
|
result.add(departmentInfoDO.getId());
|
}
|
}
|
if (result.size() == 1) {
|
// 递归寻找子部门
|
addChildrenId(result.get(0), result, allDeps);
|
|
}
|
return result;
|
}
|
|
|
private static void addChildrenId(Long rootId, List<Long> result, List<DepartmentInfoDO> allDeps) {
|
List<DepartmentInfoDO> children = new ArrayList<>();
|
for (DepartmentInfoDO departmentInfo : allDeps) {
|
if (rootId.equals(departmentInfo.getParentId())) {
|
children.add(departmentInfo);
|
result.add(departmentInfo.getId());
|
}
|
}
|
if (children.size() > 0) {
|
for (DepartmentInfoDO child : children) {
|
addChildrenId(child.getId(), result, allDeps);
|
}
|
}
|
}
|
}
|