package com.jiayue.insu.inclientqn.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.jiayue.insu.inclientqn.constant.CommonStant;
import com.jiayue.insu.inclientqn.entity.Record;
import com.jiayue.insu.inclientqn.entity.Station;
import com.jiayue.insu.inclientqn.inenum.QNHLEnum;
import com.jiayue.insu.inclientqn.inenum.StatusEnum;
import com.jiayue.insu.inclientqn.pushData.PushForecastData;
import com.jiayue.insu.inclientqn.service.RecordService;
import com.jiayue.insu.inclientqn.service.StationService;
import com.jiayue.insu.inclientqn.service.client.WXCorrforeService;
import com.jiayue.insu.inclientqn.util.DateTimeUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description: 武穴向一体化云端传输超短期原始数据
 * @author: yuanhao
 * @createDate: 2022/6/17
 * @version: 1.0
 */
public class WXPushDataJob implements Job {

    @Autowired
    private PushForecastData pushForecastData;
    @Autowired
    private StationService stationService;
    @Autowired
    private RecordService recordService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        long time = DateTimeUtils.get15min(DateUtil.date().getTime());
        // 获取全部场站
        List<Station> stationList = stationService.findAll();
        Record record;
        LocalDateTime signTime = LocalDateTimeUtil.of(time);

        // 如果场站大于2个，则为多场站情况
        if (stationList != null && stationList.size() > 1) {
            for (Station station : stationList) {
                record = recordService.findTimeAndTypeAndContentAndStateAndStationCode(signTime, CommonStant.RECORD_TYPE_PUSH_INIT, QNHLEnum.DATA_ORIGINALCDQ_TYPE.getCode(), StatusEnum.SUCCESS.getSign(),station.getStationCode());
                if (record != null) {
                    return;
                }
                pushForecastData.pushUltraShortTermDataWX(station, signTime);
            }
            // 如果只有一个场站
        } else if (stationList != null && stationList.size() == 1) {
            record = recordService.findTimeAndTypeAndContentAndState(signTime, CommonStant.RECORD_TYPE_PUSH_INIT, QNHLEnum.DATA_ORIGINALCDQ_TYPE.getCode(), StatusEnum.SUCCESS.getSign());
            if (record != null) {
                return;
            }
            pushForecastData.pushUltraShortTermDataWX(stationList.get(0), signTime);
        }

    }
}
