1.Controller:
/*** 区域树生成
*/
@GetMapping("/list")
private Result regionTree() {
Station station = getStation(getUser());
String stationCode = station.getStationTelecode();
List<NewTreeVo> tree = stationInfRelaService.regionTree(stationCode);
return Result.ok(tree);
}
2.Service:
List<NewTreeVo> regionTree(String stationCode);
3.Impl:
@Overridepublic List<NewTreeVo> regionTree(String stationCode) {
List<StationInfRelaTree> allList = stationInfRelaTreeDao.findByStationCode(stationCode);
List<StationInfRelaTree> rootList = allList.stream().filter(s -> s.getParent() == null).collect(Collectors.toList());
Integer rootType = 1;
return rootList.stream()
//转为vo对象
.map(root -> generateNewTreeVo(root, rootType))
//获取子节点
.peek(root -> {
String relaTreeId = root.getUserdata().getRelaTreeId();
int curRootType = Integer.parseInt(root.getUserdata().getType());
root.setItems(getItems(allList, relaTreeId, curRootType));
}).collect(Collectors.toList());
}
/**
* 获取子节点
* @param allList 所有数据集
* @param parentId 父节点
* @param rootType 父节点类型
* @return
*/
private static Set<NewTreeVo> getItems(List<StationInfRelaTree> allList, String parentId, int rootType) {
return allList.stream()
.filter(node -> node.getParent() != null && node.getParent().getRelaTreeId().equals(parentId))
.map(node -> generateNewTreeVo(node, rootType + 1))
.peek(node -> {
String relaTreeId = node.getUserdata().getRelaTreeId();
int curRootType = Integer.parseInt(node.getUserdata().getType());
node.setItems(getItems(allList, relaTreeId, curRootType));
})
.collect(Collectors.toSet());
}
/**
* StationInfRelaTree 转为 NewTreeVo
* @param infRelaTree
* @param type 节点类型
* @return
*/
private static NewTreeVo generateNewTreeVo(StationInfRelaTree infRelaTree, Integer type) {
return NewTreeVo.builder()
.id(infRelaTree.getId())
.text(infRelaTree.getInfName())
.userdata(generateUserData(infRelaTree, type)).build();
}
/**
* 生成NewTreeVo.UserData
* @see NewTreeVo.UserData
* @param infRelaTree
* @param type
* @return
*/
private static NewTreeVo.UserData generateUserData(StationInfRelaTree infRelaTree, Integer type) {
NewTreeVo.UserData userData = new NewTreeVo.UserData();
//属性拷贝
BeanUtils.copyProperties(infRelaTree, userData);
//其他属性赋值
StationInfRelaTree parent = infRelaTree.getParent();
if (parent != null) {
userData.setParent_id(parent.getRelaTreeId());
}else{
userData.setParent_id("1");
}
userData.setType(type.toString());
return userData;
}
4:Dao:
@Query("select t from StationInfRelaTree t where t.stationCode = :stationCode")List<StationInfRelaTree> findByStationCode(@Param("stationCode") String stationCode);
标签:infRelaTree,node,13,Java,笔记,param,stationCode,return,NewTreeVo From: https://www.cnblogs.com/sensenh/p/17325588.html