package com.jiayue.insu.incloud.job.data.push;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.jiayue.insu.incloud.constants.CommonStant;
import com.jiayue.insu.incloud.constants.enums.StatusEnum;
import com.jiayue.insu.incloud.entity.Record;
import com.jiayue.insu.incloud.entity.Station;
import com.jiayue.insu.incloud.pushdata.PushDataForLX;
import com.jiayue.insu.incloud.pushdata.PushDataForSH;
import com.jiayue.insu.incloud.service.RecordService;
import com.jiayue.insu.incloud.service.StationService;
import io.undertow.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 飔合科技（辽宁）上送原始短期数据定时任务
 *
 * @author yh
 * @version 1.0
 * @since 2022/5/22 11:29
 */
public class LXPushInitDqDataJob implements Job {

    @Autowired
    private PushDataForLX pushDataForLX;
    @Autowired
    private StationService stationService;
    @Autowired
    private RecordService recordService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String code = jobExecutionContext.getTrigger().getJobKey().getName();
        String[] split = code.split("-");
        if (split != null && split.length > 0) {
            String stationCode = split[0];
            if (split.length > 2) {
                stationCode = split[0] + "-" + split[1];
            }

            Station station = stationService.findByStationCode(stationCode);
            if (station != null) {
                // 安徽永武风光单独判断
                if ("J00797".equals(station.getStationCode()) || "J00637".equals(station.getStationCode())) {

                    LocalTime noon = LocalTime.NOON; // 12:00:00
                    LocalDateTime noonToday = LocalDateTime.of(LocalDate.now(), noon);
                    // 判断当前时间是否在中午12点之前
                    if (LocalDateTime.now().isBefore(noonToday)) {
                        //查询是否有今天成功记录 （为提高稳定性，该定时任务采取在规定时间段内 重复循环执行的定时策略，故：需要查询是否存在成功记录）
                        Record successRecord = recordService.findTimeAndTypeAndStateAndStation(
                                LocalDateTimeUtil.beginOfDay(LocalDateTime.now()),
                                CommonStant.RECORD_TYPE_PUSH_INIT,
                                StatusEnum.SUCCESS.getSign(), stationCode);
                        //没有成功记录进行业务
                        if (successRecord == null) {
                            pushDataForLX.pushDQData(station);
                        }
                    } else {
                        List<Record> recordList = recordService.findTypeAndStateAndStationAndCreatTime(
                                    CommonStant.RECORD_TYPE_PUSH_INIT,
                                StatusEnum.SUCCESS.getSign(), stationCode,LocalDateTimeUtil.beginOfDay(LocalDateTime.now()));

                        if (recordList.size() <= 1) {
                            pushDataForLX.pushDQData(station);
                        }
                    }
                } else {
                    //查询是否有今天成功记录 （为提高稳定性，该定时任务采取在规定时间段内 重复循环执行的定时策略，故：需要查询是否存在成功记录）
                    Record successRecord = recordService.findTimeAndTypeAndStateAndStation(
                            LocalDateTimeUtil.beginOfDay(LocalDateTime.now()),
                            CommonStant.RECORD_TYPE_PUSH_INIT,
                            StatusEnum.SUCCESS.getSign(), stationCode);
                    //没有成功记录进行业务
                    if (successRecord == null) {
                        pushDataForLX.pushDQData(station);
                    }
                }
            }
        }

    }

}
