package com.xhkjedu.sstudy.controller.question;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sstudy.model.question.TQuestion;
import com.xhkjedu.sstudy.model.question.TQuestionClass;
import com.xhkjedu.sstudy.model.question.TQuestionExplain;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.service.question.QuestionClassService;
import com.xhkjedu.sstudy.service.subjectbook.DirectorService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.sstudy.vo.question.QuestionClassDetailVo;
import com.xhkjedu.sstudy.vo.question.QuestionClassVo;
import com.xhkjedu.sstudy.vo.subjectbook.SubjectQuestiontypeVo;
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;

/**
 * @author ywx
 * @classname QuestionClassController
 * @description 
 * @date 2020/8/19 8:54
 **/
@RestController
@RequestMapping("/question_class")
public class QuestionClassController {
    @Resource
    private QuestionClassService questionClassService;
    @Resource
    private DirectorService directorService;

    /*@PostMapping("/end_correct")
    public ResultVo endCorrect(@RequestBody TQuestionClass question) {
        N_Utils.validation(new Object[]{question.getPaperid(),"作业id",1
                ,question.getClassid(),"班级id",1,question.getQctype(),"试卷类型",1});
        questionClassService.endCorrect(question);
        return new ResultVo(0,"批阅完成");
    }*/

    @PostMapping("/director_list")
    public ResultVo directorList(@RequestBody TQuestionClass question) {
        Integer page = question.getPage();
        Integer pageSize = question.getPageSize();
        String lsbid = question.getLsbid();
        Integer classid = question.getClassid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1
                ,lsbid,"教材关联id",2,classid,"班级id",1});

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

        PageHelper.startPage(page, pageSize);
        List<QuestionClassVo> list = questionClassService.directorList(question);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取章节班级错题列表成功",pageResult);
    }

    @PostMapping("/point_list")
    public ResultVo pointList(@RequestBody TQuestionClass question) {
        Integer page = question.getPage();
        Integer pageSize = question.getPageSize();
        String subjectid = question.getSubjectid();
        Integer classid = question.getClassid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1
                ,subjectid,"科目id",2,classid,"班级id",1});
        PageHelper.startPage(page, pageSize);
        List<QuestionClassVo> list = questionClassService.pointList(question);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取章节班级错题列表成功",pageResult);
    }

    //添加试题讲解
    @PostMapping("/add_explain")
    public ResultVo addExplain(@RequestBody TQuestionExplain explain) {
        N_Utils.validation(new Object[]{explain.getQuestionid(),"试题id",2
                ,explain.getQctype(),"试题类型",1,explain.getFilepath(),"文件地址",2});
        questionClassService.addExplain(explain);
        return new ResultVo(0, "保存成功");
    }

    //更改试题讲解观看次数
    @PostMapping("/read_explain")
    public ResultVo readExplain(@RequestBody TQuestionExplain explain) {
        N_Utils.validation(new Object[]{explain.getQcpid(),"错题讲解id",1});
        int readCount = questionClassService.updateReadCount(explain.getQcpid());
        return new ResultVo(0,"更新错题讲解播放次数成功",readCount);
    }

    @PostMapping("/update_mp4code")
    public ResultVo updateMp4code(@RequestBody TQuestionExplain explain) {
        N_Utils.validation(new Object[]{explain.getQcpid(),"讲解id",1
                ,explain.getMp4code(),"mp4文件编码",2,explain.getFilepath(),"文件地址",2});
        questionClassService.updateMp4code(explain);
        return new ResultVo(0,"修改成功");
    }

    @PostMapping("/list_qtype")
    public ResultVo listQtype(@RequestBody TQuestionClass question) {
        String subjectid = question.getSubjectid();
        Integer classid = question.getClassid();
        N_Utils.validation(new Object[]{subjectid,"科目id",2,classid,"班级id",1});
        List<SubjectQuestiontypeVo> list = questionClassService.listQtype(question);
        return new ResultVo(0,"获取题型列表成功",list);
    }

    @PostMapping("/objective_detail")
    public ResultVo objectiveDetail(@RequestBody TQuestionClass question) {
        Integer qctype = question.getQctype();
        Integer qcid = question.getQcid();
        N_Utils.validation(new Object[]{qctype,"试题类型",1,qcid,"班级错题id",1});
        QuestionClassDetailVo detail = questionClassService.objectiveDetail(question);
        return new ResultVo(0,"获取客观题详情成功",detail);
    }

    @PostMapping("/subjective_detail")
    public ResultVo subjectiveDetail(@RequestBody TQuestionClass question) {
        Integer qctype = question.getQctype();
        Integer qcid = question.getQcid();
        N_Utils.validation(new Object[]{qctype,"试题类型",1,qcid,"班级错题id",1});
        QuestionClassDetailVo detail = questionClassService.subjectiveDetail(question);
        return new ResultVo(0,"获取主观题详情成功",detail);
    }

    @PostMapping("/handle_exp")
    public ResultVo handleExplain() {
        questionClassService.handleExplain();
        return new ResultVo(0,"处理视频时长成功");
    }

    //试题讲解的所有视频
    @PostMapping("/qexplains")
    public ResultVo questionExplains(@RequestBody TQuestionExplain question) {
        Integer qctype = question.getQctype();
        String questionid = question.getQuestionid();
        N_Utils.validation(new Object[]{questionid,"试题id",2,qctype,"试题类型",1});
        List<TQuestionExplain> list = questionClassService.listExplainsByQuestionid(question);
        return new ResultVo(0,"获取试题讲解成功",list);
    }

    //举一反三试题
    @PostMapping("/list_jyfsquestion")
    public ResultVo listJYFSQuestion(@RequestBody TQuestion question) {
        String questionid = question.getQuestionid();
        Integer userid = question.getCreateid();
        Integer schoolid = question.getSchoolid();
        N_Utils.validation(new Object[]{questionid,"试题id",2,userid,"用户id",1,schoolid,"学校id",1});
        List<Map> list = questionClassService.listJYFSQuestion(questionid,userid,schoolid);
        return new ResultVo(0,"获取举一反三试题列表成功",list);
    }

    /*
     * 复合题详情
     * @Param [question]
     * @Author ywx
     * @Date 2022/10/19 11:30
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/complex_detail")
    public ResultVo complexDetail(@RequestBody TQuestionClass question) {
        N_Utils.validation(new Object[]{question.getQcid(), "班级错题id", 1});
        QuestionClassDetailVo detail = questionClassService.complexDetail(question);
        return new ResultVo(0, "获取复合题详情成功", detail);
    }
}
