package com.xhkjedu.smarking.controller.stupaper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.exception.MissingParametersException;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudent;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion;
import com.xhkjedu.smarking.service.stupaper.MsPaperStudentService;
import com.xhkjedu.smarking.vo.stupaper.PaperStudentWebVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
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;
import java.util.List;
import java.util.Map;

/**
 * @Description 阅卷试卷学生表相关操作
 * @Author auto
 * @Date 2024-10-15
 */
@Slf4j
@RestController
@RequestMapping("/mps")
public class MsPaperStudentController {
    @Resource
    private MsPaperStudentService msPaperStudentService;

    /*
     * @Description 学生考试列表-待进行
     * @Date 2024/10/30 16:33:51
     * @Author WN
     * @Param [eps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_pre")
    public ResultVo listExamPrePapersForStudent(@RequestBody MsPaperStudent mps) {
        N_Utils.validation(new Object[]{mps.getStudentid(), "学生id", 1});
        List<Map<String,Object>> rtnlst = msPaperStudentService.listExamPrePapersForStudent(mps);
        return new ResultVo(0, "查询成功", rtnlst);
    }

    /*
     * @Description 学生考试列表-已结束
     * @Date 2024/10/30 16:48:00
     * @Author WN
     * @Param [eps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_end")
    public ResultVo listExamEndPapersForStudent(@RequestBody MsPaperStudent mps) {
        Integer page = mps.getPage();
        Integer pageSize = mps.getPageSize();
        N_Utils.validation(new Object[]{mps.getStudentid(), "学生id", 1,page, "显示页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map<String,Object>> rtnlst = msPaperStudentService.listExamEndPapersForStudent(mps.getStudentid());
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(rtnlst));
        return new ResultVo(0, "查询成功", pageResult);
    }

    /*
     * @Description 学生待考-获取服务器当前时间
     * @Date 2024/10/31 9:56:00
     * @Author WN
     * @Param [ps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/wait_time")
    public ResultVo getNowTimestamp(@RequestBody MsPaperStudent ps) {
        Integer timestamp = N_Utils.getSecondTimestamp();
        return new ResultVo(0, "获取当前时间戳", timestamp);
    }

    /*
     * @Description 学生待考-Web考试科目信息、试卷试题信息
     * @Date 2024/10/31 11:51:39
     * @Author WN
     * @Param [mps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/wait_infoweb")
    public ResultVo getWaitWebStudentPaper(@RequestBody MsPaperStudent mps) {
        N_Utils.validation(new Object[]{mps.getMpsid(), "学生试卷id", 1});
        Map<String,Object> map = msPaperStudentService.getWaitWebStudentPaperByMpsid(mps.getMpsid());
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * @Description 学生待考-Web考试科目信息、试卷试题信息
     * @Date 2024/10/31 14:53:58
     * @Author WN
     * @Param [mps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/wait_infoand")
    public ResultVo getAndroidWebStudentPaper(@RequestBody MsPaperStudent mps) {
        N_Utils.validation(new Object[]{mps.getMpsid(), "学生试卷id", 1});
        Map<String,Object> map = msPaperStudentService.getWaitAndroidStudentPaperByMpsid(mps.getMpsid());
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * @Description 学生待考-进入考试
     * @Date 2024/10/31 16:41:50
     * @Author WN
     * @Param [mps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/wait_in")
    public ResultVo updatePaperStudentForIn(@RequestBody MsPaperStudent mps) {
        try {
            N_Utils.validation(new Object[]{mps.getMpsid(), "学生试卷id", 1});
            Integer endTime = msPaperStudentService.updatePaperStudentForIn(mps.getMpsid());
            return new ResultVo(0, "操作成功", endTime);
        }catch(MissingParametersException e){
            log.error("进入考场出错：" + e.getMessage());
            return new ResultVo(1,e.getMessage());
        } catch (Exception e) {
            log.error("进入考场出错：" + e.getMessage());
            return new ResultVo(1, "操作失败");
        }
    }

    /*
     * @Description 学生考试-web提交试题
     * @Date 2024/11/1 11:01:01
     * @Author WN
     * @Param [psq]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/commit_queweb")
    public ResultVo commitQuestionAnswerForWeb(@RequestBody PaperStudentWebVo paperStudentWebVo) {
        N_Utils.validation(new Object[]{paperStudentWebVo.getMpsid(), "学生试卷id", 1});
        List<MsPaperStudentQuestion> questions = paperStudentWebVo.getQuestions();
        if (N_Utils.isListEmpty(questions)) {
            return new ResultVo(1, "试题不能为空");
        }
        msPaperStudentService.saveCommitQuestionForWeb(paperStudentWebVo);
        return new ResultVo(0, "保存成功");
    }

    /*
     * @Description 学生考试-android提交试题
     * @Date 2024/11/1 10:41:59
     * @Author WN
     * @Param [psq]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/commit_queand")
    public ResultVo commitQuestionAnswerForAndroid(@RequestBody MsPaperStudentQuestion psq) {
        N_Utils.validation(new Object[]{psq.getMpsqid(), "学生试题id", 1});
        msPaperStudentService.saveCommitQuestionForAndroid(psq);
        return new ResultVo(0, "保存成功");
    }

    /*
     * @Description 学生考试-提交试卷
     * @Date 2024/11/1 11:41:53
     * @Author WN
     * @Param [paperStudent]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/commit_paper")
    public ResultVo commitPaperAnswer(@RequestBody MsPaperStudent paperStudent) {
        N_Utils.validation(new Object[]{paperStudent.getMpsid(), "学生试卷id", 1});
        return msPaperStudentService.saveCommitPaper(paperStudent);
    }

    /*
     * @Description 学生试卷-题型试题、作答、得分情况 (修改分数)
     * @Date 2024/11/14 14:39:57
     * @Author WN
     * @Param [paperStudent]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/stu_paper")
    public ResultVo listStuPaperQtypeQueAnswerForWeb(@RequestBody MsPaperStudent paperStudent) {
        N_Utils.validation(new Object[]{paperStudent.getMpsid(), "学生试卷id", 1});
        List<Map<String,Object>> rtnlst = msPaperStudentService.listStuPaperQtypeQueAnswerForWeb(paperStudent.getMpsid());
        return new ResultVo(0, "查询成功", rtnlst);
    }

}
