package com.nnmzkj.common.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
public class TreeParamDto implements Serializable {
private static final long serialVersionUID = -7692266106281926159L;
private Long id;
private String name;
private Long parentId;
private Long menuId; //冗余字段
private Integer isParent;
private List<TreeParamDto> children ;
}
/////////////////
package com.nnmzkj.common.utils;
import com.nnmzkj.common.dto.BaseTreeGrid;
import com.nnmzkj.common.dto.TreeParamDto;
import com.nnmzkj.sys.model.SysMenuEntity;
import com.nnmzkj.sys.model.SysOrgEntity;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class TreeUtils {
/**
* 真*树形结构
* @param myId
* @param list
* @param <T>
* @return
*/
public static <T extends TreeParamDto> List<T> getTreeArray(Long myId,List<T> list){
List<T> result = new ArrayList<T>();
List<T> childList = getChild(myId, list);
if (childList.size() >0){
for (T t : childList) {
List<T> treeArray = getTreeArray(t.getId(), list);
if(t.getChildren() == null) {
t.setChildren(new ArrayList<TreeParamDto>());
}
for (T t1 : treeArray) {
t.getChildren().add(t1);
}
result.add(t);
}
}
return result;
}
public static <T extends TreeParamDto> List<T> selectTree(Long myId,List<T> list,List<T> result,String str){
List<T> childList = getChild(myId, list);
if (childList.size() >0){
if (myId >0){
str = "|----" + str;
}
for (T t : childList) {
t.setName(str+t.getName());
result.add(t);
result = selectTree(t.getId(), list,result,str);
}
return result;
}
return result;
}
/**
* 获取子集
* @param list
* @param <T>
* @return
*/
public static <T extends TreeParamDto> List<T> getChild(Long myId,List<T> list){
List<T> result = new ArrayList<T>();
if (!StringUtil.isNullOrEmpty(list) && list.size() >0){
for (T t : list) {
if (t.getParentId().equals(myId)){
result.add(t);
}
}
}
return result;
}
public static List<SysOrgEntity> selectOrg(List<SysOrgEntity> list){
list = getTreeList(list);
for (SysOrgEntity sysOrg : list) {
String str ="";
Byte level = sysOrg.getLevel();
if (level != 0){
for (int i=0 ;i<sysOrg.getLevel();i++){
str = str + "|----";
}
}
sysOrg.setName(str+sysOrg.getName());
}
return list;
}
/**
* 使列表树结构排序
* @param allList
* @return
*/
public static List<SysOrgEntity> getTreeList(List<SysOrgEntity> allList){
List<SysOrgEntity> rootList = getTreeRootList(0,allList);
List<SysOrgEntity> list = new ArrayList<SysOrgEntity>();
for (SysOrgEntity entity : rootList) {
list.addAll(getChildList(entity,allList));
}
return list ;
}
/**
* 获取所有子机构
* @param parent 父机构
* @param allList 所有列表
* @return
*/
public static List<SysOrgEntity> getChildList(SysOrgEntity parent,List<SysOrgEntity> allList){
//存放子机构的集合
ArrayList<SysOrgEntity> listChild = new ArrayList<SysOrgEntity>();
listChild.add(parent);
for (SysOrgEntity entity : allList) {
if(entity.getParentId().equals(parent.getOrgId())) {
listChild.addAll(getChildList(entity, allList)) ;
}
}
return listChild ;
}
/**
* 获取根机构集合
* @param rootLevel 根机构等级
* @param allList
* @return
*/
public static List<SysOrgEntity> getTreeRootList(int rootLevel,List<SysOrgEntity> allList){
if(rootLevel>=5){
return null;
}
List<SysOrgEntity> rootList = new ArrayList<SysOrgEntity>();
for (SysOrgEntity entity : allList) {
if(entity.getLevel()==rootLevel){
rootList.add(entity);
}
}
if(rootList.size()>0){
return rootList ;
}else{
rootList = getTreeRootList(rootLevel+1,allList);
}
return rootList;
}
}