package com.xhkjedu.sstudy.controller.paperstudent;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudent;
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.paperstudent.PaperStudentWebService;
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.paperstudent.PaperSearchVo;
import com.xhkjedu.sstudy.vo.paperstudent.PaperStudentVo;
import com.xhkjedu.sstudy.vo.paperstudent.PaperStudentWebVo;
import com.xhkjedu.sstudy.vo.paperstudent.StudentPaperVo;
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 PaperStudentWebController
 * @description 
 * @date 2020/9/3 9:30
 **/
@RestController
@RequestMapping("/psw")
public class PaperStudentWebController {
    @Resource
    private PaperStudentWebService paperStudentWebService;
    @Resource
    private PaperStudentService paperStudentService;
    @Resource
    private DirectorService directorService;

    /**
     * 学生作业列表
     * @Param [paperSearch]
     * @Author ywx
     * @Date 2020/9/3 10:52
     * @return com.xhkjedu.vo.ResultVo
     **/
    @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());
            }
        }
        PageHelper.startPage(page, pageSize);
        List<PaperStudentVo> list = paperStudentWebService.listWeb(paperSearch);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取作业列表成功",pageResult);
    }

    /**
     * 题库作业--开始做作业 (兼容题库作业和靶向作业)
     * @Param [paperStudent]
     * @Author ywx
     * @Date 2020/9/3 11:37
     * @return com.xhkjedu.vo.ResultVo
     **/
    @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;
        StudentPaperVo paper = paperStudentWebService.startPaper(psid);
        return new ResultVo(0,"开始成功！",paper);
    }

    /**
     * 答题卡作业--开始做作业
     * @Param [paperStudent]
     * @Author ywx
     * @Date 2020/9/3 11:37
     * @return com.xhkjedu.vo.ResultVo
     **/
    @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;
        StudentPaperVo paper = paperStudentWebService.scantronStartPaper(psid);
        return new ResultVo(0,"开始成功！",paper);
    }

    /**
     * 题库作业--提交作业
     * @Param [ps]
     * @Author ywx
     * @Date 2020/9/3 11:38
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/submit_paper")
    public ResultVo submitPaper(@RequestBody PaperStudentWebVo paper) {
        List<TPaperStudentQuestion> psqs = paper.getPsqs();
        if (N_Utils.isListEmpty(psqs)){
            return new ResultVo(1, "作答详情不能为空");
        }
        return paperStudentWebService.submitPaper(paper);
    }

    @PostMapping("/ssubmit_paper")
    public ResultVo scantronSubmitPaper(@RequestBody PaperStudentWebVo paper) {
        List<TPaperStudentScantron> pss = paper.getPss();
        if (N_Utils.isListEmpty(pss)){
            return new ResultVo(1, "作答详情不能为空");
        }
        return paperStudentWebService.scantronSubmitPaper(paper);
    }

    //学生作业详情——答题卡作业
    @PostMapping("/spaper_question")
    public ResultVo ScantronPaperQuestion(@RequestBody TPaperStudent paperStudent) {
        Integer psid = paperStudent.getPsid();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1});
        StudentPaperVo paper = paperStudentWebService.listPaperStudentScantrons(psid);
        return new ResultVo(0,"获取附件作业详情成功！",paper);
    }

    @PostMapping("/paper_question")
    public ResultVo paperQuestion(@RequestBody TPaperStudent paperStudent) {
        Integer psid = paperStudent.getPsid();
        N_Utils.validation(new Object[]{psid,"试卷学生id",1});
        StudentPaperVo paper = paperStudentWebService.listPaperStudent(psid);
        return new ResultVo(0,"获取题库作业详情成功！",paper);
    }
}
