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

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.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.tree.MapTree;
import org.dromara.hutool.core.tree.TreeNode;
import org.dromara.hutool.core.tree.TreeUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import vion.constant.RedisKeyEnum;
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.IRUserRoleService;
import vion.service.IRoleService;
import vion.service.IUserService;
import vion.vo.RoleVO;
import vion.vo.UserVO;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 IRUserRoleService userRoleService;
    private final RedissonClient redissonClient;
    private final Converter converter;

    @Override
    public Page<UserVO> getUserList(UserDTO dto) {
        var userIdSet = Opt.ofNullable(dto.getRoleId())
                .map(roleId -> userRoleService.lambdaQuery().eq(RUserRole::getRoleId, roleId).list())
                .filter(CollUtil::isNotEmpty)
                .map(l -> l.stream().map(RUserRole::getUserId).collect(Collectors.toSet()))
                .orElse(Set.of(-1L));

        var wrapper = new MPJLambdaWrapper<>(converter.convert(dto, User.class))
                .selectAll(User.class)
                .select(Dept::getDeptName)
                .leftJoin(Dept.class, Dept::getDeptId, User::getDeptId)
                .ne(User::getStatus, 5)
                .in(ObjUtil.isNotNull(dto.getRoleId()), User::getId, userIdSet)
                .orderByAsc(User::getStatus);
        var userVOS = this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), UserVO.class, wrapper);
        var 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)));
        var 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<MapTree<String>> getOrgTree(String deptId) {
        var deptList = deptService.list();
        var userList = this.lambdaQuery().ne(User::getStatus, 5).list();

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

    @Override
    public void syncUser() {
        Opt.ofEmptyAble(this.lambdaQuery().ge(User::getModifyTime, LocalDate.now()).list())
                .ifPresent(l -> l.forEach(user -> {
                    redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + user.getId()).set(user);
                    redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_NAME.getVal() + user.getUsername()).set(user);
                }));
    }
}