package com.microservices.dms.achievementLibrary.service;

import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.microservices.dms.achievementLibrary.domain.KeyValueVo;
import com.microservices.dms.achievementLibrary.mapper.AchievementsMapper;
import com.microservices.dms.resourceLibrary.domain.vo.KeyValVo;
import com.microservices.dms.resourceLibrary.mapper.CompetitionResourceLibraryMapper;
import com.microservices.dms.utils.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class BigScreenStatisticService {

    private final AchievementsMapper achievementsMapper;
    private final CompetitionResourceLibraryMapper competitionResourceLibraryMapper;

    public Map<String, Object> getAchievementTopStatistic(String source) {
        Date[] days = DateUtil.getDays(0, 6);
        Map<String, Long> m = achievementsMapper.getAchievement(days[0], days[1], source)
                .stream().collect(Collectors.toMap(KeyValVo::getK, KeyValVo::getV));
        List<String> dateStr = DateUtil.getDateStrMMDD(days[0], days[1]);
        Date[] lastDays = DateUtil.getDays(7, 13);
        long lastSum = Optional.ofNullable(achievementsMapper.countAchievementBySource(lastDays[0], lastDays[1], source)).orElse(0L);
        long sum = Optional.ofNullable(achievementsMapper.countAchievementBySource(null, null, source)).orElse(0L);
        long cSum = m.values().stream().mapToLong(Long::longValue).sum();
        double rate;
        if (lastSum == 0) rate = 100D;
        else {
            rate = (double) (cSum - lastSum) / lastSum;
        }

        List<KeyValVo<String, Long>> res = new ArrayList<>();
        for (String s : dateStr) {
            Long a = m.getOrDefault(s, 0L);
            KeyValVo<String, Long> k1 = new KeyValVo<>();
            k1.setK(s);
            k1.setV(a);
            res.add(k1);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("list", res);
        map.put("sum", sum);
        map.put("rate", rate);
        return map;
    }

    public List<Map<String, Object>> getAchievementDataYearly() {
        return achievementsMapper.getAchievementDataYearly();
    }

    public List<Map<String, Object>> getAchievementType() {
        return achievementsMapper.getAchievementType();
    }

    public List<Map<String,Object>> getAchievementActData() {
        Date[] days = DateUtil.getDays(0, 6);
        Map<String, Long> wlist = achievementsMapper.getAchievementActDataW(days[0], days[1]).stream().collect(Collectors.toMap(KeyValueVo::getName, KeyValueVo::getValue));
        Map<String, Long> clist = achievementsMapper.getAchievementActDataC(days[0], days[1]).stream().collect(Collectors.toMap(KeyValueVo::getName, KeyValueVo::getValue));
        Map<String, Long> dlist = achievementsMapper.getAchievementActDataD(days[0], days[1]).stream().collect(Collectors.toMap(KeyValueVo::getName, KeyValueVo::getValue));
        Map<String, Long> flist = achievementsMapper.getAchievementActDataF(days[0], days[1]).stream().collect(Collectors.toMap(KeyValueVo::getName, KeyValueVo::getValue));

        List<String> dateStr = DateUtil.getDateStrMMDD(days[0], days[1]);
        List<KeyValueVo> wres = new ArrayList<>();
        List<KeyValueVo> cres = new ArrayList<>();
        List<KeyValueVo> dres = new ArrayList<>();
        List<KeyValueVo> fres = new ArrayList<>();

        for (String s : dateStr) {
            Long a = wlist.getOrDefault(s, 0L);
            KeyValueVo k1 = new KeyValueVo();
            k1.setKey(s);
            k1.setValue(a);
            k1.setName("关注");
            wres.add(k1);
        }

        for (String s : dateStr) {
            Long a = clist.getOrDefault(s, 0L);
            KeyValueVo k1 = new KeyValueVo();
            k1.setKey(s);
            k1.setValue(a);
            k1.setName("浏览");
            cres.add(k1);
        }


        for (String s : dateStr) {
            Long a = dlist.getOrDefault(s, 0L);
            KeyValueVo k1 = new KeyValueVo();
            k1.setKey(s);
            k1.setValue(a);
            k1.setName("下载");
            dres.add(k1);
        }

        for (String s : dateStr) {
            Long a = flist.getOrDefault(s, 0L);
            KeyValueVo k1 = new KeyValueVo();
            k1.setKey(s);
            k1.setValue(a);
            k1.setName("收藏");
            fres.add(k1);
        }

        List<Map<String, Object>> r = new ArrayList<>();
        Map<String, Object> w = new HashMap<>();
        w.put("key", "关注");
        w.put("value", wres);
        Map<String, Object> c = new HashMap<>();
        c.put("key","浏览");
        c.put("value", cres);
        Map<String, Object> d = new HashMap<>();
        d.put("key","下载");
        d.put("value", dres);
        Map<String, Object> f = new HashMap<>();
        f.put("key", "收藏");
        f.put("value", fres);


        r.add(w);
        r.add(c);
        r.add(d);
        r.add(f);

        return r;
    }

    public List<Map<String, Object>> getAchievementDomain() {
        return achievementsMapper.getAchievementDomain();
    }

    public Object getAchievementAddYearly() {

        Date[] currentYear = DateUtil.getLast12Months();

        Map<String, Long> m = achievementsMapper.getAchievementAddYearlyData(currentYear[0],currentYear[1])
                .stream().collect(Collectors.toMap(KeyValVo::getK, KeyValVo::getV));
        List<String> dateStr = DateUtil.getDateStrYYYYMM(currentYear[0], currentYear[1]);
        List<KeyValVo<String, Long>> res = new ArrayList<>();
        for (String s : dateStr) {
            Long a = m.getOrDefault(s, 0L);
            KeyValVo<String, Long> k1 = new KeyValVo<>();
            k1.setK(s);
            k1.setV(a);
            res.add(k1);
        }
        return res;
    }

    public List<Map<String, Object>> getAchievementHotRank() {
        return achievementsMapper.getAchievementHotRank();
    }

    public Map<String, Object> getCompetitionTopStatistic() {
        Map<String, Object> res = new HashMap<>();
        Long finish = competitionResourceLibraryMapper.getCompetitionFinish();
        res.put("finish", finish);
        Long enroll = competitionResourceLibraryMapper.getCompetitionEnroll();
        res.put("enroll", enroll);
        Long underway = competitionResourceLibraryMapper.getCompetitionUnderway();
        res.put("underway", underway);
        Long submitCount = competitionResourceLibraryMapper.getCompetitionSubmitCount();
        res.put("submitCount", submitCount);
        Long transfer = competitionResourceLibraryMapper.getCompetitionTransferCount();
        res.put("transfer", transfer);
        Long needAudit = competitionResourceLibraryMapper.getCompetitionNeedAuditCount();
        res.put("needAudit", needAudit);
        return res;
    }

    public List<KeyValueVo> getCompetitionStatisticYearly() {
        return competitionResourceLibraryMapper.getCompetitionStatisticYearly();
    }

    public Object getCompetitionHot() {
        return competitionResourceLibraryMapper.getCompetitionHot();
    }

    public List<KeyValVo<String, Long>> getCompetitionYearlyPaperAdd() {
        Date[] currentYear = DateUtil.getLast12Months();

        Map<String, Long> m = competitionResourceLibraryMapper.getCompetitionYearlyPaperAdd(currentYear[0],currentYear[1])
                .stream().collect(Collectors.toMap(KeyValVo::getK, KeyValVo::getV));
        List<String> dateStr = DateUtil.getDateStrYYYYMM(currentYear[0], currentYear[1]);
        List<KeyValVo<String, Long>> res = new ArrayList<>();
        for (String s : dateStr) {
            Long a = m.getOrDefault(s, 0L);
            KeyValVo<String, Long> k1 = new KeyValVo<>();
            k1.setK(s);
            k1.setV(a);
            res.add(k1);
        }
        return res;
    }

    public List<KeyValVo<String, Long>> getCompetitionYearlyFinish() {
        Date[] currentYear = DateUtil.getLast12Months();

        Map<String, Long> m = competitionResourceLibraryMapper.getCompetitionYearlyFinish(currentYear[0],currentYear[1])
                .stream().collect(Collectors.toMap(KeyValVo::getK, KeyValVo::getV));
        List<String> dateStr = DateUtil.getDateStrYYYYMM(currentYear[0], currentYear[1]);
        List<KeyValVo<String, Long>> res = new ArrayList<>();
        for (String s : dateStr) {
            Long a = m.getOrDefault(s, 0L);
            KeyValVo<String, Long> k1 = new KeyValVo<>();
            k1.setK(s);
            k1.setV(a);
            res.add(k1);
        }
        return res;
    }
}
