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

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.pulldata.PullCorrectDataForSH;
import com.jiayue.insu.incloud.service.RecordService;
import com.jiayue.insu.incloud.service.StationService;
import lombok.extern.slf4j.Slf4j;
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
 */
@Slf4j
public class SHKJPullCorrectDataJob implements Job {

    @Autowired
    private PullCorrectDataForSH pullCorrectDataForSH;
    @Autowired
    private StationService stationService;
    @Autowired
    private RecordService recordService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String code = jobExecutionContext.getTrigger().getJobKey().getName();
        // 校验并获取场站信息
        Station station = stationService.checkAndFindStation(code,CommonStant.RECORD_TYPE_PULL_CORRECT);
        //没有成功记录进行业务
        if (station != null) {
            Record successRecord = recordService.findTimeAndTypeAndStateAndStation(
                    LocalDateTimeUtil.beginOfDay(LocalDateTime.now()),
                    CommonStant.RECORD_TYPE_PULL_CORRECT,
                    StatusEnum.SUCCESS.getSign(), station.getStationCode());
            if (successRecord == null){
                pullCorrectDataForSH.pullDQData(station);
            }

        }else{
            log.error("========== 一体化定时任务异常:  场站信息为空  {}==========",code);
        }
    }
}
