package com.xhkjedu.sapi.service.region;

import com.xhkjedu.sapi.mapper.cloud.CloudMapper;
import com.xhkjedu.sapi.mapper.region.RSchoolAnalyzeMapper;
import com.xhkjedu.sapi.model.cloud.TCloud;
import com.xhkjedu.sapi.vo.region.RegionParam;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.region.RSStuVo;
import com.xhkjedu.sapi.vo.region.RSTeacherVo;
import com.xhkjedu.sapi.vo.region.SchoolVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @classname RSchoolAnalyzeService
 * @description 
 * @date 2021/7/7 14:19
 **/
@Service
public class RSchoolAnalyzeService {
    @Resource
    private RSchoolAnalyzeMapper rSchoolAnalyzeMapper;
    @Resource
    private CloudMapper cloudMapper;

    //获取学校列表
    public Map listSchool(RegionParam param) {
        Map map = new HashMap();
        TCloud cloud = cloudMapper.getCloud();
        map.put("cloudmap",cloud.getCloudmap());
        List<SchoolVo> schools = rSchoolAnalyzeMapper.listSchool(param);
        List<Map> levels = new ArrayList<>();
        /*Map<Object, List<SchoolVo>> schoollevel = schools.stream().collect(Collectors.groupingBy(m -> m.getSchoollevel()));
        for(Map.Entry lmap:schoollevel.entrySet()) {
            Map level = new HashMap();
            level.put("schoollevel",lmap.getKey());
            List<SchoolVo> llist = (List<SchoolVo>) lmap.getValue();
            int bjnum = llist.stream().mapToInt(SchoolVo::getBjnum).sum();
            level.put("bjnum",bjnum);
            int jsnum = llist.stream().mapToInt(SchoolVo::getJsnum).sum();
            level.put("jsnum",jsnum);
            int xsnum = llist.stream().mapToInt(SchoolVo::getXsnum).sum();
            level.put("xsnum",xsnum);
            level.put("xxnum",llist.size());
            levels.add(level);
        }*/
        List<SchoolVo> schoollevel = schools.stream().filter(s -> s.getSchoollevel() == 1).collect(Collectors.toList());
        Map level = new HashMap();
        level.put("schoollevel",1);
        level.put("xxnum",schoollevel.size());
        levels.add(level);
        schoollevel = schools.stream().filter(s -> s.getSchoollevel() == 2).collect(Collectors.toList());
        level = new HashMap();
        level.put("schoollevel",2);
        level.put("xxnum",schoollevel.size());
        levels.add(level);
        schoollevel = schools.stream().filter(s -> s.getSchoollevel() == 3).collect(Collectors.toList());
        level = new HashMap();
        level.put("schoollevel",3);
        level.put("xxnum",schoollevel.size());
        levels.add(level);

        map.put("schools",schools);
        map.put("levels",levels);
        map.put("xxznum",schools.size());
        map.put("bjznum",schools.stream().mapToInt(SchoolVo::getBjnum).sum());
        map.put("jsznum",schools.stream().mapToInt(SchoolVo::getJsnum).sum());
        map.put("xsznum",schools.stream().mapToInt(SchoolVo::getXsnum).sum());
        return map;
    }

