package com.xindazhou.user.api.permission;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.biz.system.permission.dto.DeptDataPermissionRespDTO;
import com.xindazhou.framework.common.biz.system.permission.PermissionCommonApi;
import com.xindazhou.user.service.permission.PermissionService;
import org.springframework.context.annotation.Primary;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.Set;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
@Primary // 由于 PermissionCommonApi 的存在，必须声明为 @Primary Bean
public class PermissionApiImpl implements PermissionCommonApi {

    @Resource
    private PermissionService permissionService;

    @Override
    public CommonResult<Boolean> hasAnyPermissions(Long userId, String... permissions) {
        return success(permissionService.hasAnyPermissions(userId, permissions));
    }

    @Override
    public CommonResult<Boolean> hasAnyRoles(Long userId, String... roles) {
        return success(permissionService.hasAnyRoles(userId, roles));
    }

    @Override
    public CommonResult<DeptDataPermissionRespDTO> getDeptDataPermission(Long userId) {
        return success(permissionService.getDeptDataPermission(userId));
    }

    @Override
    public CommonResult<Boolean> processUserDeleted(Long userId) {
        permissionService.processUserDeleted(userId);
        return success(true);
    }

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

    @Override
    public CommonResult<Set<Long>> getUserRoleIdListByUserId(Long userId) {
        return success(permissionService.getUserRoleIdListByUserId(userId));
    }

    @Override
    public CommonResult<Set<Long>> getRoleMenuListByRoleId(Collection<Long> roleIds) {
        return success(permissionService.getRoleMenuListByRoleId(roleIds));
    }

    @Override
    public CommonResult<Boolean> assignUserRole(Long userId, Set<Long> roleIds) {
        permissionService.assignUserRole(userId, roleIds);
        return success(true);
    }

    @Override
    public CommonResult<Boolean> assignRoleMenu(Long roleId, Set<Long> menuIds) {
        permissionService.assignRoleMenu(roleId, menuIds);
        return success(true);
    }

    @Override
    public CommonResult<Set<Long>> getRoleMenuListBySingleRoleId(Long roleId) {
        return success(permissionService.getRoleMenuListByRoleId(java.util.Collections.singleton(roleId)));
    }

}

