package com.xhkjedu.sstudy.service.paper;

import com.xhkjedu.sstudy.mapper.paper.PaperMapper;
import com.xhkjedu.sstudy.mapper.paper.PaperTeaAppMapper;
import com.xhkjedu.sstudy.model.paper.TPaper;
import com.xhkjedu.sstudy.model.question.TQuestionClass;
import com.xhkjedu.sstudy.vo.paper.PClassVo;
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.sstudy.vo.paperstudent.StudentVo;
import com.xhkjedu.utils.N_Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @classname PaperTeacherAppService
 * @description 
 * @date 2020/10/12 15:08
 **/
@Service
@Slf4j
public class PaperTeaAppService {
    @Resource
    private PaperTeaAppMapper paperTeaAppMapper;
    @Resource
    private PaperMapper paperMapper;

    //获取作业列表
    public List<PaperTeacherAppVo> list(PaperSearchVo paperSearch) {
        if (paperSearch.getPsstate().equals(21)) {//未布置作业列表
            return paperTeaAppMapper.listWbz(paperSearch);
        }
        List<PaperTeacherAppVo> list = paperTeaAppMapper.listTeacherApp(paperSearch);
        for (PaperTeacherAppVo p : list) {
            PClassVo pc = setNum(p.getStudents());
            p.setFbnum(pc.getFbnum());
            p.setCommitnum(pc.getCommitnum());
            p.setChecknum(pc.getChecknum());
            p.setWtjnum(pc.getFbnum() - pc.getCommitnum());
            p.setDpgnum(pc.getCommitnum() - pc.getChecknum());
            p.setStudents(null);
        }

        return list;
    }

    /**
     * 作业情况
     * @Param [paperid]
     * @Author ywx
     * @Date 2020/10/15 9:09
     * @return java.util.List<com.xhkjedu.vo.teacherapp.PClassVo>
     **/
    public List<PClassVo> getPaperDetail(Integer paperid, Integer checkid) {
        TPaper paper = paperMapper.selectByPrimaryKey(paperid);
        List<PClassVo> list = paperTeaAppMapper.getPaperDetail(paperid, checkid);
        for (PClassVo p : list) {
            PClassVo pc = setNum(p.getStudents());
            p.setFbnum(pc.getFbnum());
            p.setCommitnum(pc.getCommitnum());
            p.setChecknum(pc.getChecknum());
            p.setStudents(null);
            p.setChecktype(paper.getChecktype());
        }
        return list;
    }

    //封装试卷发布、提交、批改数量
    public PClassVo setNum(List<StudentVo> students){
        int fbnum = students.size();
        int ytjnum = 0;
        int checknum = 0;
        for (StudentVo s : students) {
            Integer pschecked = s.getPschecked();
            if (pschecked == 1){
                ytjnum++;
            } else if (pschecked == 2){
                ytjnum++;
                checknum ++;
            }
        }
        PClassVo p = new PClassVo();
        p.setFbnum(fbnum);
        p.setCommitnum(ytjnum);
        p.setChecknum(checknum);
        return p;
    }

    /**
     * 作业报告
     * @Param [paper]
     * @Author ywx
     * @Date 2020/10/14 9:12
     * @return java.util.List<com.xhkjedu.vo.teacherapp.PaperTeacherReportVo>
     **/
    public List<PaperTeacherReportVo> getReport(TPaper paper) {
        List<PaperTeacherReportVo> list = paperTeaAppMapper.getReport(paper);
        for (PaperTeacherReportVo q : list) {
            List<StudentVo> students = q.getStudents();
            int ytjnum = students.size();
            int correctnum = 0;
            for (StudentVo s : students) {
                if (s.getPschecked() == 1){
                    correctnum ++;
                }
            }
            q.setCorrectrate(N_Utils.getIntegerDivideAndMulitiply(correctnum,ytjnum));
            q.setStudents(null);
        }
        return list;
    }

    /**
     * 未提交学生列表
     * @Param [paperid]
     * @Author ywx
     * @Date 2020/10/14 15:07
     * @return java.util.List<com.xhkjedu.vo.teacherapp.PStudentVo>
     **/
    public List<PStudentVo> listWtjStudent(Integer paperid, Integer classid) {
        return paperTeaAppMapper.listWtjStudent(paperid, classid);
    }

