package com.xy.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.xy.consts.CommConsts;
import com.xy.dto.AuthorizeDto;
import com.xy.redis.RedisCache;
import com.xy.service.AuthorizeService;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Api(tags = "鉴权相关接口")
@Service
@AllArgsConstructor
public class AuthorizeServiceImpl implements AuthorizeService {
    private SysMenuServiceImpl sysMenuService;

    @Override
    @ApiOperation("获取所有系统ID")
    public R<List<Long>> getSystemIds() {
        List<Long> systemIds = RedisCache.getSystemIds();
        return R.ok(systemIds);
    }

    @Override
    @ApiOperation("获取所有角色ID")
    public R<List<Long>> getRoleIds(AuthorizeDto authorizeDto) {
        List<Long> roleIds = RedisCache.getRoleIds(authorizeDto.getSystemId());
        return R.ok(roleIds);
    }

    @Override
    @ApiOperation("获取所有角色CODE")
    public R<List<String>> getRoleCodes(AuthorizeDto authorizeDto) {
        List<String> roleCodes = RedisCache.getRoleCodes(authorizeDto.getSystemId());
        return R.ok(roleCodes);
    }

    @Override
    @ApiOperation("是否是管理员")
    public R<Boolean> isAdmin(AuthorizeDto authorizeDto) {
        Boolean admin = RedisCache.isAdmin(authorizeDto.getSystemId());
        return R.ok(admin);
    }

    @Override
    @ApiOperation("获取所有菜单接口uri")
    public R<List<String>> getMenuInterfaceUri(AuthorizeDto authorizeDto) {
        List<String> menuInterfaceUri = RedisCache.getMenuInterfaceUri(authorizeDto.getSystemId());
        return R.ok(menuInterfaceUri);
    }

    @Override
    @ApiOperation("登录鉴权")
    public R<Boolean> authByLogin() {
        boolean b = RedisCache.authByLogin();
        return R.ok(b);
    }

    @Override
    @ApiOperation("接口鉴权")
    public R<Boolean> authByInterface(AuthorizeDto authorizeDto) {
        boolean b = RedisCache.authByInterface(authorizeDto.getSystemId(), authorizeDto.getUri());
        return R.ok(b);
    }

    @Override
    @ApiOperation("数据鉴权")
    public R<Boolean> authByData(AuthorizeDto authorizeDto) {
        boolean b = RedisCache.authByData(authorizeDto.getSystemId(), authorizeDto.getUri());
        return R.ok(b);
    }

    @Override
    @ApiOperation("获取登录id")
    public R<Object> getLoginId() {
        Object loginId = RedisCache.getLoginId(Object.class);
        return R.ok(loginId);
    }

    @Override
    @ApiOperation("token续签")
    public R<Boolean> renewTimeout() {
        StpUtil.renewTimeout(RedisCache.getToken(), CommConsts.TOKEN_TIMEOUT);
        return R.ok(true);
    }

    @Override
    @ApiOperation("获取客户端类型")
    public R<String> getClientType() {
        String clientType = RedisCache.getClientType();
        return R.ok(clientType);
    }


}
