package com.xhkjedu.sapi.service.region;

import com.xhkjedu.sapi.mapper.region.RegionResourceMapper;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.region.*;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName RegionResourceService
 * Description TODO
 * Author WN
 * Date 2021/5/27 15:41
 **/
@Service
public class RegionResourceService {
    @Resource
    RegionResourceMapper regionResourceMapper;

    //资源统计--资源分类
    public Map listResourcesAnalyze(){
        //资源数量
        Map map = regionResourceMapper.getResourceNum();
        Integer collectnum = regionResourceMapper.getResourceCollectNum();//收藏数量
        map.put("collect",collectnum);

        List<ResourceTypeVo> lst = regionResourceMapper.listResourceSubjectTypenum();
        List<RLevelVo> levels = new ArrayList<>();
        List<ResourceTypeVo> types = new ArrayList<>();

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

            Integer totalnum = lst.stream().mapToInt(ResourceTypeVo::getRnum).sum();

            for(ResourceTypeVo r : lst){
                //先找学段
                if(!isExistLeves(r.getLevel(),levels)){
                    //学段不存在,则新创建学段
                    RLevelVo rlvo = new RLevelVo();
                    rlvo.setLevel(r.getLevel());
                    //学段下所有
                    List<ResourceTypeVo> stypes = lst.stream().filter(resourceTypeVo -> resourceTypeVo.getLevel().equals(r.getLevel())).collect(Collectors.toList());
                    Integer levelnum = stypes.stream().mapToInt(ResourceTypeVo::getRnum).sum();
                    double levelrate = N_Utils.getIntegerDivideAndMulitiply(levelnum,totalnum);

                    List<RSubjectVo> subjects = listResourceSubjects(totalnum,stypes);//学段下科目
                    RSubjectVo maxsubject = subjects.stream().max(Comparator.comparing(RSubjectVo::getSnum)).get();

                    rlvo.setSubjects(subjects);
                    rlvo.setLevelrate(levelrate);
                    rlvo.setMaxsubject(maxsubject.getSubjectname());
                    rlvo.setMaxnum(maxsubject.getSnum());
                    rlvo.setMaxrate(maxsubject.getSrate());

                    levels.add(rlvo);
                }

                //资源分类
                if(!isExistTypes(r.getRtype(),types)){
                    //指定分类集合
                    List<ResourceTypeVo> stypes = lst.stream().filter(resourceTypeVo -> resourceTypeVo.getRtype().equals(r.getRtype())).collect(Collectors.toList());
                    Integer typenum = stypes.stream().mapToInt(ResourceTypeVo::getRnum).sum();
                    double rate = N_Utils.getIntegerDivideAndMulitiply(typenum,totalnum);

                    ResourceTypeVo rtypevo = new ResourceTypeVo();
                    rtypevo.setRtype(r.getRtype());
                    rtypevo.setRrate(rate);
                    types.add(rtypevo);
                }

            }
        }

