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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
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.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vion.dto.UserDTO;
import vion.mapper.UserMapper;
import vion.model.Dept;
import vion.model.RUserRole;
import vion.model.Role;
import vion.model.User;
import vion.service.IDeptService;
import vion.service.IRoleService;
import vion.service.IUserService;
import vion.vo.RoleVO;
import vion.vo.UserVO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

    private final IDeptService deptService;
    private final IRoleService roleService;
    private final Converter converter;

    @Override
    public Page<UserVO> getUserList(UserDTO dto) {
        MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, new User()))
                .selectAll(User.class)
                .select(Dept::getDeptName)
                .leftJoin(Dept.class, Dept::getDeptId, User::getDeptId)
                .orderByAsc(User::getStatus);
        Page<UserVO> userVOS = this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), UserVO.class, wrapper);
        MPJLambdaWrapper<Role> roleWrapper = new MPJLambdaWrapper<Role>()
                .selectAll(Role.class)
                .select(RUserRole::getUserId)
                .leftJoin(RUserRole.class, RUserRole::getRoleId, Role::getId)
                .in(RUserRole::getUserId, Opt.ofEmptyAble(userVOS.getRecords().stream().map(UserVO::getId).collect(Collectors.toList())).orElse(ListUtil.of(-1L)));
        List<RoleVO> roleVOList = roleService.selectJoinList(RoleVO.class, roleWrapper);
        Opt.ofEmptyAble(roleVOList)
                .map(l -> l.stream().collect(Collectors.groupingBy(RoleVO::getUserId)))
                .ifPresent(m -> userVOS.getRecords().forEach(u -> u.setRoleVOList(m.getOrDefault(u.getId(), ListUtil.empty()))));
        return userVOS;
    }

    @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.<String, Object>builder().put("type", "dept").put("pkId", v.getId()).build());
            nodeList.add(node);
        });
        userList.forEach(v -> {
            TreeNode<String> node = new TreeNode<>(v.getUserid(), v.getDeptId().toString(), v.getUsername(), 2);
            node.setExtra(MapUtil.<String, Object>builder().put("type", "user").put("pkId", v.getId()).build());
            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);
    }
}