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.paperstudent.TPaperStudent;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentQuestion;
import com.xhkjedu.sstudy.service.paper.PaperAnalyzeService;
import com.xhkjedu.sstudy.vo.paper.*;
import com.xhkjedu.sstudy.vo.paperstudent.StudentPaperVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.vo.paper.PaperListVo;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @classname PaperAnalyzeController
 * @description 
 * @date 2020/6/24 9:48
 **/
@RestController
@RequestMapping("/paper_analyze")
public class PaperAnalyzeController {
    @Autowired
    private PaperAnalyzeService paperAnalyzeService;

    /**
     * 作业统计--按人
     * @Param [paper]
     * @Author ywx
     * @Date 2020/6/24 10:32
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/people")
    public ResultVo peopleAnalyze(@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});
        PaperAnalyzeVo analyze = paperAnalyzeService.getPeopleAnalyze(paper);
        return new ResultVo(0,"获取作业统计--按人成功",analyze);
    }

    @PostMapping("/list_stu")
    public ResultVo listStu(@RequestBody TPaper paper){
        Integer paperid = paper.getPaperid();
        Integer papertype = paper.getPapertype();
        Integer classid = paper.getClassid();
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        N_Utils.validation(new Object[]{paperid,"作业id",1,papertype,"作业类型",1,classid,"班级id",1,page,"显示页码",1,pageSize,"显示条数",1});
        PageHelper.startPage(page,pageSize);
        List<AnalyzeStudentVo> list = paperAnalyzeService.listStu(paper);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取作业统计--按人--班级学生列表成功",pageResult);
    }

    /**
     * 作业统计--按题
     * @Param [paper]
     * @Author ywx
     * @Date 2020/6/24 15:07
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/question")
    public ResultVo questionAnalyze(@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});
        PaperAnalyzeVo analyze = paperAnalyzeService.getQuestionAnalyze(paperid, papertype, classid);
        return new ResultVo(0,"获取作业统计--按题成功",analyze);
    }

    @PostMapping("/question_detail")
    public ResultVo questionDetailAnalyze(@RequestBody AnalyzeQuestionVo paper){
        Integer paperid = paper.getPaperid();
        String questionid = paper.getQuestionid();
        Integer classid=paper.getClassid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,questionid,"试题id",2,classid,"班级id",1});
        QuestionAnalyzeVo analyze = paperAnalyzeService.getQuestionDetailAnalyze(paper);
        return new ResultVo(0,"获取作业统计--按题详情成功",analyze);
    }

    @PostMapping("/list_question_stu")
    public ResultVo listQuestionStu(@RequestBody TPaperStudentQuestion paper){
        Integer paperid = paper.getPaperid();
        String questionid = paper.getQuestionid();
        Integer classid=paper.getClassid();
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        N_Utils.validation(new Object[]{paperid,"作业id",1,questionid,"试题id",2,classid,"班级id",1
                ,page,"显示页码",1,pageSize,"显示条数",1});
        PageHelper.startPage(page,pageSize);
        List<AnalyzeStudentVo> list = paperAnalyzeService.listQuestionStu(paperid, questionid,classid,paper.getOd());
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取作业统计--按题详情--学生列表成功",pageResult);
    }

    @PostMapping("/squestion_detail")
    public ResultVo squestionDetailAnalyze(@RequestBody AnalyzeQuestionVo paper){
        Integer paperid = paper.getPaperid();
        Integer ptqorder = paper.getPtqorder();
        Integer classid=paper.getClassid();
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        N_Utils.validation(new Object[]{paperid,"作业id",1,ptqorder,"题号",1,classid,"班级id",1
                ,page,"显示页码",1,pageSize,"显示条数",1});
        PageHelper.startPage(page,pageSize);
        List<AnalyzeStudentVo> list = paperAnalyzeService.getSQuestionDetailAnalyze(paperid, ptqorder, classid, paper.getOd());
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取答题卡作业统计--按题详情成功",pageResult);
    }

    @PostMapping("/people_detail")
    public ResultVo peopleDetailAnalyze(@RequestBody TPaperStudent student){
        Integer psid = student.getPsid();
        N_Utils.validation(new Object[]{psid,"作业学生id",1});
        StudentPaperVo paper = paperAnalyzeService.getPeopleDetailAnalyze(psid);
        return new ResultVo(0,"获取作业统计--按人详情成功",paper);
    }

    @PostMapping("/people_sdetail")
    public ResultVo peopleSDetailAnalyze(@RequestBody TPaperStudent student){
        Integer psid = student.getPsid();
        N_Utils.validation(new Object[]{psid,"作业学生id",1});
        StudentPaperVo paper = paperAnalyzeService.getPeopleSDetailAnalyze(psid);
        return new ResultVo(0,"获取答题卡作业统计--按人详情成功",paper);
    }

    /**
     * @Description 作业统计--按人详情
     * @Param [student]
     * @Author ywx
     * @Date 2023/5/6 10:14
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/stu_detail")
    public ResultVo StuDetailAnalyze(@RequestBody TPaperStudent student) {
        Integer psid = student.getPsid();
        N_Utils.validation(new Object[]{psid, "作业学生id", 1});
        StudentPaperVo paper = paperAnalyzeService.getStuDetailAnalyze(psid);
        return new ResultVo(0, "获取作业统计--按人详情成功", paper);
    }

    @PostMapping("/paper_class")
    public ResultVo paperClass(@RequestBody TPaper paper){
        Integer paperid = paper.getPaperid();
        Integer schoolid = paper.getSchoolid();
        Integer createid = paper.getCreateid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,schoolid,"学校id",1,createid,"用户id",1});
        List<Map> list = paperAnalyzeService.listPaperClass(paperid,schoolid,createid);
        return new ResultVo(0,"获取作业班级列表成功",list);
    }

    @PostMapping("/paper_class_student")
    public ResultVo paperClassStudent(@RequestBody TPaperClass paper){
        Integer paperid = paper.getPaperid();
        Integer classid = paper.getClassid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,classid,"班级id",1});
        Map list = paperAnalyzeService.listPaperClassStudent(paper);
        return new ResultVo(0,"获取作业班级学生列表成功",list);
    }

    @PostMapping("/paper_grade")
    public ResultVo paperGrade(@RequestBody AnalyzeSearchVo paper){
        if (paper.getUsertype() == null) return new ResultVo(1,"用户类型不能为空");
        Integer schoolid = paper.getSchoolid();
        Integer gradeid = paper.getGradeid();
        N_Utils.validation(new Object[]{schoolid,"学校id",1,gradeid,"年级id",1});
        List<PaperGAnalyzeVo> list = paperAnalyzeService.listPaperGrade(paper);
        return new ResultVo(0,"获取年级下作业统计列表成功",list);
    }

    @PostMapping("/paper_list")
    public ResultVo listPaper(@RequestBody AnalyzeSearchVo paper) {
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        Integer teacherid = paper.getTeacherid();
        Integer gradeid = paper.getGradeid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,teacherid,"教师id",1
                ,gradeid,"年级id",1,paper.getCreateid(),"创建人id",1});
        //paperAnalyzeService.setYear(paper);
        paperAnalyzeService.setClassIds(paper);//设置教师权限班级
        PageHelper.startPage(page, pageSize);
        List<PaperListVo> list = paperAnalyzeService.listPaper(paper);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取年级下教师发布作业列表成功",pageResult);
    }

    @PostMapping("/export")
    public ResultVo exportPaperAnalyze(@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});
        Map analyze = paperAnalyzeService.exportPaperAnalyze(paper);
        return new ResultVo(0,"获取作业导出数据成功",analyze);
    }

    @PostMapping("/paper_stu")
    public ResultVo paperStu(@RequestBody AnalyzeSearchVo paper){
        if (paper.getUsertype() == null) return new ResultVo(1,"用户类型不能为空");
        Integer schoolid = paper.getSchoolid();
        Integer gradeid = paper.getGradeid();
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1, gradeid, "年级id", 1
                , page, "显示页码", 1, pageSize, "显示条数", 1});
        //paperAnalyzeService.setYear(paper);
        paperAnalyzeService.setClassIds(paper);
        List<AStudentPaperVo> list = paperAnalyzeService.listPaperStuGrade(paper);
        Integer start = (page - 1) * pageSize;
        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.size());
        pageResult.setList(list.stream().skip(start).limit(pageSize).collect(Collectors.toList()));
        return new ResultVo(0, "获取作业统计--按学生列表成功", pageResult);
    }

    @PostMapping("/paper_sdetail")
    public ResultVo listPaperStuDetail(@RequestBody AnalyzeSearchVo paper) {
        if (paper.getUsertype() == null) return new ResultVo(1,"用户类型不能为空");
        Integer studentid = paper.getStudentid();
        Integer gradeid = paper.getGradeid();
        N_Utils.validation(new Object[]{studentid,"学生id",1,gradeid,"年级id",1});
        //paperAnalyzeService.setYear(paper);
        Map map = paperAnalyzeService.listPaperStuDetail(paper);
        return new ResultVo(0,"获取作业统计--按学生详情成功",map);
    }

    @PostMapping("/paper_sexport")
    public ResultVo listPaperStuExport(@RequestBody AnalyzeSearchVo paper) {
        if (paper.getUsertype() == null) return new ResultVo(1,"用户类型不能为空");
        Integer schoolid = paper.getSchoolid();
        Integer gradeid = paper.getGradeid();
        N_Utils.validation(new Object[]{schoolid,"学校id",1,gradeid,"年级id",1});
        Map list = paperAnalyzeService.listPaperStuExport(paper);
        return new ResultVo(0,"获取作业统计--按学生导出列表成功",list);
    }
}
