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); } } } }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