package com.tofly.xrtygis.handle;


import com.tofly.xrtygis.custom.util.DateTimeUtil;
import com.tofly.xrtygis.service.DmanightflowService;
import com.tofly.xrtygis.service.IndicatorCorrectionService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
@Slf4j
@Service
@AllArgsConstructor
public class ScheduledTask {

    private final IndicatorCorrectionService indicatorCorrectionService;

    private final DmanightflowService dmanightflowService;

    /**
     *
     * 	每月一日零点同步dma分区缓存数据总
     */
    @Async
    @Scheduled(cron = "0 0 0 1 1/1 ? ")
    @SneakyThrows
    public  void totalCalculate(){
        LocalDate firstDayOfCurrentMonth = DateTimeUtil.getFirstDayOfCurrentMonth();
        Date date = DateTimeUtil.localDateToDate(firstDayOfCurrentMonth);
        indicatorCorrectionService.totalCalculate(date);
    }

    /**
     *
     * 	每月一日零点同步dma分区缓存数据分组
     */
    @Async
    @Scheduled(cron = "0 0 0 1 1/1 ? ")
    @SneakyThrows
    public  void leakageRateCalculate(){
        LocalDate firstDayOfCurrentMonth = DateTimeUtil.getFirstDayOfCurrentMonth();
        Date date = DateTimeUtil.localDateToDate(firstDayOfCurrentMonth);
        indicatorCorrectionService.leakageRateCalculate(date);
    }

    /**
     *
     * 	每天六点同步夜间最小流量
     */
    @Async
    @Scheduled(cron = "0 0 6 1/1 * ? ")
    @SneakyThrows
    public  void nightMinimumFlowAnalysis(){
        LocalDateTime todayBeginTime = DateTimeUtil.getTodayBeginTime();
        Date date = DateTimeUtil.convertLDTToDate(todayBeginTime);
        dmanightflowService.nightMinimumFlowAnalysis(date);
    }
}
