package com.xhkjedu.sexam.controller.paperstudent;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.exception.MissingParametersException;
import com.xhkjedu.sexam.model.paperstudent.EPaperStudent;
import com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion;
import com.xhkjedu.sexam.model.paperstudent.EScanerror;
import com.xhkjedu.sexam.service.paperstudent.EPaperStudentService;
import com.xhkjedu.sexam.vo.paperstudent.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 WN
 * Date 2022/7/26 13:34
 **/
@RestController
@RequestMapping("/eps")
@Slf4j
public class EPaperStudentController {
    @Resource
    private EPaperStudentService ePaperStudentService;

    //学生考试列表--进行中
    @PostMapping("/jxlist")
    public ResultVo listExamPapersForStudentJx(@RequestBody EPaperStudent eps) {
        N_Utils.validation(new Object[]{eps.getStudentid(), "学生id", 1});
        eps.setSstate(1); //进行中

        List<Map> rtnlst = ePaperStudentService.listExamPapersForStudent(eps);
        return new ResultVo(0, "成功获取考试列表", rtnlst);
    }

    //学生考试列表--已结束
    @PostMapping("/jslist")
    public ResultVo listExamPapersForStudentJs(@RequestBody EPaperStudent eps) {
        N_Utils.validation(new Object[]{eps.getStudentid(), "学生id", 1});
        eps.setSstate(2);//结束的考试
        Integer page = eps.getPage();
        Integer pageSize = eps.getPageSize();
        N_Utils.validation(new Object[]{page, "显示页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> rtnlst = ePaperStudentService.listExamsEndForStudent(eps);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(rtnlst));
        return new ResultVo(0, "成功获取考试列表", pageResult);
    }

    //待考界面--考试信息
    @PostMapping("/info")
    public ResultVo getExamSubjectPaper(@RequestBody EPaperStudent eps) {
        N_Utils.validation(new Object[]{eps.getEpsid(), "考试学生id", 1});
        Map map = ePaperStudentService.getExamSubjectPaperByEpsid(eps.getEpsid());
        return new ResultVo(0, "成功获取考试信息", map);
    }

    //学生开始考试(android)
    @PostMapping("/spa")
    public ResultVo startPaperForAndroid(@RequestBody EPaperStudent eps) {
        try {
            N_Utils.validation(new Object[]{eps.getEpsid(), "学生试卷id", 1});
            Map map = ePaperStudentService.startPaperForAndroid(eps);
            return new ResultVo(0, "成功获取学生试卷", map);
        }catch (MissingParametersException e){
            log.error("学生开始考试android出错：" + e.getMessage());
            return new ResultVo(1,e.getMessage());
        }catch (Exception e){
            log.error("学生开始考试android出错：" + e.getMessage());
            return new ResultVo(1,e.getMessage());
        }

    }

    //学生开始考试获取试卷
    @PostMapping("/span")
    public ResultVo startPaperForAndroidNew(@RequestBody EPaperStudent eps) {
        try {
            N_Utils.validation(new Object[]{eps.getEpsid(), "学生试卷id", 1});
            Map map = ePaperStudentService.startPaperForAndroidNew(eps);
            return new ResultVo(0, "成功获取学生试卷", map);
        }catch (MissingParametersException e){
            log.error("学生开始考试android出错：" + e.getMessage());
            return new ResultVo(1,e.getMessage());
        }catch (Exception e){
            log.error("学生开始考试android出错：" + e.getMessage());
            return new ResultVo(1,e.getMessage());
        }

    }

    //提交单题
    @PostMapping("/scq")
    public ResultVo saveCommitQuestion(@RequestBody EPaperStudentQuestion sq) {
        try {
            N_Utils.validation(new Object[]{sq.getEpsqid(), "试题id", 1});
            ePaperStudentService.saveCommitQuestion(sq);
            return new ResultVo(0, "成功提交试题");
        } catch (Exception e) {
            log.error("提交android试题失败：" + e.getMessage());
            return new ResultVo(1, "提交试题失败");
        }
    }

    //提交试卷
    @PostMapping("/scp")
    @Action("考试测评-提交试卷(app)")
    public ResultVo saveCommitPaper(@RequestBody EPaperStudent ps) {
        try {
            N_Utils.validation(new Object[]{ps.getEpsid(), "试卷id", 1});
            return ePaperStudentService.saveCommitPaper(ps);
        } catch (Exception e) {
            log.error("提交试卷出错：" + e.getMessage());
            return new ResultVo(1, "提交失败");
        }
    }
    //进入考试
    @PostMapping("/in")
    public ResultVo inSubjectExam(@RequestBody EPaperStudent ps) {
        try {
            N_Utils.validation(new Object[]{ps.getEpsid(), "试卷id", 1});
            Integer endTime = ePaperStudentService.updatePaperStudentState(ps.getEpsid());
            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 当前考试状态
     * @Param [ps]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/10/27 15:28
     **/
    @PostMapping("/ess")
    public ResultVo getExamSubjectState(@RequestBody EPaperStudent ps) {
        try {
            N_Utils.validation(new Object[]{ps.getEpsid(), "试卷id", 1});
            int result = ePaperStudentService.getExamstateSubjectState(ps.getEpsid());
            return new ResultVo(0, "获取成功",result);
        } catch (Exception e) {
            log.error("获取考试状态出错：" + e.getMessage());
            return new ResultVo(1, "获取考试状态失败");
        }
    }

    //学生开始考试(web)
    @PostMapping("/spw")
    public ResultVo startPaperForWeb(@RequestBody EPaperStudent eps) {
        try {
            N_Utils.validation(new Object[]{eps.getEpsid(), "学生试卷id", 1});
            Map psvo = ePaperStudentService.startPaperForWeb(eps);
            return new ResultVo(0, "成功获取学生试卷", psvo);
        }catch (MissingParametersException e){
            log.error("学生web获取试卷出错：" + e.getMessage());
            return new ResultVo(1,e.getMessage());
        }catch (Exception e){
            log.error("学生web获取试卷出错：" + e.getMessage());
            return new ResultVo(1,"获取试卷失败");
        }
    }

    //提交试题（web:一次提交可能多可客观题或一个主观题）
    @PostMapping("/scqw")
    public ResultVo saveCommitQuestionWeb(@RequestBody PaperStudentWebVo ps) {
        try {
            N_Utils.validation(new Object[]{ps.getEpsid(), "试卷id", 1});
            List<EPaperStudentQuestion> qlist = ps.getQuestions();
            if (N_Utils.isListEmpty(qlist)) {
                return new ResultVo(1, "试题不能为空");
            }
            return ePaperStudentService.saveCommitQuestionForWeb(ps);
        } catch (Exception e) {
            log.error("提交web试题出错：" + e.getMessage());
            return new ResultVo(1, "提交失败");
        }
    }

    //提交试卷(web)
    @PostMapping("/scpw")
    @Action("考试测评-提交试卷(网页)")
    public ResultVo saveCommitPaperWeb(@RequestBody PaperStudentWebVo ps) {
        try {
            N_Utils.validation(new Object[]{ps.getEpsid(), "试卷id", 1});
            List<EPaperStudentQuestion> qlist = ps.getQuestions();
            if (N_Utils.isListEmpty(qlist)) {
                return new ResultVo(1, "试题不能为空");
            }
            return ePaperStudentService.saveCommitPaperForWeb(ps);
        } catch (Exception e) {
            log.error("提交web试卷出错：" + e.getMessage());
            return new ResultVo(1, "提交失败");
        }
    }

    //保存扫描的答题卡
    @PostMapping("/sca")
    @Action("考试测评-保存扫描答题卡")
    public ResultVo saveCommitPaperScantron(@RequestBody PaperStudentWebVo ps) {
        N_Utils.validation(new Object[]{ps.getBatchid(), "批次id", 1, ps.getEpid(), "试卷id", 1});
        try {
            ePaperStudentService.saveCommitPaperForScantron(ps);
            return new ResultVo(0, "成功保存学生答题卡");
        } catch (Exception e) {
            log.error("保存学生答题卡失败：" + e.getMessage());
            return new ResultVo(1, "保存学生答题卡失败");
        }
    }

    //保存扫描出错答题卡
    @PostMapping("/scaer")
    @Action("考试测评-保存出错答题卡")
    public ResultVo saveScanPaperError(@RequestBody EScanerror eScanerror) {
        try {
            N_Utils.validation(new Object[]{eScanerror.getBatchid(),"批次id",1});
            ePaperStudentService.saveScanPaperError(eScanerror);
            return new ResultVo(0, "成功保存出错答题卡");
        } catch (Exception e) {
            log.error("保存出错答题卡失败：" + e.getMessage());
            return new ResultVo(1, "保存出错答题卡失败");
        }
    }

    //学生试卷作答及得分
    @PostMapping("/spscore")
    public ResultVo getStudentPaperScore(@RequestBody EPaperStudent ps) {
        try {
            N_Utils.validation(new Object[]{ps.getEpsid(),"学生试卷id",1});
            Map map = ePaperStudentService.getStudentPaperScore(ps.getEpsid());
            return new ResultVo(0, "成功获取学生试卷作答及得分",map);
        } catch (Exception e) {
            log.error("获取学生试卷作答及得分失败：" + e.getMessage());
            return new ResultVo(1, "获取学生试卷作答及得分失败");
        }
    }

    //搜索考试学生
    @PostMapping("/searchstu")
    public ResultVo getStuSubjectPaperByname(@RequestBody EPaperStudent ps) {
        try {
            N_Utils.validation(new Object[]{ps.getExamid(),"考试id",1,ps.getSubjectid(),"科目id",3});
            List<Map> rtnlst = ePaperStudentService.getStuSubjectPaperByname(ps);
            return new ResultVo(0, "成功搜索学生",rtnlst);
        } catch (Exception e) {
            log.error("搜索学生失败：" + e.getMessage());
            return new ResultVo(1, "搜索学生失败");
        }
    }

    /**
     * 试卷确认 (因为图像不完整时未解析出试卷，该接口未调动)
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [ps]
     * @Author ywx
     * @Date 2022/8/19 15:31
     **/
    @PostMapping("/sjqr")
    @Action("考试测评-试卷确认")
    public ResultVo sjConfirm(@RequestBody PaperStudentWebVo ps) {
        N_Utils.validation(new Object[]{ps.getBatchid(), "批次id", 1, ps.getSerrorid(), "扫描出错id", 1});
        try {
            ePaperStudentService.sjKhConfirm(ps);
            return new ResultVo(0, "试卷确认成功");
        } catch (Exception e) {
            log.error("试卷确认失败：" + e.getMessage());
            return new ResultVo(1, "试卷确认失败");
        }
    }

    /**
     * 考号确认
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [ps]
     * @Author ywx
     * @Date 2022/8/19 17:51
     **/
    @PostMapping("/khqr")
    @Action("考试测评-考号确认")
    public ResultVo khConfirm(@RequestBody PaperStudentWebVo ps) {
        N_Utils.validation(new Object[]{ ps.getSerrorid(), "扫描出错id", 1
                , ps.getEpid(), "试卷id", 1, ps.getExamno(), "考号", 2});
        try {
            ePaperStudentService.sjKhConfirm(ps);
            return new ResultVo(0, "考号确认成功");
        } catch (Exception e) {
            log.error("考号确认失败：" + e.getMessage());
            return new ResultVo(1, "考号确认失败");
        }
    }

    /**
     * 考号重复确认
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [ps]
     * @Author ywx
     * @Date 2022/8/22 10:32
     **/
    @PostMapping("/khcfqr")
    @Action("考试测评-考号重复确认")
    public ResultVo khcfConfirm(@RequestBody PaperStudentWebVo ps) {
        N_Utils.validation(new Object[]{1, ps.getSerrorid(), "扫描出错id", 1
                , ps.getEpid(), "试卷id", 1, ps.getExamno(), "考号", 2});
        try {
            ePaperStudentService.khcfConfirm(ps);
            return new ResultVo(0, "考号重复确认成功");
        } catch (Exception e) {
            log.error("考号重复确认失败：" + e.getMessage());
            return new ResultVo(1, "考号重复确认失败");
        }
    }

    /**
     * 缺考确认
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [list]
     * @Author ywx
     * @Date 2022/8/22 10:47
     **/
    @PostMapping("/qkqr")
    @Action("考试测评-缺考确认")
    public ResultVo qkConfirm(@RequestBody List<PaperStudentWebVo> list) {
        if (N_Utils.isListEmpty(list)) return new ResultVo(1, "处理数据不能为空");
        for (PaperStudentWebVo ps : list) {
            N_Utils.validation(new Object[]{ps.getSerrorid(), "扫描出错id", 1});
        }
        try {
            ePaperStudentService.qkConfirm(list);
            return new ResultVo(0, "缺考确认成功");
        } catch (Exception e) {
            log.error("缺考确认失败：" + e.getMessage());
            return new ResultVo(1, "缺考确认失败");
        }
    }

    /*
     * 获取考试剩余时间（秒）
     * @Param [stu]
     * @Author ywx
     * @Date 2022/11/11 10:40
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/get_endtime")
    public ResultVo getEndTime(@RequestBody EPaperStudent stu) {
        Integer esid = stu.getEsid();
        Integer studentid = stu.getStudentid();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, studentid, "学生id", 1});
        Integer timestamp = ePaperStudentService.getEndTime(esid, studentid);
        return new ResultVo(0, "获取考试剩余时间", timestamp);
    }
}
