package cn.iocoder.yudao.module.system.api.permission;

import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO;
import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleMapper;
import cn.iocoder.yudao.module.system.service.permission.RoleService;
import com.google.common.collect.ImmutableList;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色 API 实现类
 *
 * @author 芋道源码
 */
@Service
public class RoleApiImpl implements RoleApi {

    @Resource
    private RoleService roleService;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public void validRoleList(Collection<Long> ids) {
        roleService.validateRoleList(ids);
    }

    @Override
    public List<Long> getRoleUserIds(String code) {
        RoleDO roleCode = roleService.getRoleByCode(code);
        Long roleId = roleCode.getId();
        List<Long> collect = userRoleMapper.selectListByRoleIds(ImmutableList.of(roleId)).stream().map(UserRoleDO::getUserId).collect(Collectors.toList());
        return collect;
    }
}
