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

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.service.RecordService;
import com.jiayue.insu.incloud.service.StationService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * 林洋推送原始气象定时任务
 *
 * @author yh
 * @version 1.0
 * @since 2022/5/22 11:29
 */
public class LXPushInitNwpDataJob 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 one = stationService.findByStationCode(stationCode);
            if (one != null) {
                //查询是否有今天成功记录 （为提高稳定性，该定时任务采取在规定时间段内 重复循环执行的定时策略，故：需要查询是否存在成功记录）
                Record successRecord = recordService.findTimeAndTypeAndStateAndStation(
                        LocalDateTimeUtil.beginOfDay(LocalDateTime.now()),
                        CommonStant.RECORD_TYPE_PUSH_INIT_NWP,
                        StatusEnum.SUCCESS.getSign(), stationCode);
                //没有成功记录进行业务
                if(successRecord == null) {
                    pushDataForLX.pushNWPData(one);
                }
            }
        }

    }
}
