package com.xtd.project.hkmanager.controller;

import com.xtd.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

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

/**
 * @className: AlarmScheduled
 * @description: 定时拉取报警信息
 * @author: lym
 * @date: 2020/4/27 16:01
 */
@Configuration
@EnableScheduling
public class AlarmScheduled {

    @Autowired
    private AlarmController alarmController;

    /**
     * 定时添加报警信息
     */
    @Scheduled(cron = ("0 0/5 * * * ?"))
    public void quertSaveAlarmInfo(){
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:00+08:00");
//        String currDate = format.format(new Date()); //当前日期时间
//
//        //计算得到开始时间
//        String startDate = getStartDate(currDate);
//
//        System.out.println("startTime:"+startDate +"   endTime:"+currDate);
//        alarmController.synAlarm(0,1000,0,startDate,currDate);
    }

    /**
     * 计算得到开始时间
     * @param currDate
     */
    private String getStartDate(String currDate){
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:00");
        String time = timeFormat.format(new Date()); //当前时间
        String[] times = time.split(":");

        String hh = times[0],mm = times[1],currDay = null,startDay = null;
        int starthhNum = 0,startmmNum = 0; //开始分
        if ("00".equals(mm)){ //整点
            if ("00".equals(hh)) {//凌晨
                starthhNum = 23;
                startDay = DateUtils.getNextDay(new Date()); //前一天
                currDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            }else {
                starthhNum = Integer.parseInt(hh) - 1; //1小时前
            }
            startmmNum = 55;
        }else {
            starthhNum = Integer.parseInt(hh);
            int mmNum = Integer.parseInt(mm);
            startmmNum = mmNum - 5; //五分钟前
        }

        String starthh = "00",startmm = "00"; //开始时、分
        if (starthhNum < 10){
            starthh = "0" + starthhNum;
        }else {
            starthh = String.valueOf(starthhNum);
        }
        times[0] = starthh;

        if (startmmNum < 10){
            startmm = "0" + startmmNum;
        }else {
            startmm = String.valueOf(startmmNum);
        }
        times[1] = startmm;

        String startTime = String.join(":",times); //开始时间
        String startDate = currDate;
        if (startDay != null){ //凌晨
            startDate = startDate.replace(currDay,startDay);
        }
        startDate = startDate.replace(time,startTime);
        return startDate;
    }

}
