package com.xhkjedu.sapi.service.region;

import com.xhkjedu.sapi.mapper.region.RPaperAnalyzeMapper;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.region.RPaperVo;
import com.xhkjedu.sapi.vo.region.RSchoolVo;
import com.xhkjedu.sapi.vo.region.RegionParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RPaperAnalyzeService
 * Description TODO 区域作业统计
 * Author WN
 * Date 2021/7/13 16:15
 **/
@Service
public class RPaperAnalyzeService {
    @Autowired
    RPaperAnalyzeMapper rPaperAnalyzeMapper;

    //各类作业数量
    public Map getRegionPaperNum(RegionParam param){
        Map map = new HashMap();

        //作业
        Integer papernum = rPaperAnalyzeMapper.getRegionPaperNumPaper(param);
        //预习
        Integer previewnum = rPaperAnalyzeMapper.getRegionPaperNumPreview(param);
        //背诵
        Integer recitenum = rPaperAnalyzeMapper.getRegionPaperNumRecite(param);
        //作业辅导
        Integer queexpnum = rPaperAnalyzeMapper.getRegionPaperNumQueexp(param);

        map.put("papernum",papernum);
        map.put("previewnum",previewnum);
        map.put("recitenum",recitenum);
        map.put("queexpnum",queexpnum);
        return map;
    }

    //作业各类率统计——作业
    public Map listRegionSchoolRatePaper(RegionParam param){
        Map map = new HashMap();
        List<RSchoolVo> list = rPaperAnalyzeMapper.listRegionSchoolRatePaper(param);

        List<Map> ratelist = new ArrayList<>();
        double totalcommitrate = 0;//作业提交率
        double totalcommitasrate = 0;//作业按时提交率
        double totalcheckrate = 0;//作业批改率
        double totalscorerate = 0;//作业正确率

        if(list!=null && list.size()>0){
            Integer totalStunum = 0;//总作业发布次数
            Integer totalCommitnum = 0;//总作业提交数量
            Integer totalCommitasnum = 0;//总作业按时提交数量
            Integer totalChecknum = 0;//总作业批改数量
            Double totalPaperscore = 0.0;//总作业分值
            Double totalStuscore = 0.0;//总作业学生得分

            for(RSchoolVo s:list){
                //学校内作业统计数量
                List<RPaperVo> papers = s.getPaperlist();

                //学校作业发布次数
                Integer schoolStunum = papers.stream().mapToInt(RPaperVo::getStunum).sum();
                totalStunum = totalStunum + schoolStunum;
                //学校作业提交数量
                Integer schoolCommitnum = papers.stream().mapToInt(RPaperVo::getCommitnum).sum();
                totalCommitnum = totalCommitnum + schoolCommitnum;
                //学校作业按时提交数量
                Integer schoolCommitasnum = papers.stream().mapToInt(RPaperVo::getCommitasnum).sum();
                totalCommitasnum = totalCommitasnum + schoolCommitasnum;
                //学校作业批改数量
                Integer schoolChecknum = papers.stream().mapToInt(RPaperVo::getChecknum).sum();
                totalChecknum = totalChecknum + schoolChecknum;
                //学校作业分值
                Double schoolPaperscore = papers.stream().mapToDouble(RPaperVo::getPaperscore).sum();
                totalPaperscore = totalPaperscore + schoolPaperscore;
                //学校作业学生得分
                Double schoolStuscore = papers.stream().mapToDouble(RPaperVo::getStuscore).sum();
                totalStuscore = totalStuscore + schoolStuscore;

                //作业提交率
                double commitrate = N_Utils.getIntegerDivideAndMulitiply(schoolCommitnum,schoolStunum);
                //作业按时提交率
                double commitasrate = N_Utils.getIntegerDivideAndMulitiply(schoolCommitasnum,schoolStunum);
                //作业批改率
                double checkrate = 0;
                if(schoolCommitnum>0){
                    checkrate = N_Utils.getIntegerDivideAndMulitiply(schoolChecknum,schoolCommitnum);
                }
                //作业正确率
                double scorerate = N_Utils.getDoubleDivideAndMulitiply(schoolStuscore,schoolPaperscore);

                Map rateMap = new HashMap();
                rateMap.put("schoolname",s.getSchoolname());
                rateMap.put("shortname",s.getShortname());
                rateMap.put("papernum",papers.size());
                rateMap.put("commitrate",commitrate);
                rateMap.put("commitasrate",commitasrate);
                rateMap.put("checkrate",checkrate);
                rateMap.put("scorerate",scorerate);

                ratelist.add(rateMap);
            }

            //计算总率
            //作业提交率
            totalcommitrate = N_Utils.getIntegerDivideAndMulitiply(totalCommitnum,totalStunum);
            //作业按时提交率
            totalcommitasrate = N_Utils.getIntegerDivideAndMulitiply(totalCommitasnum,totalStunum);

            if(totalCommitnum>0){
                totalcheckrate = N_Utils.getIntegerDivideAndMulitiply(totalChecknum,totalCommitnum);
            }
            //作业正确率
            totalscorerate = N_Utils.getDoubleDivideAndMulitiply(totalStuscore,totalPaperscore);
        }

        map.put("totalcommitrate",totalcommitrate);
        map.put("totalcommitasrate",totalcommitasrate);
        map.put("totalcheckrate",totalcheckrate);
        map.put("totalscorerate",totalscorerate);
        map.put("schools",ratelist);

        return map;
    }

