ludc
2023-10-19 a358eb77f76aab25196a489c641d1687f2756cef
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.vci.ubcs.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vci.ubcs.starter.util.MybatisParameterUtil;
import com.vci.ubcs.system.entity.RoleMenu;
import com.vci.ubcs.system.mapper.RoleMenuMapper;
import com.vci.ubcs.system.service.IRoleMenuService;
import com.vci.ubcs.system.service.IRoleService;
import com.vci.ubcs.system.user.entity.User;
import com.vci.ubcs.system.user.feign.IUserClient;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {
 
    @Resource
    private RoleMenuMapper roleMenuMapper;
 
    @Resource
    private IUserClient userClient;
 
    /**
     * 根据用户id查询,被授权的菜单id
     * @param userId
     * @return
     */
    @Override
    public List<Long> getMenuIdByUserId(Long userId) {
        // 根据用户id查询菜单id
        R<User> userR = userClient.userInfoById((Func.isEmpty(userId) ? AuthUtil.getUserId():userId));
        if (!userR.isSuccess() || Func.isEmpty(userR.getData())) {
            throw new ServiceException("用户信息查询失败,原因:"+userR.getMsg());
        }
 
        // 当查询条件in大于一千条时
        List<String> roleIdList = Func.toStrList(userR.getData().getRoleId());
        //构建查询菜单id的查询条件
        LambdaQueryWrapper<RoleMenu> roleMenuWrapper = Wrappers.<RoleMenu>query()
            .lambda().select(RoleMenu::getMenuId);
        if(roleIdList.size()>=1000){
            MybatisParameterUtil.cutInParameter(roleMenuWrapper,RoleMenu::getRoleId,roleIdList);
        }else {
            roleMenuWrapper.in(RoleMenu::getRoleId,roleIdList);
        }
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(roleMenuWrapper);
        if(roleMenus.isEmpty()){
            return new ArrayList<>();
        }
        return roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
    }
 
 
}