package com.xhkjedu.sapi.service.analyze;

import com.xhkjedu.sapi.mapper.analyze.SchoolResourceMapper;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.analyze.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName SchoolResourceService
 * Description TODO 资源概况
 * Author WN
 * Date 2020/8/8 23:38
 **/
@Service
public class SchoolResourceService {
    @Autowired
    private SchoolResourceMapper schoolResourceMapper;

    //资源统计--总数量
    public SchoolResourceNumVo getSchoolResourceNum(Integer schoolid){
        return schoolResourceMapper.getSchoolResourceNum(schoolid);
    }

    //资源分布--资源类型统计
    public List<SchoolResourceTypeNumVo> listSchoolResourceTypeNum(Integer schoolid){
        List<SchoolResourceTypeNumVo> lst = schoolResourceMapper.listSchoolResourceTypeNum(schoolid);
        //前端已经计算，接口不再计算
        /*Integer totalnum = lst.stream().mapToInt(SchoolResourceTypeNumVo::getResourcenum).sum();
        for(SchoolResourceTypeNumVo sr : lst){
            sr.setResourcerate(N_Utils.getIntegerDivideAndMulitiply(sr.getResourcenum(),totalnum));
        }*/

        return lst;
    }

    //资源分布--资源格式统计
    public List<SchoolResourceClassNumVo> listSchoolResourceClassNum(Integer schoolid){
        List<SchoolResourceClassNumVo> lst = schoolResourceMapper.listSchoolResourceClassNum(schoolid);
        //前端已经计算，接口不再计算
        /*Integer totalnum = lst.stream().mapToInt(SchoolResourceClassNumVo::getResourcenum).sum();
        for(SchoolResourceClassNumVo sr : lst){
            sr.setResourcerate(N_Utils.getIntegerDivideAndMulitiply(sr.getResourcenum(),totalnum));
        }*/

        return lst;
    }

    //资源上传量统计
    public List<SchoolResourceUploadNumVo> listSchoolResourceUploadNum(Integer schoolid,Integer type){
        Long endtime = N_Utils.getNowtimestamp();
        Long starttime = N_Utils.getLongGoTime(type);
        List<SchoolResourceUploadNumVo> lst = schoolResourceMapper.listSchoolResourceUploadNum(schoolid,starttime,endtime);
        //获取资源resourcetyp的值
        List<Integer> resourcetypes = schoolResourceMapper.listAllResourceType();
        for(SchoolResourceUploadNumVo rupload : lst){
            List<SchoolResourceTypeNumVo> rtypelist = rupload.getRtypelist();
            rtypelist = saveListSchoolResourceTypeNumVo(rtypelist);

            Map<Integer, SchoolResourceTypeNumVo> typeMap = rtypelist.stream().collect(Collectors.toMap(SchoolResourceTypeNumVo::getResourcetype, obj -> obj, (key1, key2) -> key1));
            List<SchoolResourceTypeNumVo> newTypes = new ArrayList<>();
            for(Integer rt : resourcetypes){
                if(typeMap.containsKey(rt)){
                    newTypes.add(typeMap.get(rt));
                }else{
                    SchoolResourceTypeNumVo srt = new SchoolResourceTypeNumVo();
                    srt.setResourcetype(rt);
                    newTypes.add(srt);
                }
            }
            rupload.setRtypelist(newTypes);
        }
        return lst;
    }

    //资源分类1课件2导学案3习题4视频库 不存在时处理成0
    private List<SchoolResourceTypeNumVo> saveListSchoolResourceTypeNumVo(List<SchoolResourceTypeNumVo> rtypelist){
        //判断是否存在 1课件2导学案3习题4视频库5教学设计
        for(int t=1;t<6;t++){
            boolean istype1 = true;
            if(t==1){
                istype1 = rtypelist.stream().anyMatch(task -> task.getResourcetype()==1);
            }else if(t==2){
                istype1 = rtypelist.stream().anyMatch(task -> task.getResourcetype()==2);
            }else if(t==3){
                istype1 = rtypelist.stream().anyMatch(task -> task.getResourcetype()==3);
            }else if(t==4){
                istype1 = rtypelist.stream().anyMatch(task -> task.getResourcetype()==4);
            }else if(t==5){
                istype1 = rtypelist.stream().anyMatch(task -> task.getResourcetype()==5);
            }

            if(!istype1){
                SchoolResourceTypeNumVo stype = saveSchoolResourceTypeNumVo(t);
                rtypelist.add(stype);
            }
        }

        rtypelist.sort(Comparator.comparing(SchoolResourceTypeNumVo::getResourcetype));

        return rtypelist;
    }

    private SchoolResourceTypeNumVo saveSchoolResourceTypeNumVo(Integer type){
        SchoolResourceTypeNumVo stype = new SchoolResourceTypeNumVo();
        stype.setResourcetype(type);
        stype.setResourcenum(0);
        stype.setResourcerate(0);
        return stype;
    }