    //作业各类率统计--背诵
    public Map listRegionSchoolRateRecite(RegionParam param){
        Map map = new HashMap();
        List<RSchoolVo> list = rPaperAnalyzeMapper.listRegionSchoolRateRecite(param);
        List<Map> rtnlist = new ArrayList<>();

        double totalcommitrate = 0;//背诵提交率
        double totalcommitasrate = 0;//背诵按时提交率
        double totalscorerate = 0;//得分率

        if(list!=null && list.size()>0){
            Integer totalStunum = 0;//总背诵发布次数
            Integer totalCommitnum = 0;//总背诵提交数量
            Integer totalCommitasnum = 0;//总背诵按时提交数量
            Integer totalPaperscore = 0;//总背诵分值
            Integer totalStuscore = 0;//总背诵学生得分

            for(RSchoolVo s:list){
                //学校内背诵统计数量
                List<RPaperVo> papers = s.getPaperlist();

                //学校背诵发布次数
                Integer schoolStunum = papers.stream().mapToInt(RPaperVo::getStunum).sum();
                totalStunum = totalStunum + schoolStunum;
                //学校背诵提交数量
                Integer schoolCommitnum = papers.stream().mapToInt(RPaperVo::getCommitnum).sum();
                totalCommitnum = totalCommitnum + schoolCommitnum;
                //学校背诵按时提交数量
                Integer schoolCommitasnum = papers.stream().mapToInt(RPaperVo::getCommitasnum).sum();
                totalCommitasnum = totalCommitasnum + schoolCommitasnum;

                //学校背诵分值
                Integer schoolPaperscore = schoolStunum * 100;
                totalPaperscore = totalPaperscore + schoolPaperscore;
                //学校背诵学生得分
                Double sum = papers.stream().mapToDouble(RPaperVo::getStuscore).sum();
                Integer schoolStuscore = sum.intValue();
                totalStuscore = totalStuscore + schoolStuscore;

                //背诵提交率
                double commitrate = N_Utils.getIntegerDivideAndMulitiply(schoolCommitnum,schoolStunum);
                //背诵按时提交率
                double commitasrate = N_Utils.getIntegerDivideAndMulitiply(schoolCommitasnum,schoolStunum);
                //背诵得分率
                double scorerate = N_Utils.getIntegerDivideAndMulitiply(schoolStuscore,schoolPaperscore);

                Map rateMap = new HashMap();
                rateMap.put("schoolname",s.getSchoolname());
                rateMap.put("shortname",s.getShortname());
                rateMap.put("recitenum",papers.size());
                rateMap.put("commitrate",commitrate);
                rateMap.put("commitasrate",commitasrate);
                rateMap.put("scorerate",scorerate);

                rtnlist.add(rateMap);
            }

            //计算总率
            //背诵提交率
            totalcommitrate = N_Utils.getIntegerDivideAndMulitiply(totalCommitnum,totalStunum);
            //背诵按时提交率
            totalcommitasrate = N_Utils.getIntegerDivideAndMulitiply(totalCommitasnum,totalStunum);
            //背诵得分率
            totalscorerate = N_Utils.getIntegerDivideAndMulitiply(totalStuscore,totalPaperscore);
        }

        map.put("totalcommitrate",totalcommitrate);
        map.put("totalcommitasrate",totalcommitasrate);
        map.put("totalscorerate",totalscorerate);
        map.put("schools",rtnlist);

        return map;
    }

