自动化立体仓库 - WMS系统
whycq
2022-08-06 7d7090b098add475808ffdebebff8b4e2a3b98ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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<Map> getTree(String id){
        ArrayList<Map> result = new ArrayList<>();
        Tag tag = tagService.selectById(id);
        // 主节点
        Map<String, Object> map = new HashMap<>();
        map.put("title", tag.getName());
        map.put("id", tag.getId());
        map.put("spread", true);
        List<Map> childrens = new ArrayList<>();
        map.put("children", childrens);
        dealTag(tag, childrens);
        result.add(map);
        // 开始处理字节点
//        deal(tag, childrens);
        return result;
    }
 
    /**
     * 递归获取子节点数据
     */
    public void dealTag(Tag parent, List<Map> list) {
        List<Tag> tags = tagService.selectList(
                new EntityWrapper<Tag>()
                        .eq("parent_id", parent.getId())
                        .eq("status", "1"));
        for (Tag tag : tags) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", tag.getName());
            map.put("id", tag.getId());
            map.put("spread", true);
            List<Map> childrens = new ArrayList<>();
            map.put("children", childrens);
            dealTag(tag, childrens);
            list.add(map);
        }
    }
 
 
    // -------------------------------------------------------------------------------------------------------
 
    /**
     * 条件筛选
     */
    @SuppressWarnings("unchecked")
    public void remove(String condition, List<Map> list) {
        Iterator<Map> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map map = iterator.next();
            if (map.get("children") != null) {
                List<Map> children = (List<Map>) 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<Map> getNodeTree(String id, Long hostId){
        ArrayList<Map> result = new ArrayList<>();
        Node node = nodeService.selectById(id);
        // 主节点
        Map<String, Object> map = new HashMap<>();
        map.put("title", node.getName());
        map.put("id", node.getId());
        map.put("spread", true);
        List<Map> 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<Map> list, Long hostId) {
        Wrapper<Node> wrapper = new EntityWrapper<Node>()
                .eq("parent_id", parent.getId())
                .eq("status", "1");
        if (hostId != null) {
            wrapper.eq("host_id", hostId);
        }
        List<Node> nodes = nodeService.selectList(wrapper);
        for (Node node : nodes) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", node.getName());
            map.put("id", node.getId());
            map.put("spread", true);
            List<Map> childrens = new ArrayList<>();
            map.put("children", childrens);
            dealNode(node, childrens, hostId);
            list.add(map);
        }
    }
 
 
    // -------------------------------------------------------------------------------------------------------
 
}