package com.koobietech.eas.controller;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.koobietech.eas.common.exception.EasException;
import com.koobietech.eas.common.result.JsonResult;
import com.koobietech.eas.dao.Pojo.AdminPojo;
import com.koobietech.eas.dao.adminLoginPojo.UserDetail;
import com.koobietech.eas.dao.dto.LoginToken;
import com.koobietech.eas.service.StudentLoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lc
 */
@RestController
@Tag(name = "学生登陆模块")
public class StudentLoginController {

    @Resource
    CaptchaService captchaService;

    @Resource
    StudentLoginService studentLoginService;

    @PostMapping("/studentLogin")
    @Operation(summary = "学生登陆", description = "用户名和密码为请求载荷，若登录成功，返回两个token")
    public JsonResult studentLogin(@RequestBody AdminPojo adminPojo) {

        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaVerification(adminPojo.getCaptchaVerification());

        ResponseModel response = captchaService.verification(captchaVO);
        if (!response.isSuccess()) {
            throw new EasException(response.getRepMsg(), Integer.parseInt(response.getRepCode()));
        }

        LoginToken ret = studentLoginService.studentLogin(adminPojo);
        return JsonResult.data(ret);
    }
}
