package com.jiayue.insu.inclientqn.job;

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.StatusEnum;
import com.jiayue.insu.inclientqn.pushData.PushRealValueData;
import com.jiayue.insu.inclientqn.service.RecordService;
import com.jiayue.insu.inclientqn.service.StationService;
import com.jiayue.insu.inclientqn.service.client.BackDataAnalysisService;
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;

/**
 *  实际功率回传嘉越一体化云定时任务
 */
public class ActualPowerFeedbackForJYJob implements Job {

    @Autowired
    private StationService stationService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private PushRealValueData pushRealValueData;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //查询是否有今天成功记录 （为提高稳定性，该定时任务采取在规定时间段内 重复循环执行的定时策略，故：需要查询是否存在成功记录）
        Record successRecord;
        LocalDateTime localDateTime = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        // 查询全部场站信息
        List<Station> stationList = stationService.findAll();
        // 如果场站大于1个，则循环请求一体化修正数据
        if (stationList != null && stationList.size() > 1) {
            for (Station station : stationList) {
                // 如果有执行记录，则不再执行
                successRecord = recordService.findTimeAndTypeAndStateAndStationCode(localDateTime, CommonStant.RECORD_TYPE_REAL_VALUE_FOR_JY, StatusEnum.SUCCESS.getCode(), station.getStationCode());
                if (successRecord != null) {
                    return;
                }
                // 实际功率回传嘉越云
                pushRealValueData.pushRealValueForJy(station,localDateTime);
            }
            // 如果场站大于1个，则直接请求一体化修正数据
        } else if (stationList != null && stationList.size() == 1) {
            successRecord = recordService.findTimeAndTypeAndState(localDateTime, CommonStant.RECORD_TYPE_REAL_VALUE_FOR_JY, StatusEnum.SUCCESS.getCode());
            // 如果有执行记录，则不再执行
            if (successRecord != null) {
                return;
            }
            // 实际功率回传嘉越云
            pushRealValueData.pushRealValueForJy(stationList.get(0),localDateTime);
        }

    }
}