    /**
     * 教师统计
     * @Param []
     * @Author ywx
     * @Date 2021/7/9 15:21
     * @return java.util.Map
     **/
    public Map listTeacher(RegionParam param) {
        Map map = new HashMap();
        //教师性别统计
        List<RSTeacherVo> teachers = rSchoolAnalyzeMapper.listTeacherSex(param.getYear());
        //性别总数量
        Integer znum = teachers.stream().mapToInt(RSTeacherVo::getNum).sum();
        List<Map> sexes = new ArrayList<>();
        //性别分组列表
        /*Map<Integer, List<RSTeacherVo>> sexcollect = teachers.stream().collect(Collectors.groupingBy(RSTeacherVo::getUsersex));
        for(Map.Entry smap:sexcollect.entrySet()) {
            //性别列表
            List<RSTeacherVo> sexex2 = (List<RSTeacherVo>) smap.getValue();
            Map tsex = new HashMap();
            tsex.put("usersex",smap.getKey());
            Integer tnum = sexex2.stream().collect(Collectors.summingInt(RSTeacherVo::getNum));
            tsex.put("tnum",tnum);
            tsex.put("rate",N_Utils.formatProportion(tnum,znum));
            sexes.add(tsex);
        }*/
        //未知性别
        List<RSTeacherVo> sexex2 = teachers.stream().filter(t -> t.getUsersex() == 0).collect(Collectors.toList());
        Map tsex = new HashMap();
        tsex.put("usersex",0);
        Integer tnum = N_Utils.isListEmpty(sexex2)?0:sexex2.stream().collect(Collectors.summingInt(RSTeacherVo::getNum));
        tsex.put("tnum",tnum);
        tsex.put("rate", N_Utils.getIntegerDivideAndMulitiply(tnum,znum));
        sexes.add(tsex);
        //男
        sexex2 = teachers.stream().filter(t -> t.getUsersex() == 1).collect(Collectors.toList());
        tsex = new HashMap();
        tsex.put("usersex",1);
        tnum = N_Utils.isListEmpty(sexex2)?0:sexex2.stream().collect(Collectors.summingInt(RSTeacherVo::getNum));
        tsex.put("tnum",tnum);
        tsex.put("rate", N_Utils.getIntegerDivideAndMulitiply(tnum,znum));
        sexes.add(tsex);
        //女
        sexex2 = teachers.stream().filter(t -> t.getUsersex() == 2).collect(Collectors.toList());
        tsex = new HashMap();
        tsex.put("usersex",2);
        tnum = N_Utils.isListEmpty(sexex2)?0:sexex2.stream().collect(Collectors.summingInt(RSTeacherVo::getNum));
        tsex.put("tnum",tnum);
        tsex.put("rate", N_Utils.getIntegerDivideAndMulitiply(tnum,znum));
        sexes.add(tsex);
        map.put("sexes",sexes);
        //阶段性别分组列表
        Map<Integer, List<RSTeacherVo>> levelcollect = teachers.stream().collect(Collectors.groupingBy(RSTeacherVo::getLevel));
        List<Map> slevels = new ArrayList<>();
        for(Map.Entry lmap:levelcollect.entrySet()) {
            //阶段性别列表
            List<RSTeacherVo> lschools = (List<RSTeacherVo>) lmap.getValue();
            //阶段性别学校分组列表
            Map<Integer, List<RSTeacherVo>> schoolcollect = lschools.stream().collect(Collectors.groupingBy(RSTeacherVo::getSchoolid));
            List<Map> schools = new ArrayList<>();
            for(Map.Entry smap:schoolcollect.entrySet()) {
                //学校性别列表
                List<RSTeacherVo> lschools2 = (List<RSTeacherVo>) smap.getValue();
                RSTeacherVo school = lschools2.get(0);
                Map s = new HashMap();
                s.put("level",school.getLevel());
                s.put("schoolname",school.getSchoolname());
                s.put("shortname",school.getShortname());
                Integer wzxbnum = 0;//未知性别
                Integer boynum = 0;//男
                Integer girlnum = 0;//女
                for (RSTeacherVo sex : lschools2) {
                    Integer usersex = sex.getUsersex();
                    Integer num = sex.getNum();
                    if (usersex == 0) {
                        wzxbnum = num;
                    } else if (usersex == 1) {
                        boynum = num;
                    } else if (usersex == 2) {
                        girlnum = num;
                    }
                }
                s.put("wzxbnum",wzxbnum);
                s.put("boynum",boynum);
                s.put("girlnum",girlnum);
                s.put("xjnum",lschools2.stream().mapToInt(RSTeacherVo::getNum).sum());
                schools.add(s);
            }
            Map level = new HashMap();
            level.put("level",lmap.getKey());
            Integer lnum = lschools.stream().collect(Collectors.summingInt(RSTeacherVo::getNum));
            level.put("num",lnum);
            level.put("schools",schools);
            slevels.add(level);
        }
        map.put("slevels",slevels);

        //教师学段统计
        List<RSTeacherVo> list = rSchoolAnalyzeMapper.listTeacherSubject();
        Map<Integer, List<RSTeacherVo>> lcollect = list.stream().collect(Collectors.groupingBy(RSTeacherVo::getLevel));
        znum = list.size();
        List<Map> levels = new ArrayList<>();
        for(Map.Entry lmap:lcollect.entrySet()) {
            List<RSTeacherVo> levels2 = (List<RSTeacherVo>) lmap.getValue();
            Map l = new HashMap();
            Integer lnum = levels2.size();
            l.put("level",lmap.getKey());
            l.put("num",lnum);
            l.put("rate", N_Utils.getIntegerDivideAndMulitiply(lnum,znum));

            //教师科目统计
            List<Map> subjects = new ArrayList<>();
            levels2 = levels2.stream().sorted(Comparator.comparing(RSTeacherVo::getSubjectorder)).collect(Collectors.toList());
            Map<String, List<RSTeacherVo>> scollect = levels2.stream().collect(Collectors.groupingBy(RSTeacherVo::getSubjectid,LinkedHashMap::new, Collectors.toList()));
            for(Map.Entry smap:scollect.entrySet()) {
                List<RSTeacherVo> subjects2 = (List<RSTeacherVo>) smap.getValue();
                Map s = new HashMap();
                Integer snum = subjects2.size();
                RSTeacherVo subject = subjects2.get(0);
                s.put("level",subject.getLevel());
                s.put("subjectname",subject.getSubjectname());
                s.put("num",snum);
                s.put("rate", N_Utils.getIntegerDivideAndMulitiply(snum,lnum));
                subjects.add(s);
            }
            l.put("subjects",subjects);
            levels.add(l);
        }
        map.put("levels",levels);
        return map;
    }

