package com.ruoyi.hezhi.api.kaoshi;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.hezhi.domain.dto.ConferenceRegistrationDTO;
import com.ruoyi.hezhi.domain.dto.StudentDTO;
import com.ruoyi.hezhi.domain.dto.StudyCenterDTO;
import com.ruoyi.hezhi.service.ITbConferenceRegistrationService;
import com.ruoyi.hezhi.service.ITbExamRegistrationService;
import com.ruoyi.hezhi.service.ITbStudentService;
import com.ruoyi.hezhi.service.ITbStudyCenterService;
import io.swagger.annotations.Api;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: LCL
 * @Date: 2024/10/08
 * @Description:通用API
 */
@Api("申请")
@RestController
@RequestMapping("/api/apply")
public class ApiApplyController {

    @Resource
    private ITbStudyCenterService studyCenterService;
    @Resource
    private ITbStudentService studentService;
    @Resource
    private ITbConferenceRegistrationService conferenceRegistrationService;
    @Resource
    private ITbExamRegistrationService examRegistrationService;


    /**
     * 学习中心入驻
     *
     * @param studyCenterDTO
     * @return ok
     */
    @RepeatSubmit
    @RateLimiter(key = "API_joinInStudyCenter", time = 5, count = 50)
    @PostMapping("/joinInStudyCenter")
    public AjaxResult joinInStudyCenter(@RequestBody @Validated({StudyCenterDTO.joinInStudyCenter.class}) StudyCenterDTO studyCenterDTO, BindingResult bindingResult) {
        // 参数校验
        if (bindingResult.hasErrors()) {
            return AjaxResult.allParameterErrors(bindingResult);
        }
        try {
            return studyCenterService.joinInStudyCenter(studyCenterDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("学习中心入驻异常");
        }
    }

    /**
     * 学员认证
     *
     * @param studentDTO
     * @return ok
     */
    @RepeatSubmit
    @RateLimiter(key = "API_authenticationStudent", time = 5, count = 50)
    @PostMapping("/authenticationStudent")
    public AjaxResult authenticationStudent(@RequestBody @Validated({StudentDTO.authenticationStudent.class}) StudentDTO studentDTO, BindingResult bindingResult) {
        // 参数校验
        if (bindingResult.hasErrors()) {
            return AjaxResult.allParameterErrors(bindingResult);
        }
        try {
            return studentService.authenticationStudent(studentDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("学员认证异常");
        }
    }

    /**
     * 参会报名
     *
     * @param conferenceRegistrationDTO
     * @return ok
     */
    @RepeatSubmit
    @RateLimiter(key = "API_conferenceRegistration", time = 5, count = 50)
    @PostMapping("/conferenceRegistration")
    public AjaxResult conferenceRegistration(@RequestBody @Validated({ConferenceRegistrationDTO.conferenceRegistration.class}) ConferenceRegistrationDTO conferenceRegistrationDTO, BindingResult bindingResult) {
        // 参数校验
        if (bindingResult.hasErrors()) {
            return AjaxResult.allParameterErrors(bindingResult);
        }
        try {
            return conferenceRegistrationService.conferenceRegistration(conferenceRegistrationDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("参会报名异常");
        }
    }

}
