package com.sckw.auth.controller;

import com.sckw.auth.model.vo.req.*;
import com.sckw.auth.service.IAuthService;
import com.sckw.core.model.enums.LoginMethodEnum;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @desc: 用户权限
 * @author: czh
 * @date: 2023/6/19
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private IAuthService authService;

    /**
     * @param reqVo 登录入参
     * @return HttpResult
     * @desc: 用户登录
     * @author: czh
     * @date: 2023/6/16
     */
    @PostMapping("/login")
    public HttpResult login(@Valid @RequestBody LoginReqVo reqVo, HttpServletRequest request) {
        int systemType = request.getIntHeader("System-Type");
        String clientType = request.getHeader("Client-Type");
        String accessSpecial = request.getHeader("Access-Special");

        reqVo.setSystemType(systemType);
        reqVo.setClientType(clientType);

        LoginBase loginBase = new LoginBase();
        loginBase.setAccount(reqVo.getAccount());
        loginBase.setPassword(reqVo.getPassword());
        loginBase.setCaptcha(reqVo.getCaptcha());
        loginBase.setSystemType(systemType);
        loginBase.setClientType(clientType);
        loginBase.setAccessSpecial(accessSpecial);
        loginBase.setLoginMethod(LoginMethodEnum.ORDINARY.getValue());
        return authService.login(loginBase);
    }

    /**
     * @return HttpResult
     * @desc: 根据token获取登录信息
     * @author: czh
     * @date: 2023/9/27
     */
    @PostMapping("getLoginResByToken")
    public HttpResult getLoginResByToken(HttpServletRequest request) {
        String clientType = request.getHeader("Client-Type");
        String token = request.getHeader("Access-Token");
        return HttpResult.ok(authService.getLoginResByToken(clientType, token));
    }

    /**
     * @param id 主键ID
     * @desc 重置密码
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping("/resetPassword")
    public HttpResult resetPassword(@RequestParam Long id) {
        authService.resetPassword(id);
        return HttpResult.ok(HttpStatus.MSG_001);
    }

}