package com.ycwl.basic.task;

import cn.hutool.core.date.DateUtil;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.mapper.ScenicDeviceStatsMapper;
import com.ycwl.basic.mapper.StatisticsMapper;
import com.ycwl.basic.model.mobile.statistic.req.CommonQueryReq;
import com.ycwl.basic.model.mobile.statistic.resp.AppStatisticsFunnelVO;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.model.pc.scenicDeviceStats.entity.ScenicDeviceStatsEntity;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.mobile.AppStatisticsService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
@Profile("prod")
public class ScenicStatsTask {
    @Autowired
    private ScenicDeviceStatsMapper mapper;
    @Autowired
    private StatisticsMapper statisticsMapper;
    @Autowired
    private AppStatisticsService statisticsService;
    @Autowired
    private ScenicRepository scenicRepository;

    @Scheduled(cron = "0 1 0 * * *")
    public void countDeviceStats() {
        Date yesterdayStart = DateUtil.beginOfDay(DateUtil.yesterday());
        Date yesterdayEnd = DateUtil.endOfDay(yesterdayStart);
        Date twoDaysBeforeStart = DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.yesterday(), -1));
        Date twoDaysBeforeEnd = DateUtil.endOfDay(twoDaysBeforeStart);
        // 前天的数据
        List<ScenicDeviceStatsEntity> list = mapper.countDeviceStats(twoDaysBeforeStart, twoDaysBeforeEnd);
        if (!list.isEmpty()) {
            list.forEach(stats -> {
                stats.setDate(twoDaysBeforeStart);
                mapper.insert(stats);
            });
        }
        list = mapper.countDeviceStats(yesterdayStart, yesterdayEnd);
        if (!list.isEmpty()) {
            list.forEach(stats -> {
                stats.setDate(yesterdayStart);
                mapper.insert(stats);
            });
        }
    }
    @Scheduled(cron = "0 20 0 * * *")
    public void countScenicStats() {
        Date yesterdayStart = DateUtil.beginOfDay(DateUtil.yesterday());
        Date yesterdayEnd = DateUtil.endOfDay(yesterdayStart);
        ScenicReqQuery query = new ScenicReqQuery();
        query.setPageSize(1000);
        List<ScenicV2DTO> scenicList = scenicRepository.list(query);
        scenicList.forEach((scenic) -> {
            CommonQueryReq commonQueryReq = new CommonQueryReq();
            Long scenicId = Long.valueOf(scenic.getId());
            commonQueryReq.setScenicId(scenicId);
            commonQueryReq.setStartTime(yesterdayStart);
            commonQueryReq.setEndTime(yesterdayEnd);
            commonQueryReq.setRealtime(true);
            ApiResponse<AppStatisticsFunnelVO> resp = statisticsService.userConversionFunnel(commonQueryReq);
            AppStatisticsFunnelVO data = resp.getData();
            statisticsMapper.insertStat(scenicId, yesterdayStart, data);
        });
    }
}
