package com.tofly.xrtymis.custom.schedule;

import cn.hutool.log.Log;
import com.tofly.api.ScadaClient;
import com.tofly.xrtymis.custom.service.GdflowconfigService;
import com.tofly.xrtymis.custom.service.GdglinfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.Component;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author fcl
 * @date 2021/6/16 15:54
 */
@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
@Slf4j
@Service
public class ScheduledTask {

    @Autowired
    private GdglinfoService gdglinfoService;
    @Autowired
    private GdflowconfigService gdflowconfigService;
    @Autowired
    private ScadaClient scadaClient;

//    // 0/5 * * * * ?表示每隔5秒执行一次这个方法
//    @Scheduled(cron = "0/5 * * * * ?")
//    public void task1() {
//        System.out.println("**************task1执行了..");
//    }

    //0 */5 * * * ?表示每五分钟执行一次该方法
    // TODO 接单时限不需要
    //@Scheduled(cron = "0 0/5 * * * ?")
    public void  task2(){
        gdglinfoService.timeoutAnalysis();
    }

    //0 0/59 * * * ?表示每59分钟执行一次该方法
    @Scheduled(cron = "0 0/59 * * * ?")
    public void  task3(){
        gdglinfoService.rzczTimeout();
        gdglinfoService.rzshTimeout();
        gdglinfoService.rzzjTimeout();
        gdglinfoService.jgczTimeout();
//        gdglinfoService.gdcsTimeout();
    }

    /**
     * 定时处理修改超时判断是否超时，修改超时标记
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void timeOutTask(){
        gdglinfoService.jdTimeout();
        gdglinfoService.rzcsTimeout();
        gdglinfoService.shcsTimeout();
        gdglinfoService.zjcsTimeout();
        gdglinfoService.czcsTimeout();
        gdglinfoService.allcsTimeout();
    }

    /**
     * 定时处理修改超时判断是否超时，修改超时标记
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void timeForeignCallback(){
        gdglinfoService.foreignOrderCallback();
    }

    /**
     *
     * 	同步前两小时的异常点位模拟数据
     */
    @Async
    @Scheduled(cron = "0 0/10 * * * ? ")
    @SneakyThrows
    public void syncAbnormalPointData(){
        log.info("========================================开始自动同步前两小时的异常点位模拟数据========================================");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, -1);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date end = calendar.getTime();
        calendar.add(Calendar.HOUR, -1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date start = calendar.getTime();
        String startDate = sdf.format(start);
        String endDate = sdf.format(end);
        scadaClient.syncAbnormalPointData(startDate, endDate);
        log.info("========================================自动同步前两小时的异常点位模拟数据结束========================================");
    }

}
