java 树形结构

package com.hndfsj.test;

import com.alibaba.fastjson.JSONObject;
import com.hndfsj.app.monitoring.domain.MonitoringNode;
import com.hndfsj.app.monitoring.service.IMonitoringNodeService;
import com.hndfsj.framework.common.MReturnObject;
import com.hndfsj.framework.objects.EasyUI;
import com.hndfsj.framework.objects.TreeObject;
import com.hndfsj.framework.pager.PageModel;
import com.hndfsj.framework.pager.PageRequest;
import com.hndfsj.framework.pager.SearchCondition;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * <pre>
 * TODO:树形结构后端代码
 * </pre>
 *
 * @author zhangjunchao
 * @date 2019/7/4
 */
public class 树形结构 {

    @Resource
    private IMonitoringNodeService monitoringNodeService;


    // 树形结构显示
    @ApiOperation("在线监测节点树")
    @GetMapping("")
    public JSONObject list() throws Exception {
        PageRequest pageRequest = new PageRequest();
        // 1删除 0没有删除  查询All没有删除的节点
        pageRequest.addAndCondition(MonitoringNode.IS_DELETED, SearchCondition.EQUAL, 0);
        // 排序
        pageRequest.addSortConditions(MonitoringNode.SORT, PageRequest.ORDER_ASC);
        // 根据条件查询出所有的节点
        List<MonitoringNode> nodeAll = monitoringNodeService.findAll(pageRequest);
        // 调方法封装成树形结构返回      id:     parentId:父级id
        List<TreeObject> treeObjects = EasyUI.genTableTree(nodeAll, "id", "parentId");

        return PageModel.getInstance().includeListUnPage(new MReturnObject(MReturnObject.SUCCESS, treeObjects));
    }



}


方法类

package com.hndfsj.framework.objects;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hndfsj.admin.domain.Module;
import com.hndfsj.admin.domain.Resource;
import com.hndfsj.framework.utils.ReflectionUtils;

/**
 * EasyUI对应类
 *
 * @author ibm
 * @date   2010-7-13
 */
public class EasyUI {

	public static List<TreeObject> genTableTree(Collection<?> list,String idName,String parentIdName){ 
		//==初始化对象列表
		List<TreeObject> listTree = new ArrayList<TreeObject>(); 
		for (Iterator<?> iterator = list.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject) iterator.next();
			listTree.add(object);
		}
		
