中扬CRM客户关系管理系统
#
luxiaotao1123
2022-11-24 e117fad8350939e70c8448ab069c92fa21145c24
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.zy.crm.common.utils;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.zy.crm.manager.entity.Node;
import com.zy.crm.manager.entity.Tag;
import com.zy.crm.manager.service.NodeService;
import com.zy.crm.manager.service.TagService;
import com.zy.crm.system.entity.Dept;
import com.zy.crm.system.entity.Role;
import com.zy.crm.system.entity.User;
import com.zy.crm.system.service.DeptService;
import com.zy.crm.system.service.RoleService;
import com.zy.crm.system.service.UserService;
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;
    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
 
    /******************************** 归类树 *********************************/
 
    /**
     * 获取树图数据结构
     */
    @Cacheable(cacheNames="tagTree",key="#id")
    public ArrayList<Map<String, Object>> getTree(String id, Long hostId){
        ArrayList<Map<String, Object>> 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<String, Object>> childrens = new ArrayList<>();
        map.put("children", childrens);
        dealTag(tag, childrens, hostId);
        result.add(map);
        // 开始处理字节点
//        deal(tag, childrens);
        return result;
    }
 
    /**
     * 递归获取子节点数据
     */
    public void dealTag(Tag parent, List<Map<String, Object>> list, Long hostId) {
        Wrapper<Tag> wrapper = new EntityWrapper<Tag>()
                .eq("parent_id", parent.getId())
                .eq("status", "1");
        if (hostId != null) {
            wrapper.eq("host_id", hostId);
        }
        List<Tag> tags = tagService.selectList(wrapper);
        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<String, Object>> childrens = new ArrayList<>();
            map.put("children", childrens);
            dealTag(tag, childrens, hostId);
            list.add(map);
        }
    }
 
    /******************************** 节点树 *********************************/
 
    /**
     * 获取树图数据结构
     */
    @Cacheable(cacheNames="nodeTree",key="#id")
    public ArrayList<Map<String, Object>> getNodeTree(String id, Long hostId){
        ArrayList<Map<String, Object>> 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<String, Object>> 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<String, Object>> 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<String, Object>> childrens = new ArrayList<>();
            map.put("children", childrens);
            dealNode(node, childrens, hostId);
            list.add(map);
        }
    }
 
    /******************************** 部门树 *********************************/
 
    /**
     * 获取树图数据结构
     */
    @Cacheable(cacheNames="deptTree",key="#id")
    public ArrayList<Map<String, Object>> getDeptTree(String id, Long hostId){
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        Dept dept = deptService.selectById(id);
        // 主节点
        Map<String, Object> map = new HashMap<>();
        map.put("title", dept.getName());
        map.put("id", dept.getId());
        map.put("spread", true);
        List<Map<String, Object>> childrens = new ArrayList<>();
        map.put("children", childrens);
        dealDept(dept, childrens, hostId);
        result.add(map);
        // 开始处理字节点
//        deal(tag, childrens);
        return result;
    }
 
    /**
     * 递归获取子节点数据
     */
    public void dealDept(Dept parent, List<Map<String, Object>> list, Long hostId) {
        Wrapper<Dept> wrapper = new EntityWrapper<Dept>()
                .eq("parent_id", parent.getId())
                .eq("status", 1);
        if (hostId != null) {
            wrapper.eq("host_id", hostId);
        }
        List<Dept> depts = deptService.selectList(wrapper);
        for (Dept dept : depts) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", dept.getName());
            map.put("id", dept.getId());
            map.put("spread", true);
            List<Map<String, Object>> childrens = new ArrayList<>();
            map.put("children", childrens);
            dealDept(dept, childrens, hostId);
            list.add(map);
        }
    }
 
    /******************************** 部门 + 员工 树 *********************************/
 
    /**
     * 获取树图数据结构
     */
    @Cacheable(cacheNames="deptUserTree",key="#id")
    public ArrayList<Map<String, Object>> getDeptUserTree(String id, Long hostId, Long userId){
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        User user = userService.selectById(userId);
        Role role = roleService.selectById(user.getRoleId());
        if (role.getCode().equals("salesman")) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", user.getNickname());
            map.put("id", user.getId());
            map.put("key", "user_id");
            map.put("icon", "layui-icon layui-icon-friends");
            result.add(map);
        } else {
            Dept dept = deptService.selectById(id);
            // 主节点
            Map<String, Object> map = new HashMap<>();
            map.put("title", dept.getName());
            map.put("id", dept.getId());
            map.put("spread", true);
            map.put("key", "dept_id");
            List<Map<String, Object>> childrens = new ArrayList<>();
            map.put("children", childrens);
            dealUserDept(dept, childrens, hostId, userId);
            result.add(map);
        }
        return result;
    }
 
    public void dealUserDept(Dept parent, List<Map<String, Object>> list, Long hostId, Long userId) {
        // 人员
        List<User> users = roleService.getUserByRoleCode(hostId, parent.getId(), userId);
        for (User user : users) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", user.getNickname());
            map.put("id", user.getId());
            map.put("key", "user_id");
            map.put("icon", "layui-icon layui-icon-friends");
            list.add(map);
        }
        // 部门
        Wrapper<Dept> wrapper = new EntityWrapper<Dept>()
                .eq("parent_id", parent.getId())
                .eq("status", 1);
        if (hostId != null) {
            wrapper.eq("host_id", hostId);
        }
        List<Dept> depts = deptService.selectList(wrapper);
        for (Dept dept : depts) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", dept.getName());
            map.put("id", dept.getId());
            map.put("spread", true);
            map.put("key", "dept_id");
            List<Map<String, Object>> childrens = new ArrayList<>();
            map.put("children", childrens);
            dealUserDept(dept, childrens, hostId, userId);
            list.add(map);
        }
    }
 
    // -------------------------------------------------------------------------------------------------------
 
    /**
     * 条件筛选
     */
    @SuppressWarnings("unchecked")
    public void remove(String condition, List<Map<String, Object>> list) {
        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            if (map.get("children") != null) {
                List<Map<String, Object>> children = (List<Map<String, Object>>) map.get("children");
                if (children.size() > 0) {
                    remove(condition, children);
                } else {
                    if (!String.valueOf(map.get("title")).contains(condition)) {
                        iterator.remove();
                    }
                }
            }
        }
    }
 
}