package com.xhkjedu.sapi.service.region;

import com.xhkjedu.sapi.mapper.region.RResourceAnalyzeMapper;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.region.RLevelVo;
import com.xhkjedu.sapi.vo.region.RSResourceVo;
import com.xhkjedu.sapi.vo.region.RSchoolVo;
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 RResourceService
 * Description TODO 区域资源统计
 * Author WN
 * Date 2021/7/9 9:37
 **/
@Service
public class RResourceAnalyzeService {
    @Autowired
    private RResourceAnalyzeMapper rResourceAnalyzeMapper;

    //资源统计——资源试题数量
    public Map getResourceQuestionNum(){
        Map map = rResourceAnalyzeMapper.getRResourceNum();
        Integer collectnum = rResourceAnalyzeMapper.getRResourceCollectNum();
        Integer quenum = rResourceAnalyzeMapper.getRQuestionNum();

        map.put("collectnum",collectnum);
        map.put("questionnum",quenum);

        return map;
    }

    //资源学段，学段下分类统计
    public Map listRResourceLevelTypeNum(){
        //资源学段、分类
        List<ResourceTypeVo> list = rResourceAnalyzeMapper.listRResourceLevelTypeNum();
        Map map = new HashMap();

        List<RLevelVo> levels = new ArrayList<>();
        List<ResourceTypeVo> types = new ArrayList<>();
        List<ResourceTypeVo> sorttypes = new ArrayList<>();

        if(list!=null && list.size()>0){
            Integer totalnum = list.stream().mapToInt(ResourceTypeVo::getRnum).sum();//总数量
            for(ResourceTypeVo r : list){
                //判断学段
                if(!isExistLeves(r.getLevel(),levels)){

                    //学段下所有资源分类
                    List<ResourceTypeVo> leveltypes = list.stream().filter(resourceTypeVo -> resourceTypeVo.getLevel().equals(r.getLevel())).collect(Collectors.toList());
                    Integer levelnum = leveltypes.stream().mapToInt(ResourceTypeVo::getRnum).sum();
                    double levelrate = N_Utils.getIntegerDivideAndMulitiply(levelnum,totalnum);

                    RLevelVo rLevelVo = new RLevelVo();
                    rLevelVo.setLevel(r.getLevel());
                    rLevelVo.setLevelnum(levelnum);
                    rLevelVo.setLevelrate(levelrate);

                    levels.add(rLevelVo);
                }

                //判断分类
                if(!isExistTypes(r.getRtype(),types)){
                    //资源分类
                    List<ResourceTypeVo> stypes = list.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 rtvo = new ResourceTypeVo();
                    rtvo.setRtype(r.getRtype());
                    rtvo.setRnum(typenum);
                    rtvo.setRrate(rate);

                    types.add(rtvo);
                }
            }

            sorttypes = types.stream().sorted(Comparator.comparing(ResourceTypeVo::getRtype)).collect(Collectors.toList());
        }
        map.put("level",levels);
        map.put("types",sorttypes);

        //资源格式
        List<ResourceTypeVo> classlist = rResourceAnalyzeMapper.listRResourceClassNum();
        List<Map> classmap = new ArrayList<>();
        if(classlist!=null && classlist.size()>0){
            Integer totalnum = classlist.stream().mapToInt(ResourceTypeVo::getRnum).sum();//总数量
            for(ResourceTypeVo rvo : classlist){
                double rate = N_Utils.getIntegerDivideAndMulitiply(rvo.getRnum(),totalnum);
                Map clitem = new HashMap();
                clitem.put("classid",rvo.getRtype());
                clitem.put("cnum",rvo.getRnum());
                clitem.put("crate",rate);

                classmap.add(clitem);
            }
        }
        map.put("classes",classmap);

        return map;

    }

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

    //试题学段占比、试题使用统计
    public Map listRQuestionLevelNumAnUse(){
        Map map = new HashMap();

        //试题学段占比
        List<RLevelVo> list = rResourceAnalyzeMapper.listRQuestionLevelNum();

        List<Map> levels = new ArrayList<>();
        if(list!=null && list.size()>0){
            Integer totalnum = list.stream().mapToInt(RLevelVo::getLevelnum).sum();//总数量
            for(RLevelVo l : list){
                double rate = N_Utils.getIntegerDivideAndMulitiply(l.getLevelnum(),totalnum);
                Map mlitem = new HashMap();
                mlitem.put("level",l.getLevel());
                mlitem.put("levelnum",l.getLevelnum());
                mlitem.put("levelrate",rate);

                levels.add(mlitem);
            }
        }

        map.put("levels",levels);

        //试题使用情况统计
        List<RSchoolVo> schoollist = rResourceAnalyzeMapper.listRQuestionUse();
        if(schoollist!=null && schoollist.size()>0){
            Integer totalnum = schoollist.stream().mapToInt(RSchoolVo::getSnum).sum();//总数量
            for(RSchoolVo s : schoollist){
                double rate = N_Utils.getIntegerDivideAndMulitiply(s.getSnum(),totalnum);
                s.setSrate(rate);
            }
        }

        map.put("schools",schoollist);
        return map;
    }

    //校本资源统计
    public List<RSchoolVo> listRResourceXbType(){
        List<RSResourceVo> list = rResourceAnalyzeMapper.listRResourceXbType();

        List<RSchoolVo> schools = new ArrayList<>();

        if(list!=null && list.size()>0){
            for(RSResourceVo r : list){
                //先找学校，然后再获取学校下各类型资源
                if(!isExistSchools(r.getSchoolid(),schools)){
                    //先获取该学校下所有的资源
                    List<RSResourceVo> schoollists = list.stream().filter(rsResourceVo -> rsResourceVo.getSchoolid().equals(r.getSchoolid())).collect(Collectors.toList());
                    Integer schoolnum = schoollists.stream().mapToInt(RSResourceVo::getRnum).sum();//学校下总数量
                    RSchoolVo rsvo = new RSchoolVo();
                    rsvo.setSchoolid(r.getSchoolid());
                    rsvo.setSchoolname(r.getSchoolname());
                    rsvo.setShortname(r.getShortname());
                    rsvo.setLevel(r.getLevel());
                    rsvo.setSnum(schoolnum);
                    rsvo.setRtypelst(schoollists);

                    schools.add(rsvo);
                }
            }
        }

        return schools;
    }

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