    //资源排行榜
    public List<SchoolResourceRankVo> listSchoolResourceRank(Integer schoolid,Integer type,String subjectid,Integer gradeid){
        Long endtime = N_Utils.getNowtimestamp();
        Long starttime = N_Utils.getLongGoTime(type);
        List<SchoolResourceRankVo> lst = schoolResourceMapper.listSchoolResourceRanks(schoolid,subjectid,gradeid,starttime,endtime);

        for(SchoolResourceRankVo rank : lst){
            List<SchoolResourceTypeNumVo> typenums = rank.getRtypelist();
            Integer totalnum = typenums.stream().mapToInt(SchoolResourceTypeNumVo::getResourcenum).sum();
            rank.setTotalnum(totalnum);

            typenums = saveListSchoolResourceTypeNumVo(typenums);

        }
        //集合中根据总资源量排序
        //lst.sort(Comparator.comparing(SchoolResourceRankVo::getTotalnum).reversed());

        //集合中根据总资源量排序，并取前10条
        List<SchoolResourceRankVo> rtnlist = lst.stream().sorted(Comparator.comparing(SchoolResourceRankVo::getTotalnum).reversed()).limit(10).collect(Collectors.toList());

        return rtnlist;
    }

    //结构性试题统计--各学科试题量
    public List<SchoolResourceQuestionVo> listSchoolResourceQuestion(SchoolParams params){
        if (!N_Utils.isEmptyInteger(params.getType())) {
            Long endtime = N_Utils.getNowtimestamp();
            Long starttime = N_Utils.getLongGoTime(params.getType());
            params.setStarttime(starttime.intValue());
            params.setStoptime(endtime.intValue());
        }
        List<SchoolResourceQuestionVo> lst = schoolResourceMapper.listSchoolResourceQuestion(params);
        Integer totalnum = lst.stream().mapToInt(SchoolResourceQuestionVo::getQuestionnum).sum();
        for(SchoolResourceQuestionVo sr : lst){
            sr.setQuesitonrate(N_Utils.getIntegerDivideAndMulitiply(sr.getQuestionnum(),totalnum));
        }

        return lst;
    }

    //结构性试题统计--年级试题量
    public List<SchoolResourceQuestionVo> listSchoolResourceQuestionGrade(SchoolParams params){
        if (!N_Utils.isEmptyInteger(params.getType())) {
            Long endtime = N_Utils.getNowtimestamp();
            Long starttime = N_Utils.getLongGoTime(params.getType());
            params.setStarttime(starttime.intValue());
            params.setStoptime(endtime.intValue());
        }
        return schoolResourceMapper.listSchoolResourceQuestionGrade(params);
    }

    //结构性试题统计--教师上传排行
    public List<SchoolResourceQuestionRankVo> listSchoolResourceQuestionRank(SchoolParams params){
        if (!N_Utils.isEmptyInteger(params.getType())) {
            Long endtime = N_Utils.getNowtimestamp();
            Long starttime = N_Utils.getLongGoTime(params.getType());
            params.setStarttime(starttime.intValue());
            params.setStoptime(endtime.intValue());
        }
        return schoolResourceMapper.listSchoolResourceQuestionRank(params);
    }

    //本月新增情况
    public List<SchoolAddNumVo> listMonthAdd(Integer schoolid) {
        return schoolResourceMapper.listMonthAdd(schoolid);
    }

    //按年级资源统计列表
    public List<SchoolResourceGradeNumVo> listSchoolResourceGrade(Integer schoolid) {
        return schoolResourceMapper.listSchoolResourceGrade(schoolid);
    }

    //按科目资源统计列表
    public List<SchoolResourceSubjectNumVo> listSchoolResourceSubject(Integer schoolid) {
        return schoolResourceMapper.listSchoolResourceSubject(schoolid);
    }

    //资源上传详情排行
    public List<SchoolUserResourceNumVo> listSchoolResourceRank2(SchoolParams params) {
        if (!N_Utils.isEmptyInteger(params.getType())) {
            Long endtime = N_Utils.getNowtimestamp();
            Long starttime = N_Utils.getLongGoTime(params.getType());
            params.setStarttime(starttime.intValue());
            params.setStoptime(endtime.intValue());
        }
        return schoolResourceMapper.listSchoolResourceRank2(params);
    }

    //结构性试题数量统计
    public SchoolQuestionVo getSchoolQuestionNum(Integer schoolid) {
        return schoolResourceMapper.getSchoolQuestionNum(schoolid);
    }

    //获取按年级试题上传变化列表
    public List<SchoolQuestionUploadNumVo> listSchoolGQuestionUpload(Integer schoolid, Integer type) {
        Long endtime = N_Utils.getNowtimestamp();
        Long starttime = N_Utils.getLongGoTime(type);
        return schoolResourceMapper.listSchoolGQuestionUpload(schoolid, starttime, endtime);
    }

    //获取按科目试题上传变化列表
    public List<SchoolQuestionUploadNumVo> listSchoolSQuestionUpload(Integer schoolid, Integer type) {
        Long endtime = N_Utils.getNowtimestamp();
        Long starttime = N_Utils.getLongGoTime(type);
        return schoolResourceMapper.listSchoolSQuestionUpload(schoolid, starttime, endtime);
    }
}
