package com.tofly.xrtygis.handle;

import cn.hutool.core.date.DateUtil;
import com.tofly.xrtygis.custom.service.SendNoteService;
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.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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

    private final DmanightflowService dmanightflowService;
    private final IndicatorCorrectionService indicatorCorrectionService;
    private final SendNoteService sendNoteService;

    /**
     * 每天8点同步前三日夜间最小流量
     *
     */
    @Async
    @Scheduled(cron = "0 0 8 * * ? ")
    @SneakyThrows
    public void nightMinimumFlowAnalysis() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        List<String> dateList = new ArrayList<>();
        for (int i=0;i<3;i++) {
            calendar.add(Calendar.DATE, -1);
            Date startTime = calendar.getTime();
            dateList.add(DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"));
        }
        dateList.forEach(info -> dmanightflowService.nightMinimumFlowAnalysis(DateUtil.parse(info, "yyyy-MM-dd HH:mm:ss")));
    }

    /**
     * 每天8点计算前三天DMA分区各项指标数据统计(每日)
     *
     */
    @Async
    @Scheduled(cron = "0 0 8 * * ? ")
    @SneakyThrows
    public void leakageRateCalculate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        List<String> dateList = new ArrayList<>();
        for (int i=0;i<3;i++) {
            calendar.add(Calendar.DATE, -1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date startTime = calendar.getTime();
            dateList.add(DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"));
        }
        dateList.forEach(info -> indicatorCorrectionService.leakageRateCalculate(DateUtil.parse(info, "yyyy-MM-dd HH:mm:ss")));
    }

//    /**
//     * 每天8点计算前三天总各项指标数据统计(每日)
//     *
//     */
//    @Async
//    @Scheduled(cron = "0 0 8 * * ? ")
//    @SneakyThrows
//    public void totalStatistical() {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
//        List<String> dateList = new ArrayList<>();
//        for (int i=0;i<3;i++) {
//            calendar.add(Calendar.DATE, -1);
//            calendar.set(Calendar.HOUR_OF_DAY, 0);
//            calendar.set(Calendar.MINUTE, 0);
//            calendar.set(Calendar.SECOND, 0);
//            Date startTime = calendar.getTime();
//            dateList.add(DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"));
//        }
//        dateList.forEach(info -> indicatorCorrectionService.totalCalculate(DateUtil.parse(info, "yyyy-MM-dd HH:mm:ss")));
//    }

    /**
     * 每小时10分自动发送短信
     *
     */
    @Async
    @Scheduled(cron = "0 10 0/1 * * ? ")
    @SneakyThrows
    public void actionAuto() {
        sendNoteService.actionAuto();
    }
}
