/** 
 | 
 * 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; 
 | 
    } 
 | 
  
 | 
} 
 |