jinlin
2024-02-26 6f0714843341b168573ad0272069f7af2d3d2b87
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
87
88
89
90
91
92
93
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有,侵权必究!
 */
 
package com.zt.modules.sys.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.common.constant.CacheKey;
import com.zt.common.constant.Constant;
import com.zt.common.service.BaseService;
import com.zt.common.utils.CacheUtils;
import com.zt.core.context.UserContext;
import com.zt.modules.sys.dao.SysRoleMenuDao;
import com.zt.modules.sys.dto.RoleDto;
import com.zt.modules.sys.model.SysRoleMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 角色与菜单对应关系
 *
 * @author hehz
 */
@Service
public class SysRoleMenuService extends BaseService<SysRoleMenuDao, SysRoleMenu> {
 
    @Autowired
    private SysRoleUserService sysRoleUserService;
 
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
        List<SysRoleMenu> list = menuIdList.stream().map(menuId -> new SysRoleMenu(roleId, menuId))
                .collect(Collectors.toList());
        this.saveRelatedDatas("role_id", roleId, "menu_id", list);
 
        CacheUtils.removeAll(Constant.Cache.TOKEN);// 清理用户权限缓存
    }
 
    /**
     * 根据角色ID,获取菜单ID列表
     */
    public List<Long> getRoleMenuIds(Long roleId) {
        List<Long> list = baseDao.getByRoleId(roleId).stream().map(d -> d.getMenuId()).collect(Collectors.toList());
        return list;
    }
 
    /**
     * 根据角色id,删除角色菜单关系
     *
     * @param roleIds
     *            角色ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRoleIds(Long[] roleIds) {
        this.deleteLogic(baseDao.selectList(new QueryWrapper<SysRoleMenu>().lambda()
                .in(SysRoleMenu::getRoleId, roleIds)));
 
        CacheUtils.removeAll(Constant.Cache.TOKEN);// 清理用户权限缓存
    }
 
    /**
     * 根据菜单id,删除角色菜单关系
     *
     * @param menuId
     *            菜单id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByMenuId(Long menuId) {
        baseDao.delete(new QueryWrapper<SysRoleMenu>().lambda().eq(SysRoleMenu::getMenuId, menuId));
 
        CacheUtils.removeAll(Constant.Cache.TOKEN);// 清理用户权限缓存
    }
 
    public List<SysRoleMenu> roleMenu() {
        List<RoleDto> roles = sysRoleUserService.getUserRoles(UserContext.getUser().getId());
        List<SysRoleMenu> sysRoleMenu = null;
        for (RoleDto role : roles) {
            RoleDto roleDto = new RoleDto();
            roleDto.setId(role.getId());
             sysRoleMenu = baseDao.roleMenu(roleDto);
        }
        return sysRoleMenu;
    }
 
}