package com.elephant.framework.galaxy.aroad.module.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.elephant.framework.galaxy.aroad.module.system.entity.Role;
import com.elephant.framework.galaxy.aroad.module.system.entity.RoleMenu;
import com.elephant.framework.galaxy.aroad.module.system.mapper.RoleMapper;
import com.elephant.framework.galaxy.aroad.module.system.mapper.RoleMenuMapper;
import com.elephant.framework.galaxy.aroad.module.system.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统角色表 服务实现类
 * </p>
 *
 * @author wangjian
 * @since 2019-02-21
 */
@Service
@AllArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    private RoleMenuMapper roleMenuMapper;

    /**
     * 通过用户ID，查询角色信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<Role> findRolesByUserId(Integer userId) {
        return baseMapper.listRolesByUserId(userId);
    }

    /**
     * 通过角色ID，删除角色,并清空角色菜单缓存
     *
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value = "menu_details", allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeRoleById(Integer id) {
        roleMenuMapper.delete(Wrappers
                .<RoleMenu>update().lambda()
                .eq(RoleMenu::getRoleId, id));
        return this.removeById(id);
    }
}