        map.put("levels",levels);
        map.put("types",types);
        return map;
    }

    //资源统计--处理科目
    private List<RSubjectVo> listResourceSubjects(Integer totalnum,List<ResourceTypeVo> lst){
        List<RSubjectVo> subjects = new ArrayList<>();
        for(ResourceTypeVo r : lst){
            if(!isExistSubjects(r.getSubjectid(),subjects)){
                //科目下资源
                List<ResourceTypeVo> stypes = lst.stream().filter(resourceTypeVo -> resourceTypeVo.getSubjectid().equals(r.getSubjectid())).collect(Collectors.toList());

                Integer num = stypes.stream().mapToInt(ResourceTypeVo::getRnum).sum();
                double rate = N_Utils.getIntegerDivideAndMulitiply(num,totalnum);

                RSubjectVo rs = new RSubjectVo();
                rs.setSubjectid(r.getSubjectid());
                rs.setSubjectname(r.getSubjectname());
                rs.setSnum(num);
                rs.setSrate(rate);
                rs.setTypes(stypes);

                subjects.add(rs);
            }
        }

        return subjects;
    }



    private boolean isExistLeves(Integer level,List<RLevelVo> list){
        for (RLevelVo l : list){
            if(l.getLevel().equals(level)){
                return true;
            }
        }
        return false;
    }
    private boolean isExistTypes(Integer resourcetype,List<ResourceTypeVo> list){
        for (ResourceTypeVo rt : list){
            if(rt.getRtype().equals(resourcetype)){
                return true;
            }
        }
        return false;
    }
    private boolean isExistSubjects(String subjectid,List<RSubjectVo> list){
        for (RSubjectVo rt : list){
            if(rt.getSubjectid().equals(subjectid)){
                return true;
            }
        }
        return false;
    }

    //试题统计
    public Map listQuestionSubjectnum(){
        Map map = new HashMap();
        List<ResourceTypeVo> lst = regionResourceMapper.listQuestionSubjectnum();
        List<RLevelVo> levels = new ArrayList<>();
        Integer totalnum = 0;

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

            totalnum = lst.stream().mapToInt(ResourceTypeVo::getRnum).sum();//总试题数量

            for(ResourceTypeVo r : lst){
                if(!isExistLeves(r.getLevel(),levels)){
                    //获取学段下内容
                    List<ResourceTypeVo> sublist = lst.stream().filter(resourceTypeVo -> resourceTypeVo.getLevel().equals(r.getLevel())).collect(Collectors.toList());
                    Integer levelnum = sublist.stream().mapToInt(ResourceTypeVo::getRnum).sum();//学段下数量
                    double levelrate = N_Utils.getIntegerDivideAndMulitiply(levelnum,totalnum);

                    List<RSubjectVo> subjects = new ArrayList<>();
                    for(ResourceTypeVo rs : sublist){
                        RSubjectVo rSubjectVo = new RSubjectVo();
                        rSubjectVo.setSubjectname(rs.getSubjectname());
                        rSubjectVo.setSnum(rs.getRnum());
                        double rrate = N_Utils.getIntegerDivideAndMulitiply(rs.getRnum(),levelnum);
                        rSubjectVo.setSrate(rrate);

                        subjects.add(rSubjectVo);
                    }
                    //最高学科
                    RSubjectVo maxsubject = subjects.stream().max(Comparator.comparing(RSubjectVo::getSnum)).get();

                    RLevelVo rLevelVo = new RLevelVo();
                    rLevelVo.setLevel(r.getLevel());
                    rLevelVo.setLevelnum(levelnum);
                    rLevelVo.setLevelrate(levelrate);
                    rLevelVo.setMaxsubject(maxsubject.getSubjectname());
                    rLevelVo.setMaxnum(maxsubject.getSnum());
                    rLevelVo.setMaxrate(maxsubject.getSrate());
                    rLevelVo.setSubjects(subjects);

                    levels.add(rLevelVo);

                }
            }
        }

        map.put("questionnum",totalnum);
        map.put("levels",levels);

        return map;
    }

    //资源统计--校本资源统计
    public List<RLevelVo> listResourceSchool(){
        List<ResourceTypeVo> lst = regionResourceMapper.listResourceSchool();
        List<RLevelVo> levels = new ArrayList<>();

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

            Integer totalnum = lst.stream().mapToInt(ResourceTypeVo::getRnum).sum();

            for(ResourceTypeVo r : lst){
                //先找学段
                if(!isExistLeves(r.getLevel(),levels)){
                    //学段不存在,则新创建学段
                    RLevelVo rlvo = new RLevelVo();
                    rlvo.setLevel(r.getLevel());
                    //学段下所有
                    List<ResourceTypeVo> stypes = lst.stream().filter(resourceTypeVo -> resourceTypeVo.getLevel().equals(r.getLevel())).collect(Collectors.toList());
                    Integer levelnum = stypes.stream().mapToInt(ResourceTypeVo::getRnum).sum();
                    double levelrate = N_Utils.getIntegerDivideAndMulitiply(levelnum,totalnum);

                    List<RSubjectVo> subjects = new ArrayList<>();
                    for(ResourceTypeVo rs : stypes){
                        RSubjectVo rSubjectVo = new RSubjectVo();
                        rSubjectVo.setSubjectname(rs.getSubjectname());
                        rSubjectVo.setSnum(rs.getRnum());
                        double rrate = N_Utils.getIntegerDivideAndMulitiply(rs.getRnum(),levelnum);
                        rSubjectVo.setSrate(rrate);

                        subjects.add(rSubjectVo);
                    }

                    RSubjectVo maxsubject = subjects.stream().max(Comparator.comparing(RSubjectVo::getSnum)).get();

                    rlvo.setLevelnum(levelnum);
                    rlvo.setSubjects(subjects);
                    rlvo.setLevelrate(levelrate);
                    rlvo.setMaxsubject(maxsubject.getSubjectname());
                    rlvo.setMaxnum(maxsubject.getSnum());
                    rlvo.setMaxrate(maxsubject.getSrate());

                    levels.add(rlvo);
                }
            }
        }

       return levels;
    }

    //资源统计--热门资源统计
    public List<Map> listResourceHot(){
        return regionResourceMapper.listResourceHot();
    }

    //资源统计--近一个月校本贡献
    public Map listResourceForSchool(Integer begintime,Integer endtime){
        Map map = new HashMap();

        List<RSResourceVo> lst = regionResourceMapper.listResourceForSchool(begintime,endtime);

        List<RSchoolVo> schools = new ArrayList<>();//学校下资源统计
        List<RSResourceVo> types = new ArrayList<>();//资源类型统计
        Integer totalnum = 0;
        RSchoolVo maxschool = null;

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

            totalnum = lst.stream().mapToInt(RSResourceVo::getRnum).sum();

            for (RSResourceVo r : lst){
                if(!isExistSchools(r.getSchoolid(),schools)){
                    //获取该学校下的所有资源
                    List<RSResourceVo> sllst = lst.stream().filter(rsResourceVo -> rsResourceVo.getSchoolid().equals(r.getSchoolid())).collect(Collectors.toList());
                    Integer scnum = sllst.stream().mapToInt(RSResourceVo::getRnum).sum();//学校下总数量
                    double scrate = N_Utils.getIntegerDivideAndMulitiply(scnum,totalnum);
                    RSchoolVo rsvo = new RSchoolVo();
                    rsvo.setSchoolid(r.getSchoolid());
                    rsvo.setSchoolname(r.getSchoolname());
                    rsvo.setShortname(r.getShortname());
                    rsvo.setSnum(scnum);
                    rsvo.setSrate(scrate);
                    rsvo.setRtypelst(sllst);

                    //获取学校下不同类型资源
                    List<RSResourceVo> rsrlst = sllst.stream().filter(rsResourceVo -> rsResourceVo.getRtype().equals(r.getRtype())).collect(Collectors.toList());
                    rsvo.setRtypelst(rsrlst);

                    schools.add(rsvo);
                }

                //类型
                if(!isExistSchoolType(r.getRtype(),types)){
                    List<RSResourceVo> sllst = lst.stream().filter(rsResourceVo -> rsResourceVo.getRtype().equals(r.getRtype())).collect(Collectors.toList());
                    Integer tnum = sllst.stream().mapToInt(RSResourceVo::getRnum).sum();//类型下总数量
                    double trate = N_Utils.getIntegerDivideAndMulitiply(tnum,totalnum);

                    RSResourceVo rsr = new RSResourceVo();
                    rsr.setRtype(r.getRtype());
                    rsr.setRnum(tnum);
                    rsr.setRrate(trate);
                    types.add(rsr);
                }
            }

            //取出最高学校
            maxschool = schools.stream().max(Comparator.comparing(RSchoolVo::getSnum)).get();
        }
        map.put("resourcenum",totalnum);
        map.put("maxschool",maxschool);
        map.put("types",types);
        map.put("schools",schools);

        return map;
    }

    private boolean isExistSchools(Integer schoolid,List<RSchoolVo> list){
        for (RSchoolVo rs : list){
            if(rs.getSchoolid().equals(schoolid)){
                return true;
            }
        }
        return false;
    }
    private boolean isExistSchoolType(Integer rtype,List<RSResourceVo> list){
        for (RSResourceVo rs : list){
            if(rs.getRtype().equals(rtype)){
                return true;
            }
        }
        return false;
    }

    //试题统计--近一个月校本贡献
    public Map listQuestionForSchool(Integer begintime,Integer endtime){
        Map map = new HashMap();
        List<RSchoolVo> schools = regionResourceMapper.listQuestionForSchool(begintime,endtime);
        RSchoolVo maxschool = null;
        Integer totalnum = 0;
        if(schools!=null && schools.size()>0){
            totalnum = schools.stream().mapToInt(RSchoolVo::getSnum).sum();
            maxschool = schools.stream().max(Comparator.comparing(RSchoolVo::getSnum)).get();
            double maxschoolrate = N_Utils.getIntegerDivideAndMulitiply(maxschool.getSnum(),totalnum);
            maxschool.setSrate(maxschoolrate);
        }

        map.put("questionnum",totalnum);
        map.put("maxschool",maxschool);
        map.put("schools",schools);

        return map;
    }



}
