package com.sunwayland.websocket.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sunwayland.websocket.dao.TbPointDao;
import com.sunwayland.websocket.entity.TbPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class PsaceServiceImpl {
@Autowired
private ObjectMapper objectMapper;
@Autowired
private TbPointDao tbPointDao;
public String queryAllPoints() throws JsonProcessingException {
long startTime = System.currentTimeMillis();
TbPoint rootPoint = new TbPoint();
rootPoint.setId(0);
//查询根节点
List<TbPoint> roots = tbPointDao.queryAllPointByParentId(rootPoint);
TbPoint rootNodePoint = roots.get(0);
// 第一级节点的集合
List<TbPoint> firstLevelNodePointList = tbPointDao.queryAllPointByParentId(rootNodePoint);
if(firstLevelNodePointList.size() > 0){
iterator(firstLevelNodePointList);
rootNodePoint.setTbPointList(firstLevelNodePointList);
}
long endTime = System.currentTimeMillis();
long spendTime = (endTime - startTime)/ 1000;
String result = "查询所有节点:共耗时=" + spendTime + " S";
log.info(result);
List<TbPoint> tbPointList = new ArrayList<>();
tbPointList.add(rootNodePoint);
return objectMapper.writeValueAsString(tbPointList);
}
public void iterator(List<TbPoint> nodePointList){
for(int i = 0; i < nodePointList.size(); i++){
TbPoint rootNodePoint = nodePointList.get(i);
// byte pointType = 2;
// rootNodePoint.setPointType(pointType);
Integer childCount = tbPointDao.findCount(rootNodePoint);
if(childCount != null){
List<TbPoint> childNodePointList = tbPointDao.queryAllPointByParentIdAndPointType(rootNodePoint);
Boolean isHasNode = false;
//如果子节点数大于 0,且子节点中有类型为Node的节点,则需要对子节点集合进行迭代,否则停止
if(childNodePointList.size() > 0){
for(int j = 0; j < childNodePointList.size(); j++){
TbPoint childPoint = childNodePointList.get(j);
if(childPoint.getPointType() == 0){
isHasNode = true;
break;
}
}
}
if(isHasNode){
iterator(childNodePointList);
}
nodePointList.get(i).setTbPointList(childNodePointList);
}
}
}
}
打开App,阅读手记