package com.xhkjedu.sapi.service.analyze;

import com.xhkjedu.sapi.mapper.analyze.SchoolSummaryMapper;
import com.xhkjedu.sapi.mapper.system.SchoolMapper;
import com.xhkjedu.sapi.vo.analyze.SchoolParams;
import com.xhkjedu.sapi.vo.analyze.SchoolSummaryVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName SchoolSummaryService
 * Description TODO 数据汇总
 * Author WN
 * Date 2020/10/14 14:41
 **/
@Service
public class SchoolSummaryService {
    @Autowired
    private SchoolSummaryMapper schoolSummaryMapper;
    @Resource
    private SchoolMapper schoolMapper;

    //数据汇总
    public List<SchoolSummaryVo> schoolSummary(SchoolParams params) {
        //Integer year = schoolMapper.getYearById(params.getSchoolid());
        //params.setYear(year);
        params.setTimestamp(N_Utils.getSecondTimestamp());
        List<SchoolSummaryVo> users = schoolSummaryMapper.listUser(params);
        //教师作业统计
        List<SchoolSummaryVo> paperlist = schoolSummaryMapper.getPaperAnalyze(params);
        for (SchoolSummaryVo paper : paperlist) {
            SchoolSummaryVo user = users.stream().filter(u -> u.getUserid().equals(paper.getUserid())).findFirst().orElse(null);
            if (user != null) {
                user.setPfabunum(paper.getPfabunum());
                Integer stunum = paper.getPstunum();
                user.setPstunum(stunum);
                Integer commitnum = paper.getPcommitnum();
                user.setPcommitnum(commitnum);
                user.setPcommitrate(N_Utils.getIntegerDivideAndMulitiply(commitnum, stunum));
                Integer checknum = paper.getPchecknum();
                user.setPchecknum(checknum);
                user.setPcheckrate(N_Utils.getIntegerDivideAndMulitiply(checknum,commitnum));
                user.setPquestionnum(paper.getPquestionnum());
            }
        }

        //教师资源统计
        List<SchoolSummaryVo> resourcelist = schoolSummaryMapper.getResourceAnalyze(params);
        for(SchoolSummaryVo resource : resourcelist){
            SchoolSummaryVo user = users.stream().filter(u -> u.getUserid().equals(resource.getUserid())).findFirst().orElse(null);
            if(user != null){
                user.setZytotal(resource.getZytotal());
                user.setZyxtnum(resource.getZyxtnum());
                user.setZyjanum(resource.getZyjanum());
                user.setZykjnum(resource.getZykjnum());
                user.setZyscnum(resource.getZyscnum());
                user.setZyspnum(resource.getZyspnum());
                user.setZyywbnum(resource.getZyywbnum());
                user.setZyqtnum(resource.getZyqtnum());
            }
        }

        //教师课堂统计
        List<SchoolSummaryVo> roomlist = schoolSummaryMapper.getRoomAnalyze(params);
        Map<Integer, List<SchoolSummaryVo>> listMap = roomlist.stream().collect(Collectors.groupingBy(SchoolSummaryVo::getUserid));
        for (Map.Entry<Integer, List<SchoolSummaryVo>> entry : listMap.entrySet()) {
            List<SchoolSummaryVo> tlist = entry.getValue();
            Integer userid = entry.getKey();
            SchoolSummaryVo user = users.stream().filter(u -> u.getUserid().equals(userid)).findFirst().orElse(null);
            if (user != null) {
                int kttotal = tlist.size();
                user.setKttotal(kttotal);
                user.setKtpicnum(tlist.stream().mapToInt(SchoolSummaryVo::getKtpicnum).sum());
                user.setKtasknum(tlist.stream().mapToInt(SchoolSummaryVo::getKtasknum).sum());
                user.setKtcallnum(tlist.stream().mapToInt(SchoolSummaryVo::getKtcallnum).sum());
                user.setKtquestionnum(tlist.stream().mapToInt(SchoolSummaryVo::getKtquestionnum).sum());
                user.setKtexamnum(tlist.stream().mapToInt(SchoolSummaryVo::getKtexamnum).sum());
                int kttimes = tlist.stream().mapToInt(SchoolSummaryVo::getKttimes).sum();
                user.setKttimes(kttimes);
                Integer realstunum = tlist.stream().mapToInt(SchoolSummaryVo::getRealstunum).sum();
                Integer classnum = tlist.stream().mapToInt(SchoolSummaryVo::getClassnum).sum();
                user.setKtavttime(N_Utils.formatAvg(kttimes, kttotal));
                user.setKtsignrate(N_Utils.getIntegerDivideAndMulitiply(realstunum, classnum));
                user.setKtasknum(tlist.stream().mapToInt(SchoolSummaryVo::getKtvotenum).sum());
            }
        }

        //教师预习统计
        List<SchoolSummaryVo> guidelist = schoolSummaryMapper.getPreviewAnalyze(params);
        for(SchoolSummaryVo guide : guidelist){
            SchoolSummaryVo user = users.stream().filter(u -> u.getUserid().equals(guide.getUserid())).findFirst().orElse(null);
            if(user != null){
                user.setYxfbnum(guide.getYxfbnum());
                Integer stunum = guide.getYxstunum();
                user.setYxstunum(stunum);
                Integer commitnum = guide.getYxcomitnum();
                user.setYxcomitnum(commitnum);
                user.setYxrate(N_Utils.getIntegerDivideAndMulitiply(commitnum, stunum));
            }
        }
        //教师背诵统计
        /*List<SchoolSummaryVo> recitelist = schoolSummaryMapper.getReciteAnalyze(params);
        for(SchoolSummaryVo recite : recitelist){
            SchoolSummaryVo user = users.stream().filter(u -> u.getUserid().equals(recite.getUserid())).findFirst().orElse(null);
            if(user != null){
                user.setBsfbnum(recite.getBsfbnum());
                Integer stunum = recite.getBsstunum();
                user.setBsstunum(stunum);
                Integer commitnum = recite.getBscomitnum();
                user.setBscomitnum(commitnum);
                user.setBsrate(N_Utils.getIntegerDivideAndMulitiply(commitnum, stunum));
            }
        }*/
        //教师错题辅导
        List<SchoolSummaryVo> ctfdlist = schoolSummaryMapper.getErrorQuestionFdAnalyze(params);
        for(SchoolSummaryVo ctfd : ctfdlist){
            SchoolSummaryVo user = users.stream().filter(u -> u.getUserid().equals(ctfd.getUserid())).findFirst().orElse(null);
            if(user != null){
                user.setCtfdnum(ctfd.getCtfdnum());
                user.setCtfdpjtime(ctfd.getCtfdpjtime());
            }
        }
        return users;
    }
}
