package com.xhkjedu.sexam.controller.paper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sexam.model.exam.ESubject;
import com.xhkjedu.sexam.model.paper.EPaperCorrectquestion;
import com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion;
import com.xhkjedu.sexam.service.paper.EPaperCorrectTeacherService;
import com.xhkjedu.sexam.vo.paper.EPTeacherVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
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;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author ywx
 * @classname EPaperCorrectTeacherController
 * @description 教师阅卷
 * @date 2022/7/27 11:24
 **/
@RestController
@RequestMapping("/ept")
public class EPaperCorrectTeacherController {
    @Resource
    private EPaperCorrectTeacherService correctTeacherService;

    /**
     * 待阅卷列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/27 14:07
     **/
    @PostMapping("/list_pending")
    public ResultVo listPending(@RequestBody ESubject vo) {
        Integer teacherid = vo.getTeacherid();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1});
        List<Map> list = correctTeacherService.listPending(teacherid);
        return new ResultVo(0, "获取成功", list);
    }

    /**
     * @Description 获取待阅卷数量
     * @Date 2024/9/18 10:35
     * @Author YWX
     * @Param [vo]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/get_pending_num")
    public ResultVo getPendingNum(@RequestBody ESubject vo) {
        Integer teacherid = vo.getTeacherid();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1});
        return new ResultVo(0, "获取成功", correctTeacherService.getPendingNum(teacherid));
    }

    /**
     * 已阅卷列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/27 14:18
     **/
    @PostMapping("/list_marked")
    public ResultVo listMarked(@RequestBody ESubject vo) {
        Integer teacherid = vo.getTeacherid();
        Integer page = vo.getPage();
        Integer pageSize = vo.getPageSize();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = correctTeacherService.listMarked(teacherid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * 随机获取待批试题
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/27 16:21
     **/
    @PostMapping("/get_cq")
    public ResultVo getCorrectQuestion(@RequestBody EPTeacherVo vo) {
        Integer teacherid = vo.getTeacherid();
        Integer eptqid = vo.getEptqid();
        Integer esid = vo.getEsid();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1});
        return correctTeacherService.getCorrectQuestion(teacherid, eptqid, vo.getClassids(), esid, vo.getEpsqid(), vo.getEptqids());
    }

    /**
     * 获取上一份批阅试题
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/27 16:33
     **/
    @PostMapping("/get_pre")
    public ResultVo getPreQuestion(@RequestBody EPTeacherVo vo) {
        Integer eptqid = vo.getEptqid();
        Integer teacherid = vo.getTeacherid();
        Integer esid = vo.getEsid();
        N_Utils.validation(new Object[]{eptqid, "试卷试题id", 1, teacherid, "教师id", 1, esid, "考试科目id", 1});
        return correctTeacherService.getPreQuestion(eptqid, teacherid, esid, vo.getClassids(), vo.getEpsqid());
    }

    /**
     * 阅卷记录
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/27 16:41
     **/
    @PostMapping("/list_jl")
    public ResultVo listRecord(@RequestBody EPaperCorrectquestion vo) {
        Integer teacherid = vo.getTeacherid();
        Integer eptqid = vo.getEptqid();
        Integer page = vo.getPage();
        Integer pageSize = vo.getPageSize();
        N_Utils.validation(new Object[]{eptqid, "试卷试题id", 1, teacherid, "教师id", 1
                , page, "页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = correctTeacherService.listRecord(eptqid, teacherid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * 批阅试题
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/28 15:30
     **/
    @PostMapping("/correct")
    public ResultVo correctQuestion(@RequestBody EPaperStudentQuestion vo) throws ExecutionException, InterruptedException {
        Integer epsid = vo.getEpsid();
        Integer epsqid = vo.getEpsqid();
        Integer checkid = vo.getCheckid();
        N_Utils.validation(new Object[]{epsid, "试卷学生id", 1, epsqid, "试卷学生试题id", 1, checkid, "批阅人", 1});
        Double qscore = vo.getQscore();
        if (qscore != null && qscore.compareTo(vo.getStuscore()) == -1) {
            return new ResultVo(1, "学生得分不能大于满分");
        }
        Future<ResultVo> future = correctTeacherService.correctQuestion(vo);
        return future.get();
    }

    /**
     * 阅卷进度
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/8/15 9:07
     **/
    @PostMapping("/mark_pro")
    public ResultVo markProgress(@RequestBody EPaperStudentQuestion vo) throws ExecutionException, InterruptedException {
        Integer examid = vo.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = correctTeacherService.markProgress(examid);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * 随机获取待批试题2
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/9/23 15:02
     **/
    @PostMapping("/get_cq2")
    public ResultVo getCorrectQuestion2(@RequestBody EPTeacherVo vo) {
        Integer teacherid = vo.getTeacherid();
        Integer esid = vo.getEsid();
        List<Integer> eptqids = vo.getEptqids();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, esid, "考试科目id", 1});
        if (N_Utils.isListEmpty(eptqids)) return new ResultVo(1, "考试试题ids不能为空");
        return correctTeacherService.getCorrectQuestion2(teacherid, esid, eptqids, vo.getClassids());
    }

    /*
     * 取消试题锁定
     * @Param [vo]
     * @Author ywx
     * @Date 2022/10/23 12:11
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/un_laq")
    public ResultVo unLockAllQuestion(@RequestBody EPTeacherVo vo) {
        Integer teacherid = vo.getTeacherid();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1});
        correctTeacherService.unLockAllQuestion(teacherid);
        return new ResultVo(0, "取消试题锁定成功");
    }
}
