package com.czg.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.czg.order.entity.ShopOrderStatistic;
import com.czg.order.entity.ShopProdStatistic;
import com.czg.order.entity.ShopTableOrderStatistic;
import com.czg.order.service.ShopOrderStatisticService;
import com.czg.order.service.ShopProdStatisticService;
import com.czg.order.service.ShopTableOrderStatisticService;
import com.czg.service.order.mapper.ShopOrderStatisticMapper;
import com.czg.service.order.mapper.ShopProdStatisticMapper;
import com.czg.service.order.mapper.ShopTableOrderStatisticMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.DbChain;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * @author Administrator
 */
@Component
@Slf4j
public class StatisticTask {
    @Resource
    private ShopTableOrderStatisticService shopTableOrderStatisticService;
    @Resource
    private ShopProdStatisticService shopProdStatisticService;
    @Resource
    private ShopOrderStatisticService shopOrderStatisticService;
    @Resource
    private ShopOrderStatisticMapper shopOrderStatisticMapper;
    @Resource
    private ShopProdStatisticMapper shopProdStatisticMapper;
    @Resource
    private ShopTableOrderStatisticMapper shopTableOrderStatisticMapper;

    /**
     * 基础统计
     *
     * @param dateTime 日期时间
     */
    private void baseStatistic(DateTime dateTime) {
        try {
            shopOrderStatisticService.statistic(dateTime);
        } catch (Exception e) {
            log.error("统计订单数据失败", e);
        }
        try {
            shopProdStatisticService.statistic(dateTime);
        } catch (Exception e) {
            log.error("统计商品数据失败", e);
        }
        try {
            shopTableOrderStatisticService.statistic(dateTime);
        } catch (Exception e) {
            log.error("统计桌台数据失败", e);
        }
    }

    //    @Scheduled(cron = "1/6 * * * * ? ")
    @Scheduled(cron = "0 0 8 * * ?")
    public void run() {
        long start = System.currentTimeMillis();
        log.info("定时任务执行，开始统计数据");
        // 获取前一天
        DateTime yesterday = DateUtil.yesterday();
        baseStatistic(yesterday);
        log.info("定时任务执行完毕，耗时:{}ms", start - System.currentTimeMillis());
    }

    @Scheduled(cron = "0 0,15,30,45 * * * ? ")
    public void run2() {
        long start = System.currentTimeMillis();
        log.info("定时任务2执行，开始统计数据");
        // 获取当天
        DateTime today = DateUtil.date();
        baseStatistic(today);
        log.info("定时任务2执行完毕，耗时:{}ms", start - System.currentTimeMillis());
    }

    /**
     * 统计历史数据
     */
    public void statisticHistoryData() {
        // 指定开始日期
        LocalDate startDate = LocalDate.of(2024, 3, 1);
        // 指定结束日期
        LocalDate endDate = LocalDate.now();
        List<Long> shopIdList = DbChain.table("tb_shop_info").select("id").objListAs(Long.class);
        List<List<Long>> split = CollUtil.split(shopIdList, 10);
//         1.清除历史统计的数据
        for (List<Long> splitIdList : split) {
            splitIdList.parallelStream().forEach(shopId -> {
                shopOrderStatisticMapper.deleteByQuery(QueryWrapper.create().eq(ShopOrderStatistic::getShopId, shopId));
                shopProdStatisticMapper.deleteByQuery(QueryWrapper.create().eq(ShopProdStatistic::getShopId, shopId));
                shopTableOrderStatisticMapper.deleteByQuery(QueryWrapper.create().eq(ShopTableOrderStatistic::getShopId, shopId));
            });
        }
        // 2.开始从2024-3-1开始统计数据
        startDate.datesUntil(endDate.plusDays(1)).forEach(date -> {
            System.out.println(date.toString());
            DateTime dateTime = DateUtil.parseDate(date.toString());
            System.out.println(dateTime);
            baseStatistic(dateTime);
        });
    }
}
