package com.xhkjedu.sapi.service.region;

import com.xhkjedu.sapi.mapper.region.RRoomAnalyzeMapper;
import com.xhkjedu.sapi.vo.region.RRoomVo;
import com.xhkjedu.sapi.vo.region.RegionParam;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

/**
 * @author ywx
 * @classname RRoomAnalyzeService
 * @description 区域后台统计--课堂统计
 * @date 2021/7/13 15:54
 **/
@Service
public class RRoomAnalyzeService {
    @Resource
    private RRoomAnalyzeMapper rRoomAnalyzeMapper;

    public Map listRoom(RegionParam param) {
        Map map = new HashMap();
        //学校课堂使用统计
        List<RRoomVo> list = new ArrayList<>();
        List<RRoomVo> rooms = rRoomAnalyzeMapper.listRoom(param);
        if (N_Utils.isListNotEmpty(rooms)) {
            Map<Integer, List<RRoomVo>> schoolRoomMap = rooms.stream().collect(Collectors.groupingBy(RRoomVo::getSchoolid));
            for (Map.Entry<Integer, List<RRoomVo>> entry : schoolRoomMap.entrySet()) {
                List<RRoomVo> schoolRoomList = entry.getValue();
                RRoomVo vo = schoolRoomList.get(0);
                int qdnum = schoolRoomList.stream().mapToInt(RRoomVo::getQdnum).sum();
                int dmnum = schoolRoomList.stream().mapToInt(RRoomVo::getDmnum).sum();
                int twnum = schoolRoomList.stream().mapToInt(RRoomVo::getTwnum).sum();
                int tcnum = schoolRoomList.stream().mapToInt(RRoomVo::getTcnum).sum();
                int jtnum = schoolRoomList.stream().mapToInt(RRoomVo::getJtnum).sum();
                int votenum = schoolRoomList.stream().mapToInt(RRoomVo::getVotenum).sum();
                int qdstunum = schoolRoomList.stream().mapToInt(RRoomVo::getQdstunum).sum();
                int twstunum = schoolRoomList.stream().mapToInt(RRoomVo::getTwstunum).sum();
                int twzqnum = schoolRoomList.stream().mapToInt(RRoomVo::getTwzqnum).sum();
                int tcstunum = schoolRoomList.stream().mapToInt(RRoomVo::getTcstunum).sum();
                int tctjnum = schoolRoomList.stream().mapToInt(RRoomVo::getTctjnum).sum();
                int tczqnum = schoolRoomList.stream().mapToInt(RRoomVo::getTczqnum).sum();
                int totaltime = schoolRoomList.stream().mapToInt(RRoomVo::getTotaltime).sum();
                int classnum = schoolRoomList.stream().mapToInt(RRoomVo::getClassnum).sum();
                int realstunum = schoolRoomList.stream().mapToInt(RRoomVo::getRealstunum).sum();

                vo.setKtnum(schoolRoomList.size());
                vo.setQdnum(qdnum);
                vo.setDmnum(dmnum);
                vo.setTwnum(twnum);
                vo.setTcnum(tcnum);
                vo.setJtnum(jtnum);
                vo.setVotenum(votenum);
                vo.setQdstunum(qdstunum);
                vo.setTwstunum(twstunum);
                vo.setTwzqnum(twzqnum);
                vo.setTcstunum(tcstunum);
                vo.setTctjnum(tctjnum);
                vo.setTczqnum(tczqnum);
                vo.setTotaltime(totaltime);
                vo.setClassnum(classnum);
                vo.setRealstunum(realstunum);

                list.add(vo);
            }
        }

        //教学统计--次数统计
        setNum(map, list);

        //教学统计--区出勤、抢答、提交、正确率统计
        setRate(map, list);

        //教学统计--课堂平均每天用时统计
        List<Map> times = new ArrayList<>();
        //教学统计--学校出勤、抢答、提交、正确率统计
        List<Map> rates = new ArrayList<>();
        //教学统计--应用统计
        List<Map> types = new ArrayList<>();
        Map<Integer, List<RRoomVo>> tscollect = list.stream().collect(Collectors.groupingBy(RRoomVo::getSchoolid));
        for(Map.Entry smap:tscollect.entrySet()) {
            List<RRoomVo> schools = (List<RRoomVo>) smap.getValue();
            Integer daynum = N_Utils.getTimestampDay(param.getBegintime(),param.getEndtime());//使用天数
            Integer totaltime = schools.stream().collect(Collectors.summingInt(RRoomVo::getTotaltime));//总用时
            RRoomVo school = schools.get(0);
            String schoolname = school.getSchoolname();
            String shortname = school.getShortname();
            Map time = new HashMap();
            time.put("schoolname",schoolname);
            time.put("shortname",shortname);
            time.put("totaltime",totaltime);//课堂时长
            time.put("pjtime", N_Utils.formatAvg(totaltime,daynum));//平均用时
            times.add(time);

            Map rate = new HashMap();
            rate.put("schoolname",schoolname);
            rate.put("shortname",shortname);
            setRate(rate,schools);
            rates.add(rate);

            Map type = new HashMap();
            type.put("schoolname",schoolname);
            type.put("shortname",shortname);
            setNum(type,schools);
            types.add(type);
        }
        map.put("times",times);
        map.put("rates",rates);
        map.put("types",types);
        return map;
    }

