package com.aidex.system.service.impl;

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

import cn.hutool.core.date.*;
import com.aidex.common.constant.Constants;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.system.mapper.SysMiniappDailyVisitMapper;
import com.aidex.system.domain.SysMiniappDailyVisit;
import com.aidex.system.service.SysMiniappDailyVisitService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 小程序日数据统计Service业务层处理
 * @author ChenSir
 * @email 914769835
 * @date 2024-03-18
 */
@Service
@Transactional(readOnly = true)
public class SysMiniappDailyVisitServiceImpl extends BaseServiceImpl<SysMiniappDailyVisitMapper, SysMiniappDailyVisit> implements SysMiniappDailyVisitService {

    private static final Logger log = LoggerFactory.getLogger(SysMiniappDailyVisitServiceImpl.class);

    /**
     * 获取单条数据
     * @param sysMiniappDailyVisit 小程序日数据统计
     * @return 小程序日数据统计
     */
    @Override
    public SysMiniappDailyVisit get(SysMiniappDailyVisit sysMiniappDailyVisit) {
        SysMiniappDailyVisit dto = super.get(sysMiniappDailyVisit);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 小程序日数据统计id
     * @return 小程序日数据统计
     */
    @Override
    public SysMiniappDailyVisit get(String id) {
        SysMiniappDailyVisit dto = super.get(id);
        return dto;
    }

    /**
     * 查询小程序日数据统计列表
     * @param sysMiniappDailyVisit 小程序日数据统计
     * @return 小程序日数据统计
     */
    @Override
    public List<SysMiniappDailyVisit> findList(SysMiniappDailyVisit sysMiniappDailyVisit) {
		List<SysMiniappDailyVisit> sysMiniappDailyVisitList = super.findList(sysMiniappDailyVisit);
        return sysMiniappDailyVisitList;
    }

    /**
     * 分页查询小程序日数据统计列表
     * @param sysMiniappDailyVisit 小程序日数据统计
     * @return 小程序日数据统计
     */
    @Override
    public PageInfo<SysMiniappDailyVisit> findPage(SysMiniappDailyVisit sysMiniappDailyVisit) {
		PageInfo<SysMiniappDailyVisit> page = super.findPage(sysMiniappDailyVisit);
        return page;
    }

    /**
     * 保存小程序日数据统计
     * @param sysMiniappDailyVisit
     * @return 结果
     */
    @Override
    public boolean save(SysMiniappDailyVisit sysMiniappDailyVisit) {
        return super.save(sysMiniappDailyVisit);
    }

    /**
     * 删除小程序日数据统计信息
     * @param sysMiniappDailyVisit
     * @return 结果
     */
    @Override
    public boolean remove(SysMiniappDailyVisit sysMiniappDailyVisit) {
        return super.remove(sysMiniappDailyVisit);
    }

    /**
     * 批量删除小程序日数据统计
     * @param ids 需要删除的小程序日数据统计ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteSysMiniappDailyVisitByIds(String[] ids) {
        return mapper.deleteSysMiniappDailyVisitByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    /**
     * 批量删除小程序日数据统计
     * @param statisticsType 统计类型 week、month、year
     * @return 结果
     */
    @Override
    public Map<String, Object> getSysMiniappDailyVisit(String statisticsType, Date beginDate, Date endDate) {
        Map<String, Object> map = new HashMap<>(16);
        if (!Constants.STATISTICS_TYPE_YEAR.equals(statisticsType)){
            DateRange range = DateUtil.range(beginDate, endDate, DateField.DAY_OF_YEAR);
            // 日期视图
            SysMiniappDailyVisit sysMiniappDailyVisit = new SysMiniappDailyVisit();
            sysMiniappDailyVisit.setBeginRefDate(DateUtil.format(beginDate, "yyyy-MM-dd"));
            sysMiniappDailyVisit.setEndRefDate(DateUtil.format(endDate, "yyyy-MM-dd"));
            List<SysMiniappDailyVisit> sysMiniappDailyVisitList = super.findList(sysMiniappDailyVisit);
            List<DateTime> dateTimes = Lists.newArrayList(range.iterator());
            List<String> dataList = dateTimes.stream().map(date -> (DateUtil.format(date, "yyyy-MM-dd"))).collect(Collectors.toList());
            List<Long> visitUv = new ArrayList<>();
            List<Long> visitPv = new ArrayList<>();
            List<Long> visitUvNew = new ArrayList<>();
            for (String item : dataList){
                Optional<SysMiniappDailyVisit> first = sysMiniappDailyVisitList.stream().filter(miniappDailyVisit -> DateUtil.format(miniappDailyVisit.getRefDate(), "yyyy-MM-dd") .equals(item)) // 32, 34
                        .findFirst();
                if (first.isPresent()){
                    visitUv.add(first.get().getVisitUv());
                    visitPv.add(first.get().getVisitPv());
                    visitUvNew.add(first.get().getVisitUvNew());
                }else {
                    visitUv.add(0L);
                    visitPv.add(0L);
                    visitUvNew.add(0L);
                }
            }
            map.put("dataList", dataList);
            map.put("visitUv", visitUv);
            map.put("visitPv", visitPv);
            map.put("visitUvNew", visitUvNew);
        } else {
            // 月份视图
            DateRange range = DateUtil.range(beginDate, endDate, DateField.MONTH);
            List<DateTime> dateTimes = Lists.newArrayList(range.iterator());
            List<String> dataList = dateTimes.stream().map(date -> (DateUtil.format(date, "yyyy-MM"))).collect(Collectors.toList());
            List<Long> visitUv = new ArrayList<>();
            List<Long> visitPv = new ArrayList<>();
            List<Long> visitUvNew = new ArrayList<>();
            for (DateTime item : dateTimes){
                SysMiniappDailyVisit sysMiniappDailyVisit = new SysMiniappDailyVisit();
                sysMiniappDailyVisit.setBeginRefDate(DateUtil.format(DateUtil.beginOfMonth(item), "yyyy-MM-dd"));
                sysMiniappDailyVisit.setEndRefDate(DateUtil.format(DateUtil.endOfMonth(item), "yyyy-MM-dd"));
                List<SysMiniappDailyVisit> sysMiniappDailyVisitList = super.findList(sysMiniappDailyVisit);
                if (sysMiniappDailyVisitList.size() > 0){
                    Long visitUvSum = sysMiniappDailyVisitList.stream().mapToLong(SysMiniappDailyVisit::getVisitUv).reduce(0, Long::sum);
                    visitUv.add(visitUvSum);
                    Long visitPvSum = sysMiniappDailyVisitList.stream().mapToLong(SysMiniappDailyVisit::getVisitPv).reduce(0, Long::sum);
                    visitPv.add(visitPvSum);
                    Long visitUvNewSum = sysMiniappDailyVisitList.stream().mapToLong(SysMiniappDailyVisit::getVisitUvNew).reduce(0, Long::sum);
                    visitUvNew.add(visitUvNewSum);
                }else {
                    visitUv.add(0L);
                    visitPv.add(0L);
                    visitUvNew.add(0L);
                }
            }
            map.put("dataList", dataList);
            map.put("visitUv", visitUv);
            map.put("visitPv", visitPv);
            map.put("visitUvNew", visitUvNew);
        }
		return map;
    }



}
