package com.qy.worksheetsystem.controller;

import com.qy.worksheetsystem.model.MessageResult;
import com.qy.worksheetsystem.service.UserService;
import com.qy.worksheetsystem.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * @description 测试
 */

@RestController
@Api(value = "测试Controller")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户登录", notes = "登录--不进行拦截")
    @PostMapping("/getRedirectUri")
    public MessageResult login(@RequestParam("number") String number) {
        String realPassword = userService.getPassword(number);
        if (realPassword == null) {
            return MessageResult.fail().add("info", "用户名错误");
        } else if (!realPassword.equals(number)) {
            return MessageResult.fail().add("info", "密码错误");
        } else {
            return MessageResult.success().add("token", JWTUtil.createToken(number));
        }
    }

    @ApiOperation(value = "无权限", notes = "无权限跳转的接口")
    @RequestMapping(path = "/getRedirectUri/{message}")
    public MessageResult unauthorized(@PathVariable String message) throws UnsupportedEncodingException {
        return MessageResult.fail().add("info", message);
    }

    @ApiOperation(value = "特定用户访问", notes = "拥有 user, admin 角色的用户可以访问下面的页面")
    @PostMapping("/getMessage")
    @RequiresRoles(logical = Logical.OR, value = {"user", "admin"})
    public MessageResult getMessage() {
        return MessageResult.success().add("info", "成功获得信息！");
    }

    @ApiOperation(value = "Vip用户访问", notes = "拥有 vip 权限可以访问该页面")
    @PostMapping("/getVipMessage")
    @RequiresRoles(logical = Logical.OR, value = {"user", "admin"})
    @RequiresPermissions("vip")
    public MessageResult getVipMessage() {
        return MessageResult.success().add("info", "成功获得 vip 信息！");
    }
}