package com.xhkjedu.sapi.service.region;

import com.xhkjedu.sapi.mapper.region.RAppAnalyzeMapper;
import com.xhkjedu.sapi.vo.region.RegionParam;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.region.RAppVo;
import org.springframework.stereotype.Service;

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

/**
 * @author ywx
 * @classnam RAppAnalyzeService
 * @description 区域后台统计--应用统计
 * @date 2021/7/15 8:53
 **/
@Service
public class RAppAnalyzeService {
    @Resource
    private RAppAnalyzeMapper rAppAnalyzeMapper;

    public Map<String, Object> listApp(RegionParam param) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> schoolList = rAppAnalyzeMapper.listSchool(param.getLevel());
        if(N_Utils.isListEmpty(schoolList)) {
            map.put("dlnum",0);//登录次数
            map.put("pjdlnum",0);//平均登录次数
            map.put("dknum",0);//打开次数
            map.put("pjdknum",0);//平均打开次数
            map.put("costtime",0);//使用时长
            map.put("pjcosttime",0);//平均使用时长
            map.put("rates",new ArrayList<>());
            map.put("schools",new ArrayList<>());
            return map;
        }
        String schoolIds = schoolList.stream().map(m -> m.get("schoolid").toString()).collect(Collectors.joining(","));
        Integer begintime = param.getBegintime();
        Integer endtime = param.getEndtime();
        Integer daynum = N_Utils.getTimestampDay(begintime,endtime);//使用天数
        Integer dlnum = rAppAnalyzeMapper.getLoginNum(schoolIds,begintime,endtime,param.getYear());
        map.put("dlnum",dlnum);//登录次数
        map.put("pjdlnum", N_Utils.formatAvg(dlnum,daynum));//平均登录次数

        //应用时长、次数统计
        List<RAppVo> list = rAppAnalyzeMapper.listApp(schoolIds,begintime,endtime,param.getYear());
        Integer dknum = list.stream().mapToInt(RAppVo::getDknum).sum();
        map.put("dknum",dknum);//打开次数
        map.put("pjdknum", N_Utils.formatAvg(dknum,daynum));//平均打开次数
        Integer costtime = list.stream().mapToInt(RAppVo::getCosttime).sum();
        map.put("costtime",costtime);//使用时长
        map.put("pjcosttime", N_Utils.formatAvg(costtime,daynum));//平均使用时长

        //应用统计--应用时长概况（关于率的计算：各应用时长÷总使用时长，排名前5的应用）
        List<RAppVo> list2 = new ArrayList<>();
        Map<String, List<RAppVo>> rcollect = list.stream().collect(Collectors.groupingBy(RAppVo::getModulecode));
        for(Map.Entry<String, List<RAppVo>> rmap:rcollect.entrySet()) {
            List<RAppVo> rates2 = rmap.getValue();
            RAppVo a = new RAppVo();
            RAppVo rate = rates2.get(0);
            a.setModulecode(rate.getModulecode());
            a.setModulename(rate.getModulename());
            Integer costtime2 = rates2.stream().mapToInt(RAppVo::getCosttime).sum();
            a.setCosttime(costtime2);
            a.setTimerate(N_Utils.getIntegerDivideAndMulitiply(costtime2,costtime));
            list2.add(a);
        }
        List<RAppVo> rates = list2.stream().sorted(Comparator.comparing(RAppVo::getCosttime).reversed())
                .limit(5).collect(Collectors.toList());

        map.put("rates",rates);

        //学校应用时长、次数统计
        Map<Integer, List<RAppVo>> scollect = list.stream().collect(Collectors.groupingBy(RAppVo::getSchoolid));
        List<Map<String, Object>> schools = new ArrayList<>();
        for(Map.Entry<Integer, List<RAppVo>> smap:scollect.entrySet()) {
            List<RAppVo> schools2 = smap.getValue();
            RAppVo school = schools2.get(0);
            Map<String, Object> s = schoolList.stream().filter(m -> m.get("schoolid").equals(school.getSchoolid())).findFirst().orElse(new HashMap<>());
            List<Map<String, Object>> modules = new ArrayList<>();//学校模块列表
            for (RAppVo a : schools2) {
                Map<String, Object> module = new HashMap<>();
                module.put("modulecode",a.getModulecode());
                module.put("modulename",a.getModulename());
                module.put("dknum",a.getDknum());
                module.put("costtime",a.getDknum());
                modules.add(module);
            }
            s.put("modules",modules);
            schools.add(s);
        }
        map.put("schools",schools);
        return map;
    }
}