    private void setNum(Map map, List<RRoomVo> list) {
        map.put("ktnum",list.stream().collect(Collectors.summingInt(RRoomVo::getKtnum)));//课堂次数
        map.put("qdnum",list.stream().collect(Collectors.summingInt(RRoomVo::getQdnum)));//抢答次数
        map.put("dmnum",list.stream().collect(Collectors.summingInt(RRoomVo::getDmnum)));//点名次数
        map.put("twnum",list.stream().collect(Collectors.summingInt(RRoomVo::getTwnum)));//提问次数
        map.put("tcnum",list.stream().collect(Collectors.summingInt(RRoomVo::getTcnum)));//堂测次数
        map.put("jtnum",list.stream().collect(Collectors.summingInt(RRoomVo::getJtnum)));//截图次数
        map.put("votenum",list.stream().collect(Collectors.summingInt(RRoomVo::getVotenum)));//投票次数
    }

    private void setRate(Map map, List<RRoomVo> list) {
        Integer classnum = list.stream().collect(Collectors.summingInt(RRoomVo::getClassnum));//班级总人数
        Integer realstunum = list.stream().collect(Collectors.summingInt(RRoomVo::getRealstunum));//实际出勤总人数
        map.put("cqrate", N_Utils.getIntegerDivideAndMulitiply(realstunum,classnum));//出勤率

        Integer qdnum = list.stream().collect(Collectors.summingInt(RRoomVo::getQdnum));//发起抢答次数
        Integer qdstunum = list.stream().collect(Collectors.summingInt(RRoomVo::getQdstunum));//学生抢答次数
        map.put("qdrate", N_Utils.getIntegerDivideAndMulitiply(qdstunum,qdnum*realstunum));//抢答率

        Integer twstunum = list.stream().collect(Collectors.summingInt(RRoomVo::getTwstunum));//提问总人数
        Integer twtjrate = 0;
        double twzqrate = 0;
        if (twstunum != 0) {
            twtjrate = 100;//提问总人数和提交人数相等
            Integer twzqnum = list.stream().collect(Collectors.summingInt(RRoomVo::getTwzqnum));//提问作答正确人数
            twzqrate = N_Utils.getIntegerDivideAndMulitiply(twzqnum,twstunum);
        }
        map.put("twtjrate",twtjrate);//提问提交率
        map.put("twzqrate",twzqrate);//提问正确率

        Integer tctjnum = list.stream().collect(Collectors.summingInt(RRoomVo::getTctjnum));//堂测提交人数
        Integer tcstunum = list.stream().collect(Collectors.summingInt(RRoomVo::getTcstunum));//堂测总人数
        map.put("tctjrate", N_Utils.getIntegerDivideAndMulitiply(tctjnum,tcstunum));//堂测提交率
        Integer tczqnum = list.stream().collect(Collectors.summingInt(RRoomVo::getTczqnum));//堂测作答正确人数
        map.put("tczqrate", N_Utils.getIntegerDivideAndMulitiply(tczqnum,tctjnum));//堂测正确率
    }
}
