UserServiceImpl.java 2.05 KB
package vion.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vion.mapper.UserMapper;
import vion.model.Dept;
import vion.model.User;
import vion.service.IDeptService;
import vion.service.IUserService;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final IDeptService deptService;

    @Override
    public List<Tree<String>> getOrgTree(String deptId) {
        List<Dept> deptList = deptService.list();
        List<User> userList = this.list();

        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        deptList.forEach(v -> {
            TreeNode<String> node = new TreeNode<>(v.getDeptId().toString(), v.getParentId().toString(), v.getDeptName(), 1);
            node.setExtra(MapUtil.of("type", "dept"));
            node.setExtra(MapUtil.of("pkId", v.getId()));
            nodeList.add(node);
        });
        userList.forEach(v -> {
            TreeNode<String> node = new TreeNode<>(v.getUserid(), v.getDeptId().toString(), v.getUsername(), 2);
            node.setExtra(MapUtil.of("type", "user"));
            node.setExtra(MapUtil.of("pkId", v.getId()));
            nodeList.add(node);
        });
        List<Tree<String>> wholeTree = TreeUtil.build(nodeList, "1");
        return Opt.ofBlankAble(deptId).map(d -> {
            List<Tree<String>> resTree = new ArrayList<>();
            wholeTree.forEach(t ->
                    t.walk(tmp -> {
                        if (tmp.getId().equals(d)) {
                            resTree.add(tmp);
                        }
                    }));
            return resTree;
        }).orElse(wholeTree);
    }
}