    /**
     * 已提交学生列表
     * @Param [paper]
     * @Author ywx
     * @Date 2020/10/14 15:12
     * @return java.util.List<com.xhkjedu.vo.teacherapp.PStudentVo>
     **/
    public List<PStudentVo> listYtjStudent(TPaper paper) {
        return paperTeaAppMapper.listYtjStudent(paper);
    }

    /**
     * 获取学生答题卡
     * @Param [paper]
     * @Author ywx
     * @Date 2020/10/14 16:04
     * @return com.xhkjedu.vo.teacherapp.PStudentVo
     **/
    public PStudentVo getStudentScantron(PStudentVo paper) {
        return paperTeaAppMapper.getStudentScantron(paper);
    }

    /**
     * 学生答题卡单题详情
     * @Param [paper]
     * @Author ywx
     * @Date 2020/10/14 16:21
     * @return com.xhkjedu.vo.paperstudent.PaperStudentQuestionVo
     **/
    public PaperStudentQuestionVo getStudentQuestion(PaperStudentQuestionVo paper) {
        return paperTeaAppMapper.getStudentQuestion(paper);
    }

    /**
     * 试题详情
     * @Param [question]
     * @Author ywx
     * @Date 2020/10/15 11:11
     * @return com.xhkjedu.vo.teacherapp.PQuestionVo
     **/
    public PQuestionVo getQuestionDetail(TQuestionClass question) {
        PQuestionVo q = paperTeaAppMapper.getQuestionDetail(question);
        Double qscore = q.getQscore();
        List<PStudentVo> students = q.getStudents();
        double stuscore = students.stream().collect(Collectors.summingDouble(PStudentVo::getScorerate));
        //20220421修改发布数量为收到作业的学生数量，如果作业提交了单题也应该是已提交
//        int fbnum = students.size();
        int fbnum = paperTeaAppMapper.getPaperstudentNum(question.getPaperid(), question.getClassid());
        int tjnum = students.size();
        int ytjnum = 0;
        for (PStudentVo s : students) {
            if (s.getPschecked() == 1) {
                ytjnum++;
            }
            s.setScorerate(N_Utils.getDoubleDivideAndMulitiply(s.getScorerate(), qscore));
        }
        q.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(ytjnum, fbnum));
        q.setScorerate(N_Utils.getDoubleDivideAndMulitiply(stuscore, (qscore * tjnum)));//得分率为学生总得分，除以提交的作业中试题总分
        IntSummaryStatistics statistics = students.stream().mapToInt(PStudentVo::getCosttime).summaryStatistics();
        q.setAvgtime((int) Math.ceil(statistics.getAverage()));
        q.setMintime(statistics.getMin());
        return q;
    }

    /**
     * 作业学生列表
     *
     * @return java.util.List<com.xhkjedu.sstudy.vo.paper.PStudentVo>
     * @Param [paper]
     * @Author ywx
     * @Date 2022/5/31 10:17
     **/
    public Map listPaperStudent(TPaper paper) {
        Map map = new HashMap();
        List<PStudentVo> list = paperTeaAppMapper.listPaperStudent(paper);
        int classnum = list.size();//作业人数
        List<PStudentVo> wtjStus = list.stream().filter(s -> s.getPschecked().equals(0)).collect(Collectors.toList());
        List<PStudentVo> wpyStus = list.stream().filter(s -> s.getPschecked().equals(1)).collect(Collectors.toList());
        List<PStudentVo> ypyStus = list.stream().filter(s -> s.getPschecked().equals(2)).collect(Collectors.toList());

        map.put("classnum", classnum);
        map.put("wtjStus", wtjStus);
        map.put("wtjnum", wtjStus.size());
        map.put("wpyStus", wpyStus);
        map.put("wpynum", wpyStus.size());
        map.put("ypyStus", ypyStus);
        map.put("ypynum", ypyStus.size());

        Integer papertype = paperMapper.getPaperTypeById(paper.getPaperid());//根据作业id获取作业分类
        map.put("papertype", papertype);
        return map;
    }
}
