package com.cpp.web.job;

import com.cpp.common.utils.spring.SpringUtils;
import com.cpp.system.service.ISysConfigService;
import com.cpp.web.domain.datafactory.enums.FileTypeEnum;
import com.cpp.web.domain.enums.AlarmEnum;
import com.cpp.web.domain.enums.DataSourcesEnum;
import com.cpp.web.domain.station.*;
import com.cpp.web.service.accuracy.CalcAccuracy;
import com.cpp.web.service.alarm.AlarmLog;
import com.cpp.web.service.datafactory.CloudFileParsing;
import com.cpp.web.service.datafactory.DataStore;
import com.cpp.web.service.datafactory.ParsingLogService;
import com.cpp.web.service.datafactory.SftpFileParsing;
import com.cpp.web.service.station.ElectricFieldService;
import com.cpp.web.utils.LogUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 定时任务管理类
 */
@RequiredArgsConstructor
@Component
@Slf4j
public class ScheduledJob implements ApplicationRunner {
    private final SftpFileParsing sftpFileParsing;

    private final CalcAccuracy calcAccuracy;

    private final CloudFileParsing cloudFileParsing;

    private final ISysConfigService iSysConfigService;


    private final ParsingLogService parsingLogService;

    AlarmLog alarmLog = AlarmLog.getInstance();

    /**
     * 解析站端文件定时任务
     */
//    @Scheduled(fixedRate = 60000L)
    public void parsingFile() {
        sftpFileParsing.parsingFile();
    }

//    @Scheduled(cron = "0 0/15 0 * * *")
    public void check() {
        //短期未解析告警
        String dqEndTime = iSysConfigService.selectConfigByKey("dqEndTime");
        if (!dqEndTime.equals(StringUtils.EMPTY)) {
            Date date = null;
            try {
                date = DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd " + dqEndTime), "yyyy-MM-dd HH:mm");
                Date now = new Date();
                if (date.getTime() < now.getTime()) {
                    List<ElectricField> electricFields = parsingLogService.notTodayLogStation(FileTypeEnum.dq, DataSourcesEnum.E1);
                    for (ElectricField electricField : electricFields) {
                        alarmLog.info(DataSourcesEnum.E1, AlarmEnum.E4, LogUtil.format("”{}“发电站,未在”{}“前上传“短期”预测文件！", electricField.getName(), DateFormatUtils.format(new Date(), "yyyy-MM-dd " + dqEndTime)), electricField.getStationCode());
                    }
                }
            } catch (ParseException e) {
                log.info("dqEndTime参数转换date失败");
                e.printStackTrace();
            }
        }
    }


    /**
     * 解析中心端文件定时任务
     */
//    @Scheduled(fixedRate = 60000L)
    public void cloudFileParsing() {
        cloudFileParsing.parsingFile();
    }

    /**
     * 压缩文件上传并清理历史文件，一天一次
     */
//    @Scheduled(cron = "0 30 0 * * *")
    public void uploadZipFile() {
        sftpFileParsing.uploadZipFile();
    }


    /**
     * 计算准确率定时任务
     */
//    @Scheduled(cron = "0 30 1 * * *")
    public void calculate() {
        calcAccuracy.calculate();
    }

    /**
     * 告警消纳定时任务
     */
//    @Scheduled(fixedRate = 60000L)
    public void e() {
        AlarmLog.getInstance().eliminate();
    }


    /**
     * 告警消纳定时任务
     */
//    @Scheduled(fixedRate = 60000L)
    public void aaa() {
        for (ElectricField electricField : SpringUtils.getBean(ElectricFieldService.class).list()) {
            System.out.println(electricField.getStationCode());


            ForecastPowerShortTermRegulation forecastPowerShortTermRegulation = DataStore.getInstance().getData(electricField.getStationCode(), ForecastPowerShortTermRegulation.class);
            System.out.println(forecastPowerShortTermRegulation.getFpValue() + "," + forecastPowerShortTermRegulation.getTime());


            ForecastPowerUltraShortTermRegulation forecastPowerUltraShortTermRegulation = DataStore.getInstance().getData(electricField.getStationCode(), ForecastPowerUltraShortTermRegulation.class);
            System.out.println(forecastPowerUltraShortTermRegulation.getFpValue() + "," + forecastPowerUltraShortTermRegulation.getTime());

            PowerStationStatusData powerStationStatusData = DataStore.getInstance().getData(electricField.getStationCode(), PowerStationStatusData.class);
            System.out.println(powerStationStatusData.toString() + "," + powerStationStatusData.getTime());
            if (electricField.getElectricFieldTypeEnum().equals("E2")) {
                WindTowerStatusData data = DataStore.getInstance().getData(electricField.getStationCode(), WindTowerStatusData.class);
                System.out.println(data + "," + data.getTime());
            } else {
                WeatherStationStatusData data = DataStore.getInstance().getData(electricField.getStationCode(), WeatherStationStatusData.class);
                System.out.println(data + "," + data.getTime());

            }
        }
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        AlarmLog.getInstance().initMap();
        DataStore.getInstance().init();
    }
}
