package com.xhkjedu.sstudy.controller.question;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sstudy.model.question.TQuestionStudent;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.service.question.QuestionStudentService;
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.QuestionStudentVo;
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;

/**
 * @author ywx
 * @classname QuestionStudentController
 * @description 
 * @date 2020/6/11 14:11
 **/
@RestController
@RequestMapping("/qs")
public class QuestionStudentController {
    @Resource
    private QuestionStudentService questionStudentService;
    @Resource
    private DirectorService directorService;

    @PostMapping("/list")
    public ResultVo list(@RequestBody TQuestionStudent questionStudent) {
        Integer page = questionStudent.getPage();
        Integer pageSize = questionStudent.getPageSize();
        Integer studentid = questionStudent.getStudentid();
        String subjectid = questionStudent.getSubjectid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,studentid,"学生id",1,subjectid,"科目id",2});

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

        questionStudent.setPage((page - 1)*pageSize);
        List<QuestionStudentVo> list = questionStudentService.findAll(questionStudent);
        return new ResultVo(0,"获取错题列表成功",list);
    }

    @PostMapping("/delete")
    public ResultVo delete(@RequestBody TQuestionStudent questionStudent) {
        N_Utils.validation(new Object[]{questionStudent.getQsid(),"学生错题id",1});
        questionStudentService.deleteById(questionStudent.getQsid());
        return new ResultVo(0,"删除成功");
    }

    @PostMapping("/submit_question")
   public ResultVo submitQuestion(@RequestBody TQuestionStudent qs) {
        Integer qsid = qs.getQsid();
        Integer corrected = qs.getCorrected();
        N_Utils.validation(new Object[]{qsid,"学生错题id",1,corrected,"作答结果",1});
        questionStudentService.submitQuestion(qsid, corrected);
        return new ResultVo(0, "提交成功！");
    }

    @PostMapping("/list_qtype")
    public ResultVo listQtype(@RequestBody TQuestionStudent qs) {
        String subjectid = qs.getSubjectid();
        Integer studentid = qs.getStudentid();
        N_Utils.validation(new Object[]{subjectid,"科目id",2,studentid,"学生id",1});
        List<SubjectQuestiontypeVo> list = questionStudentService.listQtype(qs);
        return new ResultVo(0,"获取题型列表成功",list);
    }


    //////////////////////////学生错题集网页端 start//////////////////////////////
    @PostMapping("/qsw_list")
    public ResultVo list_stuweb(@RequestBody TQuestionStudent question) {
        Integer page = question.getPage();
        Integer pageSize = question.getPageSize();
        Integer studentid = question.getStudentid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,studentid,"学生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<QuestionStudentVo> list = questionStudentService.findAllStuWeb(question);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取错题列表成功",pageResult);
    }

    @PostMapping("/qsw_detail")
    public ResultVo detail_stuweb(@RequestBody TQuestionStudent student) {
        String questionid = student.getQuestionid();
        Integer qctype = student.getQctype();
        Integer studentid = student.getStudentid();
        N_Utils.validation(new Object[]{questionid,"试题id",2,qctype,"试题类型",1,student,"学生id",1});
        QuestionStudentVo question = questionStudentService.detailStuWeb(questionid, qctype, studentid);
        return new ResultVo(0,"获取错题详情成功",question);
    }
    //////////////////////////学生错题集网页端 end//////////////////////////////
}
