package com.gz.controller.system;

import com.gz.core.exception.BusinessException;
import com.gz.dto.system.AdminDTO;
import com.gz.dto.system.MenuDTO;
import com.gz.jwt.JwtPayload;
import com.gz.rvo.system.AdminRVO;
import com.gz.rvo.system.InitialHomeRVO;
import com.gz.rvo.system.LoginRVO;
import com.gz.service.system.AuthService;
import com.gz.utils.JwtUtils;
import com.gz.vo.system.LoginVO;
import com.gz.vo.system.UpdatePasswordVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 登录授权控制器
 * @author LiuchangLan
 * @date 2020/7/16 14:34
 */
@RestController
@RequestMapping("system/auth")
public class AuthController {

    @Resource
    private AuthService authService;

    @PostMapping("login")
//    @TraceLog(module = "登录授权模块",business = "用户登录")
    public LoginRVO login(@RequestBody LoginVO loginVO) throws BusinessException, NoSuchMethodException {
        return authService.login(loginVO);
    }

    @GetMapping("loginByBase64")
    public LoginRVO loginByBase64(String base64){
        return authService.login(base64);
    }

    @GetMapping("logout")
    public void logout(String refreshToken) {
        authService.logout(refreshToken);
    }

    @GetMapping("refreshToken")
    public LoginRVO refreshToken(@RequestParam String refreshToken){
        return authService.refreshToken(refreshToken);
    }

    /**
     * @description 获取登录的用户名
     * @author LiuChangLan
     * @since 2020/9/25 15:43
     */
    @GetMapping("getLoginUserName")
    public JwtPayload getLoginUserName(){
        return JwtUtils.getCurrentUserJwtPayload();
    }

    /**
     * @description 获取有权限的菜单
     * @author LiuChangLan
     * @since 2020/9/25 15:43
     */
    @GetMapping("getLoginMenus")
    public InitialHomeRVO getLoginMenus(){ return authService.getLoginMenus(); }

    /**
     * @description 获取有权限的按钮
     * @author LiuChangLan
     * @since 2020/9/25 15:43
     */
    @GetMapping("getLoginButtons")
    public List<MenuDTO> getLoginButtons(Integer menuId){
        return authService.getLoginButtons(menuId);
    }


    @PostMapping("updatePassword")
    public Integer updatePassword(@RequestBody UpdatePasswordVO vo) throws BusinessException {
        return authService.updatePassword(vo);
    }

    /**
     * @description 生成验证码
     * @author LiuChangLan
     * @since 2021/2/22 18:00
     */
    @GetMapping("generatePictureCaptcha")
    public String generatePictureCaptcha(){
        return authService.generatePictureCaptcha();
    }

    @GetMapping("currLoginAdmin")
    public AdminDTO currLoginAdmin(){
        return authService.currLoginAdmin();
    }

    @GetMapping("currLoginAdminDept")
    AdminRVO currLoginAdminDept(){
       return authService.currLoginAdminDept();
    }
}
