package com.njcn.csdevice.job;

import cn.hutool.core.date.DatePattern;
import com.njcn.csdevice.service.IRStatIntegrityDService;
import com.njcn.csdevice.service.IRStatOnlineRateDService;
import com.njcn.csharmonic.pojo.param.StatisticsDataParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * @author xy
 */
@Component
@EnableScheduling
@RequiredArgsConstructor
@Slf4j
public class DayDataJob {

    private final IRStatIntegrityDService statIntegrityDService;
    private final IRStatOnlineRateDService statOnlineRateDService;

    /**
     * 每天1点计算治理设备的完整性
     *
     * @date 2025/7/1
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void lineIntegrityJob() {
        StatisticsDataParam param = new StatisticsDataParam();
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDateTime start = yesterday.atStartOfDay();
        LocalDateTime end = yesterday.atTime(23, 59, 59);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN);
        param.setStartTime(start.format(formatter));
        param.setEndTime(end.format(formatter));
        statIntegrityDService.addData(param);
    }

    /**
     * 每天2点计算治理设备的在线率
     *
     * @date 2025/7/1
     */
    @Scheduled(cron = "0 30 1 * * ?")
    public void devOnlineRateJob() {
        StatisticsDataParam param = new StatisticsDataParam();
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDateTime start = yesterday.atStartOfDay();
        LocalDateTime end = yesterday.atTime(23, 59, 59);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN);
        param.setStartTime(start.format(formatter));
        param.setEndTime(end.format(formatter));
        statOnlineRateDService.addData(param);
    }
}
