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();
|
}
|
}
|
}
|
}
|
}
|
|
}
|