package com.xhkjedu.sstudy.controller.paper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sstudy.model.paper.TPaper;
import com.xhkjedu.sstudy.model.paperstudent.TPaperClass;
import com.xhkjedu.sstudy.model.question.TQuestionClass;
import com.xhkjedu.sstudy.service.paper.PaperTeaAppService;
import com.xhkjedu.sstudy.vo.paper.PQuestionVo;
import com.xhkjedu.sstudy.vo.paper.PStudentVo;
import com.xhkjedu.sstudy.vo.paper.PaperTeacherAppVo;
import com.xhkjedu.sstudy.vo.paper.PaperTeacherReportVo;
import com.xhkjedu.sstudy.vo.paperstudent.PaperSearchVo;
import com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
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 PaperTeacherAppController
 * @description 
 * @date 2020/10/12 15:08
 **/
@Slf4j
@RestController
@RequestMapping("/paper_teacher")
public class PaperTeaAppController {
    @Resource
    private PaperTeaAppService paperTeaAppService;


    @PostMapping("/list")
    public ResultVo list(@RequestBody PaperSearchVo paperSearch) {
        Integer page = paperSearch.getPage();
        Integer pageSize = paperSearch.getPageSize();
        Integer teacherid = paperSearch.getTeacherid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1
                ,teacherid,"教师id",1,paperSearch.getSchoolid(),"学校id",1});
        //获取当前教师权限班级
        Map classMap = paperTeaAppService.getQxClassidsByTeacherid(teacherid, null);
        String bxclassids = classMap.get("bxclassids").toString();//本校权限班级
        paperSearch.setBxclassids(bxclassids);
        paperSearch.setClassids(classMap.get("classids").toString());

        PageHelper.startPage(page, pageSize);
        List<PaperTeacherAppVo> list = paperTeaAppService.list(paperSearch);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取作业列表成功",pageResult);
    }

    /*
     * @Description 作业列表--我的
     * @Date 2024/3/12 17:28:31
     * @Author WN
     * @Param [paperSearch]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_my")
    public ResultVo listMy(@RequestBody PaperSearchVo paperSearch) {
        Integer page = paperSearch.getPage();
        Integer pageSize = paperSearch.getPageSize();
        Integer teacherid = paperSearch.getTeacherid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1
                ,teacherid,"教师id",1,paperSearch.getSchoolid(),"学校id",1});

        PageHelper.startPage(page, pageSize);
        List<PaperTeacherAppVo> list = paperTeaAppService.listMy(paperSearch);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取作业列表成功",pageResult);
    }

    @PostMapping("/paper_detail")
    public ResultVo paperDetail(@RequestBody TPaperClass pg) {
        Integer paperid = pg.getPaperid();
        Integer checkid = pg.getCheckid();
        Integer schoolid = pg.getSchoolid();
        N_Utils.validation(new Object[]{paperid, "作业id", 1, checkid, "教师id", 1, schoolid, "学校id", 1});
        Map list = paperTeaAppService.getPaperDetail(paperid, checkid, schoolid);
        return new ResultVo(0,"获取作业情况成功",list);
    }

    @PostMapping("/report")
    public ResultVo report(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        Integer papertype = paper.getPapertype();
        Integer classid = paper.getClassid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,papertype,"作业类型",1,classid,"班级id",1});
        List<PaperTeacherReportVo> list = paperTeaAppService.getReport(paper);
        return new ResultVo(0,"获取作业报告成功",list);
    }

    @PostMapping("/wtj_student")
    public ResultVo wtjStudent(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        Integer classid = paper.getClassid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,classid,"班级id",1});
        List<PStudentVo> list = paperTeaAppService.listWtjStudent(paperid, classid);
        return new ResultVo(0,"获取未提交学生列表成功",list);
    }

    @PostMapping("/ytj_student")
    public ResultVo ytjStudent(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        Integer papertype = paper.getPapertype();
        Integer classid = paper.getClassid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,papertype,"作业类型",1,classid,"班级id",1});
        if (N_Utils.isEmpty(paper.getFieldorder())) paper.setFieldorder("desc");
        List<PStudentVo> list = paperTeaAppService.listYtjStudent(paper);
        return new ResultVo(0,"获取已提交学生列表成功",list);
    }

    //学生报告中-学生答题卡
    @PostMapping("/student_scantron")
    public ResultVo studentScantron(@RequestBody PStudentVo paper) {
        try {
            Integer psid = paper.getPsid();
            Integer papertype = paper.getPapertype();
            N_Utils.validation(new Object[]{psid,"作业学生id",1,papertype,"作业类型",1});
            Map map = paperTeaAppService.getStudentScantron(paper);
            return new ResultVo(0,"获取学生答题卡成功",map);
        }catch (Exception e){
            log.error("获取学生答题卡出错："+e.getMessage());
            return new ResultVo(1,"获取学生答题卡失败");
        }
    }

    @PostMapping("/student_question")
    public ResultVo studentQuestion(@RequestBody PaperStudentQuestionVo paper) {
        Integer psid = paper.getPsid();
        Integer qorder = paper.getQorder();
        N_Utils.validation(new Object[]{psid,"作业学生id",1,qorder,"题号",1});
        PaperStudentQuestionVo question = paperTeaAppService.getStudentQuestion(paper);
        return new ResultVo(0,"获取学生答题卡单题详情成功",question);
    }

    //作业统计单题统计
    @PostMapping("/question_detail")
    public ResultVo questionDetail(@RequestBody TQuestionClass question) {
        Long paperid = question.getPaperid();
        Integer qorder = question.getQorder();
        Integer classid = question.getClassid();
        Integer qctype = question.getQctype();
        N_Utils.validation(new Object[]{paperid, "作业id", 1, qorder, "题号", 1, classid, "班级id", 1, qctype, "试题类型", 1});
        PQuestionVo questionVo = paperTeaAppService.getQuestionDetail(question);
        return new ResultVo(0, "获取试题详情成功", questionVo);
    }

    @PostMapping("/paper_student")
    public ResultVo paperStudent(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        Integer classid = paper.getClassid();
        N_Utils.validation(new Object[]{paperid, "作业id", 1, classid, "班级id", 1});
        if (N_Utils.isEmpty(paper.getFieldorder())) paper.setFieldorder("desc");
        Map map = paperTeaAppService.listPaperStudent(paper);
        return new ResultVo(0, "获取作业学生列表成功", map);
    }
}
