package com.xhkjedu.sstudy.controller.paperstudent;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudent;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentBack;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentQuestion;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentScantron;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.service.paperstudent.PaperStudentService;
import com.xhkjedu.sstudy.service.subjectbook.DirectorService;
import com.xhkjedu.sstudy.vo.paperstudent.*;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PaperStudentController
 * Description 作业--学生
 * Author WN
 * Date 2020/6/9 10:58
 **/
@RestController
@RequestMapping("/ps")
public class PaperStudentController {
    @Resource
    private PaperStudentService paperStudentService;
    @Resource
    private DirectorService directorService;


    /**
     *功能描述 学生作业列表
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/list")
    public ResultVo list(@RequestBody PaperSearchVo paperSearch) {
        Integer page = paperSearch.getPage();
        Integer pageSize = paperSearch.getPageSize();
        Integer studentid = paperSearch.getStudentid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,studentid,"学生id",1});

        //获取所选章节信息
        if(N_Utils.isNotEmpty(paperSearch.getDirectorid()) && !"0".equals(paperSearch.getDirectorid())){
            TDirector director = directorService.getDirectorById(paperSearch.getDirectorid());
            paperSearch.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                paperSearch.setDirectorall(director.getDirectorall());
            }
        }

        paperSearch.setPage((page - 1)*pageSize);
        List<PaperStudentVo> list = paperStudentService.findAll(paperSearch);
        return new ResultVo(0,"获取列表成功",list);
    }

    /**
     *功能描述 题库作业--开始做作业(兼容靶向作业)
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/start_paper")
    public ResultVo startPaper(@RequestBody TPaperStudent paperStudent) {
        Integer psid = paperStudent.getPsid();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1});
        ResultVo vo = paperStudentService.checkPsStateByPsId(psid);
        if (vo != null) return vo;
        List<PaperStudentQuestionVo> paperQuestionBeans = paperStudentService.startPaper(psid);
        return new ResultVo(0,"开始成功！",paperQuestionBeans);
    }

    /**
     *功能描述 题库作业--学生作答详情
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/paper_question")
    public ResultVo getPaperQuestion(@RequestBody TPaperStudent paperStudent) {
        Integer psid = paperStudent.getPsid();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1});
        List<PaperStudentQuestionVo> paperQuestions = paperStudentService.getPaperQuestion(psid);
        return new ResultVo(0,"获取学生作业对应试题集合根据题号排序成功！",paperQuestions);
    }

    /**
     * 获取学生待批改试题列表
     * @Param [paperStudent]
     * @Author ywx
     * @Date 2020/6/26 13:22
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_correct_question")
    public ResultVo listCorrectQuestion(@RequestBody TPaperStudent paperStudent) {
        Integer psid = paperStudent.getPsid();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1});
        //根据学生作业id获取作业是否完成批阅 2021/8/20 22:05已结束批阅的还可以批阅
        /*Integer checked = paperStudentService.getCheckedByPsId(psid);
        if (checked == 1){
            return new ResultVo(1,"已结束批阅");
        }*/
        List<PaperStudentQuestionVo> paperQuestions = paperStudentService.listCorrectQuestion(psid);
        return new ResultVo(0,"获取学生待批改试题列表成功！",paperQuestions);
    }

    /**
     *功能描述 题库作业--提交单题
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/submit_question")
    public ResultVo submitQuestion(@RequestBody TPaperStudentQuestion psq) {
        N_Utils.validation(new Object[]{psq.getPsqid(),"试卷学生试题id",1});
        return paperStudentService.submitQuestion(psq);
    }

    /**
     *功能描述 题库作业--提交作业
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/submit_paper")
    @Action("作业管理-提交题库作业")
    public ResultVo submitPaper(@RequestBody TPaperStudent ps) {
        N_Utils.validation(new Object[]{ps.getPsid(),"试卷学生id",1});
        return paperStudentService.submitPaper(ps.getPsid());
    }

    /**
     *功能描述 题库作业--批阅单人作业
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/correct")
    @Action("作业管理-批阅题库作业")
    public ResultVo correct(@RequestBody TPaperStudent ps) {
        Integer psid = ps.getPsid();
        Integer checkid = ps.getCreateid();
        String questionResult = ps.getQuestionResult();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1,checkid,"批改人id",1,questionResult,"批改结果",2});
        try {
            paperStudentService.correct(psid , checkid, questionResult);
            return new ResultVo(0, "批改成功");
        } catch (Exception e) {
            return new ResultVo(1, "批改失败");
        }
        /*if (result > 0) return new ResultVo(0, "批改成功！",0);
        else return new ResultVo(0, "批改完成",1);*/
    }

    /**
     * 批阅列表
     * @Param [ps]
     * @Author ywx
     * @Date 2020/6/18 14:35
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/correct_list")
    public ResultVo correctList(@RequestBody TPaperStudent ps) {
        Integer checkid = ps.getCheckid();
        Integer page = ps.getPage();
        Integer pageSize = ps.getPageSize();
        N_Utils.validation(new Object[]{checkid,"批阅人id",1,page,"显示页码",1,pageSize,"显示条数",1});
        ps.setPage((page - 1)*pageSize);
        List<Map> list = paperStudentService.correctList(ps);
        return new ResultVo(0, "获取学生批阅列表成功！",list);
    }

    /**
     * @Description 批阅学生列表
     * @Date 2023/12/19 16:14
     * @Author YWX
     * @Param [ps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_stu")
    public ResultVo listCorrectStu(@RequestBody TPaperStudent ps) {
        N_Utils.validation(new Object[]{ps.getCheckid(), "批阅人id", 1, ps.getPaperid(), "作业id", 1, ps.getClassid(), "班级id", 1});
        List<Map> list = paperStudentService.listCorrectStu(ps);
        return new ResultVo(0, "获取批阅学生列表成功！", list);
    }

    /**
     * 题库作业--按题批阅列表
     * @Param [ps]
     * @Author ywx
     * @Date 2021/10/14 14:12
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/qcorrect_list")
    public ResultVo qcorrectList(@RequestBody TPaperStudent ps) {
        Integer paperid = ps.getPaperid();
        Integer classid = ps.getClassid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,classid,"班级id",1});
        List<Map> map = paperStudentService.qcorrectList(paperid, classid);
        return new ResultVo(0, "获取按题批阅列表成功！",map);
    }

    /**
     * 题库作业--按题批阅
     * @Param [ps]
     * @Author ywx
     * @Date 2021/10/14 14:48
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/qcorrect")
    public ResultVo qcorrect(@RequestBody TPaperStudentQuestion psq) {
        N_Utils.validation(new Object[]{psq.getPsqid(),"试卷学生试题id",1,psq.getCheckid(),"批改人id",1
                ,psq.getPsid(),"试卷学生id",1});
        try {
            paperStudentService.qcorrect(psq);
            return new ResultVo(0, "批改成功");
        } catch (Exception e) {
            return new ResultVo(1, "批改失败");
        }
    }

    /**
     * 作业说明
     * @Param [ps]
     * @Author ywx
     * @Date 2020/6/18 17:10
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/paper_comm")
    public ResultVo getPaperComm(@RequestBody TPaperStudent ps) {
        Integer psid = ps.getPsid();
        N_Utils.validation(new Object[]{psid,"作业学生id",1});
        PaperCommVo comm = paperStudentService.getPaperComm(psid);
        return new ResultVo(0, "获取作业说明成功！",comm);
    }

    /**
     *功能描述 保存教师批注
     * @author WN
     * @date 2020/8/24
     * @param  * @param ps
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/question_mark")
    public ResultVo userAnswerMark(@RequestBody TPaperStudentQuestion ps) {
        Integer psqid = ps.getPsqid();
        String answers = ps.getUseranswer();
        N_Utils.validation(new Object[]{psqid,"学生试题id",1,answers,"学生作答内容",2});
        paperStudentService.updateUseranswer(answers,psqid);
        return new ResultVo(0, "保存成功");
    }



    //////////////////答题卡（图片作业）start/////////////////////////
    /**
     *功能描述 图片作业--开始做作业
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/sstart_paper")
    public ResultVo scantronStartPaper(@RequestBody TPaperStudent paperStudent) {
        Integer psid = paperStudent.getPsid();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1});
        ResultVo vo = paperStudentService.checkPsStateByPsId(psid);
        if (vo != null) return vo;
        List<Map> psscanlst = paperStudentService.scantronStartPaper(psid);
        return new ResultVo(0,"开始成功！",psscanlst);
    }

    /**
     *功能描述 图片作业--学生作答详情
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/spaper_question")
    public ResultVo ScantronPaperQuestion(@RequestBody TPaperStudent paperStudent) {
        Integer psid = paperStudent.getPsid();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1});
        List<PaperStudentScantronVo> paperQuestions = paperStudentService.listPaperStudentScantrons(psid);
        return new ResultVo(0,"获取学生作业对应试题集合根据题号排序成功！",paperQuestions);
    }

    @PostMapping("/slist_correct_question")
    public ResultVo sListCorrectQuestion(@RequestBody TPaperStudent paperStudent) {
        Integer psid = paperStudent.getPsid();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1});
        //根据学生作业id获取作业是否完成批阅 2021/8/20 22:05已结束批阅的还可以批阅
        /*Integer checked = paperStudentService.getCheckedByPsId(psid);
        if (checked == 1){
            return new ResultVo(1,"已结束批阅");
        }*/
        List<PaperStudentScantronVo> paperQuestions = paperStudentService.sListCorrectQuestion(psid);
        return new ResultVo(0,"获取学生答题卡待批改试题列表成功！",paperQuestions);
    }

    /**
     *功能描述 图片作业--提交单题
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/ssubmit_question")
    public ResultVo scantronSubmitQuestion(@RequestBody TPaperStudentScantron pss) {
        N_Utils.validation(new Object[]{pss.getPssid(),"试卷学生试题id",1});
        return paperStudentService.scantronSubmitQuestion(pss);
    }
    /**
     *功能描述 图片作业--提交作业
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/ssubmit_paper")
    @Action("作业管理-提交附件作业")
    public ResultVo scantronSubmitPaper(@RequestBody TPaperStudent ps) {
        N_Utils.validation(new Object[]{ps.getPsid(),"试卷学生id",1});
        return paperStudentService.scantronSubmitPaper(ps.getPsid());
    }

    /**
     *功能描述 图片作业--批阅单人
     * @date 2020/6/12
     * @param  * @param paperSearch
     */
    @PostMapping("/scorrect")
    @Action("作业管理-批阅附件作业")
    public ResultVo scantronCorrect(@RequestBody TPaperStudent ps) {
        Integer psid = ps.getPsid();
        Integer checkid = ps.getCreateid();
        String questionResult = ps.getQuestionResult();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1,checkid,"批改人id",1,questionResult,"批改结果",2});
        try {
            paperStudentService.scantronCorrect(psid , checkid, questionResult);
            return new ResultVo(0, "批改成功");
        } catch (Exception e) {
            return new ResultVo(1, "批改失败");
        }
    }

    /**
     * 附件作业--按题批阅列表
     * @Param [ps]
     * @Author ywx
     * @Date 2021/10/14 14:12
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/sqcorrect_list")
    public ResultVo sqcorrectList(@RequestBody TPaperStudent ps) {
        Integer paperid = ps.getPaperid();
        Integer classid = ps.getClassid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,classid,"班级id",1});
        List<Map> map = paperStudentService.sqcorrectList(paperid, classid);
        return new ResultVo(0, "获取按题批阅列表成功！",map);
    }

    /**
     * 图片作业--按题批阅
     * @Param [ps]
     * @Author ywx
     * @Date 2021/10/14 14:48
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/sqcorrect")
    public ResultVo sqcorrect(@RequestBody TPaperStudentScantron pss) {
        N_Utils.validation(new Object[]{pss.getPssid(),"试卷学生试题id",1,pss.getCheckid(),"批改人id",1
                ,pss.getPsid(),"试卷学生id",1});
        try {
            paperStudentService.sqcorrect(pss);
            return new ResultVo(0, "批改成功");
        } catch (Exception e) {
            return new ResultVo(1, "批改失败");
        }
    }

    /**
     *功能描述 保存教师批注
     * @author WN
     * @date 2020/8/24
     * @param  * @param ps
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/squestion_mark")
    public ResultVo userAnswerScantronMark(@RequestBody TPaperStudentScantron ps) {
        Integer pssid = ps.getPssid();
        String answers = ps.getUseranswer();
        N_Utils.validation(new Object[]{pssid,"学生试题id",1,answers,"学生作答内容",2});
        paperStudentService.updateScantronUseranswer(answers,pssid);
        return new ResultVo(0, "保存成功");
    }

    //////////////////////////////////答题卡（图片作业）end///////////////////////////////

    //学生作业退回
    @PostMapping("/back_paper")
    @Action("作业管理-退回作业")
    public ResultVo backPaper(@RequestBody TPaperStudentBack psb) {
        N_Utils.validation(new Object[]{psb.getPsid(),"试卷学生id",1});
        return paperStudentService.backPaper(psb);
    }

    //获取学生作业状态
    @PostMapping("/paper_state")
    public ResultVo getPaperState(@RequestBody TPaperStudent ps) {
        N_Utils.validation(new Object[]{ps.getPsid(), "试卷学生id", 1});
        Map paperStudent = paperStudentService.getPaperState(ps.getPsid());
        return new ResultVo(0, "获取学生作业状态成功", paperStudent);
    }

    /*
    * @Description 取消优秀作答
    * @Author WN
    * @Date 2023/6/8 17:42:23
    */
    @PostMapping("/delgood")
    public ResultVo updateStudentQuestionGood(@RequestBody PaperStudentQuestionVo ps) {
        Integer papertype = ps.getPapertype();
        Integer userid = ps.getUserid();
        N_Utils.validation(new Object[]{papertype, "作业类型", 1, userid, "用户id", 1});
        if (papertype == 2) {
            N_Utils.validation(new Object[]{ps.getPsqid(), "学生试题id", 1});
        } else {
            N_Utils.validation(new Object[]{ps.getPsid(), "试卷学生id", 1, ps.getQuestionpid(), "母题id", 2});
        }
        Integer checker = paperStudentService.isChecker(ps);
        if (checker == 0) return new ResultVo(1, "没有取消权限");
        paperStudentService.updatePaperStudentQuesGood(ps);
        return new ResultVo(0, "取消学生试题优秀作答成功");
    }



    /*
    * @Description 处理作业中学生和试题的关联关系（未作答学生未关联）
    * @Author WN
    * @Date 2023/4/27 10:57:17
    */
    @GetMapping("/chandlesq")
    public ResultVo chandleStuQues() {
        paperStudentService.chandlePaperStuQues();
        return new ResultVo(0, "成功处理学生试题关系");
    }

}
