package com.common.schedule;




import com.common.service.EsToDbSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

//众诚运维日志数据计划任务
@Slf4j
//@Component
public class ScheduledTask {

    @Autowired
    private EsToDbSyncService esToDbSyncService;

    private static final DateTimeFormatter INDEX_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");

    /**
     * 每天1点同步昨天的数据
     */

    public void syncYesterdayData() {
        log.info("开始执行昨天数据同步任务");
        LocalDate yesterday = LocalDate.now().minusDays(1);
        String indexName = "applog-" + yesterday.format(INDEX_FORMATTER);
        esToDbSyncService.syncEsLogsByIndexToDatabase(indexName);
        log.info("昨天数据同步任务完成");
    }

    /**
     * 每天1,5,9,13,17,21点同步今天的数据
     */
   // @Scheduled(cron = "0 0 1,5,9,13,17,21 * * ?")
    public void syncTodayData() {
        log.info("开始执行今天数据同步任务");
        LocalDate today = LocalDate.now();
        String indexName = "applog-" + today.format(INDEX_FORMATTER);
        esToDbSyncService.syncEsLogsByIndexToDatabase(indexName);
        log.info("今天数据同步任务完成");
    }

    /**
     * 手动同步指定日期的数据（可用于测试）
     */
    public void syncSpecificDateData(LocalDate date) {
        log.info("开始执行指定日期数据同步任务: {}", date);
        String indexName = "applog-" + date.format(INDEX_FORMATTER);
        esToDbSyncService.syncEsLogsByIndexToDatabase(indexName);
        log.info("指定日期数据同步任务完成: {}", date);
    }
}