package com.ycwl.basic.task;

import cn.hutool.core.date.DateUtil;
import com.ycwl.basic.mapper.ScenicDeviceStatsMapper;
import com.ycwl.basic.model.pc.scenicDeviceStats.entity.ScenicDeviceStatsEntity;
import org.springframework.beans.factory.annotation.Autowired;
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
public class ScenicDeviceStatsTask {
    @Autowired
    private ScenicDeviceStatsMapper mapper;
    @Scheduled(cron = "0 10 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);
            });
        }
    }
}