    /**
     * 学生统计
     * @Param []
     * @Author ywx
     * @Date 2021/7/9 15:22
     * @return java.util.Map
     *
     * @param param*/
    public Map listStu(RegionParam param) {
        Map map = new HashMap();
        List<RSStuVo> students = rSchoolAnalyzeMapper.listStu(param);
        Integer znum = students.size();
        //学生性别统计
        List<Map> sexes = new ArrayList<>();
        /*Map<Integer, List<RSStuVo>> scollect = students.stream().collect(Collectors.groupingBy(RSStuVo::getUsersex));
        for(Map.Entry smap:scollect.entrySet()) {
            List<RSStuVo> sexes2 = (List<RSStuVo>) smap.getValue();
            Integer snum = sexes2.size();
            Map sexmap = new HashMap();
            sexmap.put("usersex",smap.getKey());
            sexmap.put("num",snum);
            sexmap.put("rate",N_Utils.formatProportion(snum,znum));
            sexes.add(sexmap);
        }*/
        //未知性别
        List<RSStuVo> sexes2 = students.stream().filter(stu->stu.getUsersex()==0).collect(Collectors.toList());
        Integer snum = sexes2.size();
        Map sexmap = new HashMap();
        sexmap.put("usersex",0);
        sexmap.put("num",snum);
        sexmap.put("rate", N_Utils.getIntegerDivideAndMulitiply(snum,znum));
        sexes.add(sexmap);
        //男
        sexes2 = students.stream().filter(stu->stu.getUsersex()==1).collect(Collectors.toList());
        snum = sexes2.size();
        sexmap = new HashMap();
        sexmap.put("usersex",1);
        sexmap.put("num",snum);
        sexmap.put("rate", N_Utils.getIntegerDivideAndMulitiply(snum,znum));
        sexes.add(sexmap);
        //女
        sexes2 = students.stream().filter(stu->stu.getUsersex()==2).collect(Collectors.toList());
        snum = sexes2.size();
        sexmap = new HashMap();
        sexmap.put("usersex",2);
        sexmap.put("num",snum);
        sexmap.put("rate", N_Utils.getIntegerDivideAndMulitiply(snum,znum));
        sexes.add(sexmap);
        map.put("sexes",sexes);

        //学生学段统计
        Map<Integer, List<RSStuVo>> lcollect = students.stream().collect(Collectors.groupingBy(RSStuVo::getLevel));
        List<Map> levels = new ArrayList<>();
        for(Map.Entry lmap:lcollect.entrySet()) {
            List<RSStuVo> levels2 = (List<RSStuVo>) lmap.getValue();
            Map l = new HashMap();
            Integer lnum = levels2.size();
            l.put("level",lmap.getKey());
            l.put("num",lnum);
            l.put("rate", N_Utils.getIntegerDivideAndMulitiply(lnum,znum));

            //阶段性别学校分组列表
            Map<Integer, List<RSStuVo>> schoolcollect = levels2.stream().collect(Collectors.groupingBy(RSStuVo::getSchoolid));
            List<Map> schools = new ArrayList<>();
            for(Map.Entry smap:schoolcollect.entrySet()) {
                //学校性别列表
                List<RSStuVo> lschools2 = (List<RSStuVo>) smap.getValue();
                RSStuVo school = lschools2.get(0);
                Map s = new HashMap();
                s.put("level",school.getLevel());
                s.put("schoolname",school.getSchoolname());
                s.put("shortname",school.getShortname());
                Integer wzxbnum = 0;//未知性别
                Integer boynum = 0;//男
                Integer girlnum = 0;//女
                for (RSStuVo sex : lschools2) {
                    Integer usersex = sex.getUsersex();
                    if (usersex == 0) {
                        wzxbnum = wzxbnum + 1;
                    } else if (usersex == 1) {
                        boynum = boynum + 1;
                    } else if (usersex == 2) {
                        girlnum = girlnum + 1;
                    }
                }
                s.put("wzxbnum",wzxbnum);
                s.put("boynum",boynum);
                s.put("girlnum",girlnum);
                s.put("xjnum",lschools2.size());
                schools.add(s);
            }
            l.put("schools",schools);

            //学生年级统计
            List<Map> grades = new ArrayList<>();
            Map<Integer, List<RSStuVo>> gcollect = levels2.stream().collect(Collectors.groupingBy(RSStuVo::getGradeid));
            for(Map.Entry gmap:gcollect.entrySet()) {
                List<RSStuVo> grades2 = (List<RSStuVo>) gmap.getValue();
                Map g = new HashMap();
                Integer gnum = grades2.size();
                g.put("gradeid",gmap.getKey());
                g.put("num",gnum);
                g.put("rate", N_Utils.getIntegerDivideAndMulitiply(gnum,lnum));
                grades.add(g);
            }
            l.put("grades",grades);
            levels.add(l);
        }
        map.put("levels",levels);
        return map;
    }

    public Map listAccess(RegionParam param) {
        Map map = new HashMap();
        List<Map> access = rSchoolAnalyzeMapper.listAccess(param);
        map.put("access",access);
        Map<Integer, List<Map>> lcollect = access.stream().collect(Collectors.groupingBy(m -> (Integer) m.get("level")));
        List<Map> levels = new ArrayList<>();
        for(Map.Entry lmap:lcollect.entrySet()) {
            List<Map> schools = (List<Map>) lmap.getValue();
            Map level = new HashMap();
            level.put("level",lmap.getKey());
            level.put("schools",schools);
            levels.add(level);
        }
        map.put("levels",levels);
        return map;
    }
}