		//==对象列表放入Map
		Map<String,TreeObject> objectMap = new HashMap<String,TreeObject>();
		for (Iterator<?> iterator = listTree.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject)iterator.next();
			String id = (String)ReflectionUtils.getFieldValue(object,idName);
			objectMap.put(id, object);
		}
		
		//==生成目标树对象
		List<TreeObject> returnList = new ArrayList<TreeObject>();
		for (Iterator<?> iterator = listTree.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject)iterator.next();
			String parentId=(String)ReflectionUtils.getFieldValue(object,parentIdName);
			TreeObject pObject = objectMap.get(parentId);
			if (pObject != null) {
				pObject.addChild(object);
			} else {
				returnList.add(object);
			}
		}
		
		return returnList;		
	}
	public static List<TreeObject> genDeptTree(String deptId,Collection<?> list,String idName,String parentIdName){ 
		//==初始化对象列表
		List<TreeObject> listTree = new ArrayList<TreeObject>(); 
		for (Iterator<?> iterator = list.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject) iterator.next();
			listTree.add(object);
		}
		
		//==对象列表放入Map
		Map<String,TreeObject> objectMap = new HashMap<String,TreeObject>();
		for (Iterator<?> iterator = listTree.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject)iterator.next();
			String id = (String)ReflectionUtils.getFieldValue(object,idName);
			objectMap.put(id, object);
		}
		
		Map<String,TreeObject> newObjectMap = new HashMap<String,TreeObject>();
		//==生成目标树对象
		List<TreeObject> returnList = new ArrayList<TreeObject>();
		for (Iterator<?> iterator = listTree.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject)iterator.next();
			String parentId=(String)ReflectionUtils.getFieldValue(object,parentIdName);
			TreeObject pObject = objectMap.get(parentId);
			newObjectMap.put((String) ReflectionUtils.getFieldValue(object,"id"), object);
			if (pObject != null) {
				pObject.addChild(object);
			} else {
				returnList.add(object);
			}
		}
		TreeObject dept=newObjectMap.get(deptId);
		if(dept!=null){
			List<TreeObject> newlist = new ArrayList<TreeObject>();
			newlist.add(dept);
			return newlist;
		}
		return returnList;		
	}
	public static Map<String,TreeObject> genDeptMapTree( Collection<?> list,String idName,String parentIdName){ 
		//==初始化对象列表
		List<TreeObject> listTree = new ArrayList<TreeObject>(); 
		for (Iterator<?> iterator = list.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject) iterator.next();
			listTree.add(object);
		}
		
		//==对象列表放入Map
		Map<String,TreeObject> objectMap = new HashMap<String,TreeObject>();
		for (Iterator<?> iterator = listTree.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject)iterator.next();
			String id = (String)ReflectionUtils.getFieldValue(object,idName);
			objectMap.put(id, object);
		}
		
		Map<String,TreeObject> newObjectMap = new HashMap<String,TreeObject>();
		//==生成目标树对象
		List<TreeObject> returnList = new ArrayList<TreeObject>();
		for (Iterator<?> iterator = listTree.iterator(); iterator.hasNext();) {
			TreeObject object = (TreeObject)iterator.next();
			String parentId=(String)ReflectionUtils.getFieldValue(object,parentIdName);
			TreeObject pObject = objectMap.get(parentId);
			newObjectMap.put((String) ReflectionUtils.getFieldValue(object,"id"), object);
			if (pObject != null) {
				pObject.addChild(object);
			} else {
				returnList.add(object);
			}
		}
		return newObjectMap;		
	}
	
	public static int count(TreeObject dept){
		int count=(Integer)ReflectionUtils.getFieldValue(dept,"count");	
		if(dept.getChildren()!=null){
			List<Object> list=dept.getChildren();
			for(Object obj:list){
				TreeObject to=(TreeObject) obj;
				count+=count(to);
			}
		}
		ReflectionUtils.setFieldValue(dept,"count",count);	
		ReflectionUtils.setFieldValue(dept,"name",ReflectionUtils.getFieldValue(dept,"name")+"("+count+"/"+ReflectionUtils.getFieldValue(dept,"villageCount")+")");	
		return count;
	}
	public static int get(TreeObject dept){
		int count=(Integer)ReflectionUtils.getFieldValue(dept,"count");	
		if(dept.getChildren()!=null){
			List<Object> list=dept.getChildren();
			for(Object obj:list){
				TreeObject to=(TreeObject) obj;
				count+=count(to);
			}
		}
		ReflectionUtils.setFieldValue(dept,"count",count);	
		ReflectionUtils.setFieldValue(dept,"name",ReflectionUtils.getFieldValue(dept,"name")+"("+count+"/"+ReflectionUtils.getFieldValue(dept,"villageCount")+")");	
		return count;
	}

	/**
	 * 将对象列表转换为Tree需要的列表对象
	 * 
	 * @param list
	 * @param fieldId
	 * @param fieldName
	 * @param fieldParent
	 * @return
	 */
	public static List<ComboTreeObject> genComboTree(List<?> list,String fieldId,String fieldName,String fieldParent){ 
		//==初始化对象列表
		List<ComboTreeObject> listTree = new ArrayList<ComboTreeObject>(); 
		for (Iterator<?> iterator = list.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			ComboTreeObject treeObj = new ComboTreeObject();
			treeObj.setId((String)ReflectionUtils.getFieldValue(object,fieldId));
			treeObj.setText((String)ReflectionUtils.getFieldValue(object,fieldName));
			treeObj.setParentId((String)ReflectionUtils.getFieldValue(object,fieldParent));
			listTree.add(treeObj);
		}
		
		//==对象列表放入Map
		Map<String,ComboTreeObject> objectMap = new HashMap<String,ComboTreeObject>();
		for (Iterator<ComboTreeObject> iterator = listTree.iterator(); iterator.hasNext();) {
			ComboTreeObject object = iterator.next();
			objectMap.put(object.getId(), object);
		}
		
		//==生成目标树对象
		List<ComboTreeObject> returnList = new ArrayList<ComboTreeObject>();
		for (Iterator<ComboTreeObject> iterator = listTree.iterator(); iterator.hasNext();) {
			ComboTreeObject object = iterator.next();
			ComboTreeObject pObject = objectMap.get(object.getParentId());
			if (pObject != null) {
				pObject.addChild(object);
			} else {
				returnList.add(object);
			}
		}
		
		return returnList;
	}
	

	/**
	 * 生成分配模块资源的JSON树
	 * 
	 * @param listAll
	 * @param listAssign
	 * @return
	 */
	public static List<ComboTreeObject> genModuleResourceTree(List<Module> listAll,List<Resource> listAssign){ 
		//==初始化对象列表
		List<ComboTreeObject> listTree = new ArrayList<ComboTreeObject>(); 
		for (Iterator<Module> iterModule = listAll.iterator(); iterModule.hasNext();) {
			Module module = iterModule.next();
			//==创建Module对象
			ComboTreeObject treeObj = new ComboTreeObject();
			treeObj.setId(module.getId());
			treeObj.setText(module.getName());
			treeObj.setParentId(module.getSuperMod());
			listTree.add(treeObj);
			//==创建Resource对象
			if ("1".equals(module.getIsLeaf())) {//叶子节点
				for (Iterator<Resource> iterRes = module.getResources().iterator(); iterRes.hasNext();) {
					Resource res = iterRes.next();
					treeObj = new ComboTreeObject();
					treeObj.setId(res.getId());
					treeObj.setText(res.getName());
					treeObj.setParentId(module.getId());
					if (listAssign.contains(res)) {
                        treeObj.setChecked(true);
                    } else {
                        treeObj.setChecked(false);
                    }
					listTree.add(treeObj);
				}
			}
		}
		
		//==对象列表放入Map
		Map<String,ComboTreeObject> objectMap = new HashMap<String,ComboTreeObject>();
		for (Iterator<ComboTreeObject> iterator = listTree.iterator(); iterator.hasNext();) {
			ComboTreeObject object = iterator.next();
			objectMap.put(object.getId(), object);
		}
		
		//==生成目标树对象
		List<ComboTreeObject> returnList = new ArrayList<ComboTreeObject>();
		for (Iterator<ComboTreeObject> iterator = listTree.iterator(); iterator.hasNext();) {
			ComboTreeObject object = iterator.next();
			ComboTreeObject pObject = objectMap.get(object.getParentId());
			if (pObject != null) {
				pObject.addChild(object);
			} else {
				returnList.add(object);
			}
		}
		
		return returnList;
	}	
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值