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.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.scenic.resp.ScenicRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.mobile.AppStatisticsService;
import com.ycwl.basic.utils.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Calendar;
import java.util.List;


@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class ScenicStatsTaskIT {
    @Autowired
    private ScenicStatsTask task;
    @Autowired
    private AppStatisticsService statisticsService;
    @Autowired
    private StatisticsMapper statisticsMapper;
    @Autowired
    private ScenicRepository scenicRepository;

    @Test
    public void testA() {
        task.countDeviceStats();
    }

    @Test
    public void testB() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2025, Calendar.MAY, 1);
        ScenicReqQuery query = new ScenicReqQuery();
        query.setPageSize(1000);
        List<ScenicV2DTO> scenicList = scenicRepository.list(query);
        while (calendar.getTime().getTime() < System.currentTimeMillis()) {
            System.out.println(calendar.getTime());
            scenicList.forEach((scenic) -> {
                Long scenicId = Long.valueOf(scenic.getId());
                CommonQueryReq commonQueryReq = new CommonQueryReq();
                commonQueryReq.setScenicId(scenicId);
                commonQueryReq.setStartTime(DateUtil.beginOfDay(calendar.getTime()));
                commonQueryReq.setEndTime(DateUtil.endOfDay(calendar.getTime()));
                ApiResponse<AppStatisticsFunnelVO> resp = statisticsService.userConversionFunnel(commonQueryReq);
                AppStatisticsFunnelVO data = resp.getData();
                statisticsMapper.insertStat(scenicId, DateUtil.beginOfDay(calendar.getTime()), data);
            });
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}