package com.xhkjedu.sapi.service.region;

import com.xhkjedu.sapi.mapper.region.RAppAnalyzeMapper;
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 listApp(Integer level, Integer begintime, Integer endtime) {
        Map map = new HashMap();
        Integer daynum = N_Utils.getTimestampDay(begintime,endtime);//使用天数
        Integer dlnum = rAppAnalyzeMapper.getLoginNum(level,begintime,endtime);
        map.put("dlnum",dlnum);//登录次数
        map.put("pjdlnum", N_Utils.formatAvg(dlnum,daynum));//平均登录次数

        //应用时长、次数统计
        List<RAppVo> list = rAppAnalyzeMapper.listApp(level,begintime,endtime);
        Integer dknum = list.stream().collect(Collectors.summingInt(RAppVo::getDknum));
        map.put("dknum",dknum);//打开次数
        map.put("pjdknum", N_Utils.formatAvg(dknum,daynum));//平均打开次数
        Integer costtime = list.stream().collect(Collectors.summingInt(RAppVo::getCosttime));
        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 rmap:rcollect.entrySet()) {
            List<RAppVo> rates2 = (List<RAppVo>) rmap.getValue();
            RAppVo a = new RAppVo();
            RAppVo rate = rates2.get(0);
            a.setModulecode(rate.getModulecode());
            a.setModulename(rate.getModulename());
            Integer costtime2 = rates2.stream().collect(Collectors.summingInt(RAppVo::getCosttime));
            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> schools = new ArrayList<>();
        for(Map.Entry smap:scollect.entrySet()) {
            List<RAppVo> schools2 = (List<RAppVo>) smap.getValue();
            RAppVo school = schools2.get(0);
            Map s = new HashMap();
            s.put("schoolid",school.getSchoolid());
            s.put("schoolname",school.getSchoolname());
            s.put("shortname",school.getShortname());
            List<Map> modules = new ArrayList<>();//学校模块列表
            for (RAppVo a : schools2) {
                Map 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;
    }
}