    //作业各类率统计--预习
    public Map getRegionSchoolRatePreview(RegionParam param){
        Map map = new HashMap();
        List<RSchoolVo> list = rPaperAnalyzeMapper.listRegionSchoolRatePreview(param);
        List<Map> rtnlist = new ArrayList<>();

        double previewrate = 0;//预习率
        if(list!=null && list.size()>0){
            Integer totalstunum = 0;
            Integer totalcommitnum = 0;

            for(RSchoolVo s:list){
                //学校内预习统计数量
                List<RPaperVo> papers = s.getPaperlist();

                //学校预习发布次数
                Integer schoolStunum = papers.stream().mapToInt(RPaperVo::getStunum).sum();
                totalstunum = totalstunum + schoolStunum;
                //学校预习提交数量
                Integer schoolCommitnum = papers.stream().mapToInt(RPaperVo::getCommitnum).sum();
                totalcommitnum = totalcommitnum + schoolCommitnum;

                //预习提交率
                double commitrate = N_Utils.getIntegerDivideAndMulitiply(schoolCommitnum,schoolStunum);

                Map rateMap = new HashMap();
                rateMap.put("schoolname",s.getSchoolname());
                rateMap.put("shortname",s.getShortname());
                rateMap.put("previewnum",papers.size());
                rateMap.put("commitrate",commitrate);

                rtnlist.add(rateMap);
            }

            previewrate = N_Utils.getIntegerDivideAndMulitiply(totalcommitnum,totalstunum);
        }

        map.put("totalpreviewrate",previewrate);
        map.put("schools",rtnlist);

        return map;
    }

    //人均用时——作业
    public List<Map> listRegionSchoolUsetimePaper(RegionParam param){
        List<Map> list = rPaperAnalyzeMapper.listRegionSchoolUsetimePaper(param);

        if(list!=null && list.size()>0){

            Integer day = N_Utils.getTimestampDay(param.getBegintime(),param.getEndtime());

            for(Map map : list){
                Integer usetimes = Integer.parseInt(map.get("costtime").toString());
                Integer stunum = Integer.parseInt(map.get("stunum").toString());

                double avgtime = N_Utils.formatAvg(usetimes, day * stunum);
                map.put("avgtime",avgtime);
            }
        }
        return list;
    }

    //人均用时——预习
    public List<Map> listRegionSchoolUsetimePreview(RegionParam param){
        List<Map> list = rPaperAnalyzeMapper.listRegionSchoolUsetimePreview(param);

        if(list!=null && list.size()>0){

            Integer day = N_Utils.getTimestampDay(param.getBegintime(),param.getEndtime());

            for(Map map : list){
                Integer usetimes = Integer.parseInt(map.get("costtime").toString());
                Integer stunum = Integer.parseInt(map.get("stunum").toString());

                double avgtime = N_Utils.formatAvg(usetimes, day * stunum);
                map.put("avgtime",avgtime);
            }
        }
        return list;
    }

}
