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;
public class 树形结构 {
@Resource
private IMonitoringNodeService monitoringNodeService;
@ApiOperation("在线监测节点树")
@GetMapping("")
public JSONObject list() throws Exception {
PageRequest pageRequest = new PageRequest();
pageRequest.addAndCondition(MonitoringNode.IS_DELETED, SearchCondition.EQUAL, 0);
pageRequest.addSortConditions(MonitoringNode.SORT, PageRequest.ORDER_ASC);
List<MonitoringNode> nodeAll = monitoringNodeService.findAll(pageRequest);
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;
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<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<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<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;
}
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<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;
}
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();
ComboTreeObject treeObj = new ComboTreeObject();
treeObj.setId(module.getId());
treeObj.setText(module.getName());
treeObj.setParentId(module.getSuperMod());
listTree.add(treeObj);
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<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;
}
}