java 树




public class ThreeWyfwData implements Serializable {




/** 节点ID */
private String id;
private String pid;

/** 节点名称 */
private String label;

private String latitude;

/** 纬度 */
private String longitude;
/** 子节点 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<ThreeWyfwData> children;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getPid() {
return pid;
}

public void setPid(String pid) {
this.pid = pid;
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

public String getLatitude() {
return latitude;
}

public void setLatitude(String latitude) {
this.latitude = latitude;
}

public String getLongitude() {
return longitude;
}

public void setLongitude(String longitude) {
this.longitude = longitude;
}

public List<ThreeWyfwData> getChildren() {
return children;
}

public void setChildren(List<ThreeWyfwData> children) {
this.children = children;
}
public ThreeWyfwData() {}

public ThreeWyfwData(WyfwBuildingManage wyfwBuildingManage)
{
this.id = wyfwBuildingManage.getId();
this.pid = wyfwBuildingManage.getParentId();
this.label = wyfwBuildingManage.getBuildingName();
this.latitude = wyfwBuildingManage.getLat();
this.longitude = wyfwBuildingManage.getLon();
this.children = wyfwBuildingManage.getChildren().stream().map(ThreeWyfwData::new).collect(Collectors.toList());
}
}










@GetMapping("/getThreeWyList")
public AjaxResult getThreeWyList(@RequestParam("name") String name) {
WyfwOwnerManage wy =new WyfwOwnerManage();
wy.setBuildingName(name);
List<WyfwOwnerManage> list = wyfwOwnerManageService.selectWyfwOwnerManageList(wy);
List<WyfwBuildingManage> wyList = WyfwBuildingManageMapper.selectWyfwBuildingManageList(new WyfwBuildingManage());
return AjaxResult.success(wyfwOwnerManageService.getThreeWyList(wyList,list));
}







/**
* 获取公共机构树
* @return
*/
public List<ThreeWyfwData> getThreeWyList(List<WyfwBuildingManage> blist,List<WyfwOwnerManage> wlist) {

List<WyfwBuildingManage> returnList = new ArrayList<>();
HashSet<String> tempSet = new HashSet<>();
for (WyfwBuildingManage bu : blist) {
tempSet.add(bu.getId());
}
for (WyfwBuildingManage bu : blist) {
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempSet.contains(bu.getParentId())) {
recursionFn(blist, bu);
returnList.add(bu);
}
}
Map<String, List<WyfwOwnerManage>> devMapList = wlist.stream()
.collect(Collectors.groupingBy(WyfwOwnerManage::getBuildingId));
for (WyfwBuildingManage bu : blist) {
String tempId = String.valueOf(bu.getId());
if (devMapList.containsKey(tempId)) {
List<WyfwOwnerManage> tempList = devMapList.get(tempId);
if (!Objects.isNull(tempList) && tempList.size() > 0) {
List<WyfwBuildingManage> addList = new ArrayList<>();
tempList.forEach(item -> {
WyfwBuildingManage buy = new WyfwBuildingManage();
buy.setId(String.valueOf(item.getId()));
buy.setParentId(String.valueOf(item.getBuildingId()));
buy.setBuildingName(item.getCompanyName());
buy.setLat(item.getLatitude());
buy.setLon(item.getLongitude());
addList.add(buy);
});
if (Objects.isNull(bu.getChildren())) {
bu.setChildren(addList);
} else {
bu.getChildren().addAll(addList);
}
}
}
}

return returnList.stream().map(ThreeWyfwData::new).collect(Collectors.toList());
}


/**
* 递归列表
*/
private void recursionFn(List<WyfwBuildingManage> list, WyfwBuildingManage t) {
// 得到子节点列表
List<WyfwBuildingManage> childList = getChildList(list, t);
t.setChildren(childList);
for (WyfwBuildingManage tChild : childList) {
if (hasChild(list, tChild)) {
recursionFn(list, tChild);
}
}
}

/**
* 得到子节点列表
*/
private List<WyfwBuildingManage> getChildList(List<WyfwBuildingManage> list, WyfwBuildingManage t) {
List<WyfwBuildingManage> tlist = new ArrayList<WyfwBuildingManage>();
for (WyfwBuildingManage n : list) {
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getId())) {
tlist.add(n);
}
}
return tlist;
}
/**
* 判断是否有子节点
*/
private boolean hasChild(List<WyfwBuildingManage> list, WyfwBuildingManage t) {
return getChildList(list, t).size() > 0;
}
posted @ 2022-12-03 14:21  无情风中  阅读(46)  评论(0编辑  收藏  举报