package com.zy.common.utils; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.zy.asrs.entity.Node; import com.zy.asrs.entity.Tag; import com.zy.asrs.service.NodeService; import com.zy.asrs.service.TagService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; import java.util.*; /** * 树形图工具 * Created by vincent on 2020/10/16 */ @Component public class TreeUtils { @Autowired private TagService tagService; @Autowired private NodeService nodeService; /******************************** 归类树 *********************************/ /** * 获取树图数据结构 */ @Cacheable(cacheNames="tagTree",key="#id") public ArrayList getTree(String id){ ArrayList result = new ArrayList<>(); Tag tag = tagService.selectById(id); // 主节点 Map map = new HashMap<>(); map.put("title", tag.getName()); map.put("id", tag.getId()); map.put("spread", true); List childrens = new ArrayList<>(); map.put("children", childrens); dealTag(tag, childrens); result.add(map); // 开始处理字节点 // deal(tag, childrens); return result; } /** * 递归获取子节点数据 */ public void dealTag(Tag parent, List list) { List tags = tagService.selectList( new EntityWrapper() .eq("parent_id", parent.getId()) .eq("status", "1")); for (Tag tag : tags) { Map map = new HashMap<>(); map.put("title", tag.getName()); map.put("id", tag.getId()); map.put("spread", true); List childrens = new ArrayList<>(); map.put("children", childrens); dealTag(tag, childrens); list.add(map); } } // ------------------------------------------------------------------------------------------------------- /** * 条件筛选 */ @SuppressWarnings("unchecked") public void remove(String condition, List list) { Iterator iterator = list.iterator(); while (iterator.hasNext()) { Map map = iterator.next(); if (map.get("children") != null) { List children = (List) map.get("children"); if (children.size() > 0) { remove(condition, children); } else { if (!String.valueOf(map.get("title")).contains(condition)) { iterator.remove(); } } } } } /******************************** 节点树 *********************************/ /** * 获取树图数据结构 */ @Cacheable(cacheNames="nodeTree",key="#id") public ArrayList getNodeTree(String id, Long hostId){ ArrayList result = new ArrayList<>(); Node node = nodeService.selectById(id); // 主节点 Map map = new HashMap<>(); map.put("title", node.getName()); map.put("id", node.getId()); map.put("spread", true); List childrens = new ArrayList<>(); map.put("children", childrens); dealNode(node, childrens, hostId); result.add(map); // 开始处理字节点 // deal(tag, childrens); return result; } /** * 递归获取子节点数据 */ public void dealNode(Node parent, List list, Long hostId) { Wrapper wrapper = new EntityWrapper() .eq("parent_id", parent.getId()) .eq("status", "1"); if (hostId != null) { wrapper.eq("host_id", hostId); } List nodes = nodeService.selectList(wrapper); for (Node node : nodes) { Map map = new HashMap<>(); map.put("title", node.getName()); map.put("id", node.getId()); map.put("spread", true); List childrens = new ArrayList<>(); map.put("children", childrens); dealNode(node, childrens, hostId); list.add(map); } } // ------------------------------------------------------------------------------------------------------- }