package com.xhkjedu.sapi.service.region;

import com.xhkjedu.sapi.mapper.region.SResourceMapper;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.region.RSubjectVo;
import com.xhkjedu.sapi.vo.region.ResourceTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName SResourceService
 * Description TODO 校园可视化--资源统计
 * Author WN
 * Date 2021/6/24 11:04
 **/
@Service
public class SResourceService {
    @Autowired
    private SResourceMapper sResourceMapper;

    //资源统计
    public Map listSchoolResourcesAnalyze(Integer schoolid){
        //资源数量
        Map map = sResourceMapper.getSchoolResourceNum(schoolid);
        Integer collectnum = sResourceMapper.getSchoolResourceCollectNum(schoolid);//收藏数量
        map.put("collect",collectnum);

        List<ResourceTypeVo> lst = sResourceMapper.listSchoolResourceSubjectTypenum(schoolid);//资源分类
        List<ResourceTypeVo> types = new ArrayList<>();
        List<RSubjectVo> subjects = new ArrayList<>();
        RSubjectVo maxsubject = null;
        if(lst!=null && lst.size()>0){
            Integer totalnum = lst.stream().mapToInt(ResourceTypeVo::getRnum).sum();
            for(ResourceTypeVo r : lst){
                //找科目
                subjects = this.listResourceSubjects(totalnum,lst);

                //分类
                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);
                }
            }
            //获取最高科目
            maxsubject = subjects.stream().max(Comparator.comparing(RSubjectVo::getSnum)).get();
        }


        map.put("maxsubject",maxsubject);
        map.put("subjects",subjects);
        map.put("types",types);

        //资源格式
        List<ResourceTypeVo> classlst = sResourceMapper.listSchoolResourceSubjectClassnum(schoolid);
        List<Map> rtnclass = new ArrayList<>();
        if(classlst!=null && classlst.size()>0){
            Integer totalnum = classlst.stream().mapToInt(ResourceTypeVo::getRnum).sum();
            for(ResourceTypeVo r : classlst){
                Map mclass = new HashMap();
                mclass.put("rclass",r.getRtype());
                mclass.put("rnum",r.getRnum());

                double rate = N_Utils.getIntegerDivideAndMulitiply(r.getRnum(),totalnum);
                r.setRrate(rate);
                mclass.put("rrate",rate);
                rtnclass.add(mclass);
            }
        }

        map.put("rclass",rtnclass);
        Integer srnum = sResourceMapper.getSchoolResourceTotalNum(schoolid);//校本资源数量
        //计算校本资源占比
        double srate = 0;

        if(map.get("resourcenum")!=null){
            Integer rnum = Integer.parseInt(map.get("resourcenum").toString());
            if(rnum>0){
                srate = N_Utils.getIntegerDivideAndMulitiply(srnum,rnum);
            }
        }
        map.put("xbrate",srate);//校本资源占比

        return map;
    }

    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;
    }

    //资源统计--处理科目
    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;
    }

    //试题统计
    public Map listSchoolQuestionAnalyze(Integer schoolid){
        Map map = new HashMap();

        //获取科目下试题数量
        List<RSubjectVo> lst = sResourceMapper.listSchoolQuestionSubjectnum(schoolid);
        Integer totalnum = 0;
        RSubjectVo maxsubject = null;
        List<Map> subjects = new ArrayList<>();
        if(lst!=null && lst.size()>0){
            totalnum = lst.stream().mapToInt(RSubjectVo::getSnum).sum();

            //最高科目
            maxsubject = lst.stream().max(Comparator.comparing(RSubjectVo::getSnum)).get();
            double maxrate = N_Utils.getIntegerDivideAndMulitiply(maxsubject.getSnum(),totalnum);
            maxsubject.setSrate(maxrate);

            for(RSubjectVo r : lst){
                Map ms = new HashMap();
                ms.put("subjectname",r.getSubjectname());
                ms.put("snum",r.getSnum());

                double rate = N_Utils.getIntegerDivideAndMulitiply(r.getSnum(),totalnum);
                ms.put("srate",rate);

                subjects.add(ms);
            }
        }


        map.put("subjects",subjects);
        map.put("maxsubject",maxsubject);
        map.put("quenum",totalnum);

        //校本试题科目数量
        List<RSubjectVo> xblst = sResourceMapper.getSchoolQuestionXbSubjectnum(schoolid);
        List<Map> rtxb = new ArrayList<>();
        int xbtotal = 0;
        RSubjectVo xbmaxsubject = null;
        if(xblst!=null && xblst.size()>0){
            xbtotal = xblst.stream().mapToInt(RSubjectVo::getSnum).sum();
            xbmaxsubject = xblst.stream().max(Comparator.comparing(RSubjectVo::getSnum)).get();
            double xbmaxrate = N_Utils.getIntegerDivideAndMulitiply(xbmaxsubject.getSnum(),xbtotal);
            xbmaxsubject.setSrate(xbmaxrate);

            for(RSubjectVo s : xblst){
                Map mxb = new HashMap();
                mxb.put("subjectid",s.getSubjectid());
                mxb.put("subjectname",s.getSubjectname());
                mxb.put("snum",s.getSnum());

                double srate = N_Utils.getIntegerDivideAndMulitiply(s.getSnum(),xbtotal);
                mxb.put("srate",srate);

                rtxb.add(mxb);
            }
        }

        map.put("xbquenum",xbtotal);
        map.put("xbmaxsubject",xbmaxsubject);
        map.put("xbsubjects",rtxb);

        return map;
    }

    //校本资源统计
    public Map listSchoolResourceSchool(Integer schoolid){
        Map map = new HashMap();
        List<RSubjectVo> lst = sResourceMapper.listSchoolResourceSchool(schoolid);
        Integer totalnum = 0;
        List<Map> subjects = new ArrayList<>();
        RSubjectVo maxsubject = null;
        if(lst != null && lst.size() > 0){
            totalnum = lst.stream().mapToInt(RSubjectVo::getSnum).sum();
            //最高科目
            maxsubject = lst.stream().max(Comparator.comparing(RSubjectVo::getSnum)).get();
            double maxrate = N_Utils.getIntegerDivideAndMulitiply(maxsubject.getSnum(),totalnum);
            maxsubject.setSrate(maxrate);

            for(RSubjectVo s : lst){
                double rate = N_Utils.getIntegerDivideAndMulitiply(s.getSnum(),totalnum);

                Map ms = new HashMap();
                ms.put("subjectname",s.getSubjectname());
                ms.put("snum",s.getSnum());

                ms.put("srate",rate);

                subjects.add(ms);
            }
        }

        map.put("subjects",subjects);
        map.put("maxsubject",maxsubject);

        return map;
    }

    //热门资源统计
    public List<Map> listSchoolResourceHot(Integer schoolid){
        return sResourceMapper.listSchoolResourceHot(schoolid);
    }

    //校本资源贡献量统计
    public Map listSchoolResourceForSchool(Integer schoolid,Integer begintime,Integer endtime){
        Map map = new HashMap();
        List<ResourceTypeVo> lst = sResourceMapper.listSchoolResourceForSchool(schoolid,begintime,endtime);

        Integer totalnum = 0;
        ResourceTypeVo maxtype = null;
        List<RSubjectVo> subjects = new ArrayList<>();
        List<ResourceTypeVo> types = new ArrayList<>();

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

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

            for(ResourceTypeVo r : lst){
                //找科目
                subjects = this.listResourceSubjects(totalnum,lst);

                //分类
                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);
                    rtypevo.setRnum(typenum);
                    types.add(rtypevo);
                }
            }

            maxtype = lst.stream().max(Comparator.comparing(ResourceTypeVo::getRnum)).get();
            double maxrate = N_Utils.getIntegerDivideAndMulitiply(maxtype.getRnum(),totalnum);
            maxtype.setRrate(maxrate);
        }

        map.put("subjects",subjects);
        map.put("maxtype",maxtype);
        map.put("types",types);
        map.put("totalnum",totalnum);

        return map;
    }



}
