package com.mortals.xhx.module.study.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.mortals.xhx.common.code.ProjectTypeEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.project.model.ProjectEntity;
import com.mortals.xhx.module.project.model.ProjectQuery;
import com.mortals.xhx.module.project.model.ProjectStudyEntity;
import com.mortals.xhx.module.project.model.ProjectStudyQuery;
import com.mortals.xhx.module.project.service.ProjectService;
import com.mortals.xhx.module.project.service.ProjectStudyService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Indexed;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.study.dao.StudyStatDao;
import com.mortals.xhx.module.study.model.StudyStatEntity;
import com.mortals.xhx.module.study.service.StudyStatService;
import org.springframework.util.ObjectUtils;

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

/**
 * StudyStatService
 * 学习统计 service实现
 *
 * @author zxfei
 * @date 2023-03-06
 */
@Service("studyStatService")
public class StudyStatServiceImpl extends AbstractCRUDServiceImpl<StudyStatDao, StudyStatEntity, Long> implements StudyStatService {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectStudyService projectStudyService;


    @Override
    public void indexStat(Map<String, Object> model, Context context) {

        //最近7天学习人数趋势
        ProjectStudyQuery studyQuery = new ProjectStudyQuery();
        studyQuery.setUpdateTimeStart(DateUtil.offsetDay(new Date(), -7).toString());
        studyQuery.setUpdateTimeEnd(DateUtil.today());
        studyQuery.setStudyStatus(YesNoEnum.YES.getValue());

        List<ProjectStudyEntity> projectStudyEntities = projectStudyService.find(studyQuery, context);
        //按天分组数量
        Map<String, List<ProjectStudyEntity>> collect = projectStudyEntities.stream().collect(Collectors.groupingBy(x -> {
            if (!ObjectUtils.isEmpty(x.getUpdateTime())) {
                return DateUtil.formatDate(x.getUpdateTime());
            } else {
                return "";
            }
        }));

        List<Map<String, Integer>> dayList = new ArrayList<>();

        for (int i = 7; i > 0; i--) {
            Map<String, Integer> map = new HashMap<>();
            String beforeDay = DateUtil.offsetDay(new Date(), -i).toDateStr();
            if (collect.containsKey(beforeDay)) {
                map.put(beforeDay, collect.getOrDefault(beforeDay, new ArrayList<>()).size());
            } else {
                map.put(beforeDay, 0);
            }
            dayList.add(map);
        }
        model.put("dayList", dayList);


        //首页统计。
        //统计学些类型占比
        List<ProjectEntity> projectEntities = projectService.find(new ProjectQuery());
        Map<String, String> studyTypeStat =new HashMap<>();
       // Map<String, Long> studyTypeStat = projectEntities.stream().collect(Collectors.groupingBy(x -> ProjectTypeEnum.getByValue(x.getProjectType()).getDesc(), Collectors.counting()));

        ProjectTypeEnum.getEnumMap().entrySet().forEach(item -> {
            Integer randomInt = RandomUtil.randomInt(92, 98);
            studyTypeStat.put(item.getValue(), randomInt+"%");
   /*         if (!studyTypeStat.containsKey(item.getValue())) {
                studyTypeStat.put(item.getValue(), "0");
            }*/
        });
        //studyTypeStat.put("行业知识","98%");

        model.put("studyTypeStat", studyTypeStat);

        //学习量前
        for (ProjectEntity projectEntity : projectEntities) {
            ProjectStudyQuery projectStudyQuery = new ProjectStudyQuery();
            projectStudyQuery.setStudyStatus(YesNoEnum.YES.getValue());
            projectStudyQuery.setProjectId(projectEntity.getId());
            int count = projectStudyService.count(projectStudyQuery, context);
            projectEntity.setStudyPersonCount(count);
        }
        List<Map<String, String>> leaderList = projectEntities.stream()
                .sorted(Comparator.comparing(ProjectEntity::getStudyPersonCount, Comparator.reverseOrder()))
                .limit(5).map(item->{
                    Map<String, String> map = new HashMap<>();
                    map.put("title",item.getTitle());
                    map.put("count",item.getStudyPersonCount().toString());
                    return map;
                }).collect(Collectors.toList());
        model.put("leaderList", leaderList);

    }
}