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

import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import org.springframework.stereotype.Service;

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

/**
 * 权限 API 实现类
 *
 * @author 芋道源码
 */
@Service
public class PermissionApiImpl implements PermissionApi {

    @Resource
    private PermissionService permissionService;

    @Override
    public Set<Long> getUserRoleIdListByRoleIds(Collection<Long> roleIds) {
        return permissionService.getUserRoleIdListByRoleId(roleIds);
    }

    @Override
    public boolean hasAnyPermissions(Long userId, String... permissions) {
        return permissionService.hasAnyPermissions(userId, permissions);
    }

    @Override
    public boolean hasAnyRoles(Long userId, String... roles) {
        return permissionService.hasAnyRoles(userId, roles);
    }

    @Override
    public DeptDataPermissionRespDTO getDeptDataPermission(Long userId) {
        return permissionService.getDeptDataPermission(userId);
    }

    /**
     * 设置用户角色
     *
     * @param userId  角色编号
     * @param roleCodes 角色编号集合
     */
    @Override
    public void assignUserRole(Long userId, Set<String> roleCodes) {
        permissionService.assignUserRoleByCodes(userId, roleCodes);
    }

    /**
     * 设置用户角色
     *
     * @author 小李
     * @date 18:58 2024/8/14
     * @param userId 用户D
     * @param roleIds 角色IDs
     **/
    @Override
    public void assignUserRoleByRoleIds(Long userId, Set<Long> roleIds){
        permissionService.assignUserRole(userId, roleIds);
    }

    /**
     * 获取某个用户的角色IDs
     * @author 小李
     * @date 19:24 2024/8/14
     * @param userId 用户ID
     **/
    @Override
    public List<Long> getRoleIdsByUserId(Long userId){
        Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(userId);
        return roleIds.stream().collect(Collectors.toList());
    }

    /**
     * 根据权限ID拿拥有该权限的所有人
     * @author 小李
     * @date 10:25 2024/8/26
     * @param roleId 权限ID
     **/
    @Override
    public List<Long> getUserIdByRoleId(Long roleId){
        return permissionService.getUserIdByRoleId(roleId);
    }

}
