package com.cpp.web.core.config.sharding;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cpp.web.domain.alarm.AbnormalAlarm;
import com.cpp.web.domain.cloud.ForecastPowerShortTermCloud;
import com.cpp.web.domain.cloud.ForecastPowerUltraShortTermCloud;
import com.cpp.web.domain.cloud.NwpCloud;
import com.cpp.web.domain.datafactory.ParsingLog;
import com.cpp.web.domain.station.*;
import com.cpp.web.service.alarm.AbnormalAlarmService;
import com.cpp.web.service.cloud.ForecastPowerShortTermCloudService;
import com.cpp.web.service.cloud.ForecastPowerUltraShortTermCloudService;
import com.cpp.web.service.cloud.NwpCloudService;
import com.cpp.web.service.datafactory.ParsingLogService;
import com.cpp.web.service.station.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p> @Title ShardingTablesLoadRunner
 * <p> @Description 项目启动后，读取已有分表，进行缓存
 *
 * @author ACGkaka
 * @date 2022/12/20 15:41
 */
@Slf4j
@Order(1) // 数字越小，越先执行
@Component
@RequiredArgsConstructor
public class ShardingTablesLoadRunner{


    private final ForecastPowerUltraShortTermRegulationService forecastPowerUltraShortTermRegulationService;
    private final ForecastPowerUltraShortTermCloudService forecastPowerUltraShortTermCloudService;
    private final ForecastPowerUltraShortTermStationService forecastPowerUltraShortTermStationService;
    private final ForecastPowerShortTermRegulationService forecastPowerShortTermRegulationService;
    private final ForecastPowerShortTermCloudService forecastPowerShortTermCloudService;
    private final ForecastPowerShortTermStationService forecastPowerShortTermStationService;
    private final NwpCloudService nwpCloudService;
    private final NwpStationService nwpStationService;
    private final WeatherStationStatusDataService weatherStationStatusDataService;
    private final WindTowerStatusDataService windTowerStatusDataService;
    private final WindTurbineStatusDataService windTurbineStatusDataService;
    private final InverterStatusDataService inverterStatusDataService;
    private final PowerStationStatusDataService powerStationStatusDataService;
    private final ParsingLogService parsingLogService;
    private final ForecastPowerShortTermSendService forecastPowerShortTermSendService;


    @PostConstruct
    public void run() {
        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ForecastPowerUltraShortTermRegulation> forecastPowerUltraShortTermRegulationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        forecastPowerUltraShortTermRegulationLambdaQueryWrapper.eq(ForecastPowerUltraShortTermRegulation::getTime, new Date()).last("limit 1");
        forecastPowerUltraShortTermRegulationService.list(forecastPowerUltraShortTermRegulationLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ForecastPowerUltraShortTermCloud> forecastPowerUltraShortTermCloudLambdaQueryWrapper = new LambdaQueryWrapper<>();
        forecastPowerUltraShortTermCloudLambdaQueryWrapper.eq(ForecastPowerUltraShortTermCloud::getTime, new Date()).last("limit 1");
        forecastPowerUltraShortTermCloudService.list(forecastPowerUltraShortTermCloudLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ForecastPowerUltraShortTermStation> forecastPowerUltraShortTermStationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        forecastPowerUltraShortTermStationLambdaQueryWrapper.eq(ForecastPowerUltraShortTermStation::getTime, new Date()).last("limit 1");
        forecastPowerUltraShortTermStationService.list(forecastPowerUltraShortTermStationLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ForecastPowerShortTermCloud> forecastPowerShortTermCloudLambdaQueryWrapper = new LambdaQueryWrapper<>();
        forecastPowerShortTermCloudLambdaQueryWrapper.eq(ForecastPowerShortTermCloud::getTime, new Date()).last("limit 1");
        forecastPowerShortTermCloudService.list(forecastPowerShortTermCloudLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ForecastPowerShortTermRegulation> forecastPowerShortTermRegulationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        forecastPowerShortTermRegulationLambdaQueryWrapper.eq(ForecastPowerShortTermRegulation::getTime, new Date()).last("limit 1");
        forecastPowerShortTermRegulationService.list(forecastPowerShortTermRegulationLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ForecastPowerShortTermStation> forecastPowerShortTermStationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        forecastPowerShortTermStationLambdaQueryWrapper.eq(ForecastPowerShortTermStation::getTime, new Date()).last("limit 1");
        forecastPowerShortTermStationService.list(forecastPowerShortTermStationLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ForecastPowerShortTermSend> forecastPowerShortTermSendLambdaQueryWrapper = new LambdaQueryWrapper<>();
        forecastPowerShortTermSendLambdaQueryWrapper.eq(ForecastPowerShortTermSend::getTime, new Date()).last("limit 1");
        forecastPowerShortTermSendService.list(forecastPowerShortTermSendLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<WeatherStationStatusData> weatherStationStatusDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        weatherStationStatusDataLambdaQueryWrapper.eq(WeatherStationStatusData::getTime, new Date()).last("limit 1");
        weatherStationStatusDataService.list(weatherStationStatusDataLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<WindTowerStatusData> windTowerStatusDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        windTowerStatusDataLambdaQueryWrapper.eq(WindTowerStatusData::getTime, new Date()).last("limit 1");
        windTowerStatusDataService.list(windTowerStatusDataLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<WindTurbineStatusData> windTurbineStatusDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        windTurbineStatusDataLambdaQueryWrapper.eq(WindTurbineStatusData::getTime, new Date()).last("limit 1");
        windTurbineStatusDataService.list(windTurbineStatusDataLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<InverterStatusData> inverterStatusDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        inverterStatusDataLambdaQueryWrapper.eq(InverterStatusData::getTime, new Date()).last("limit 1");
        inverterStatusDataService.list(inverterStatusDataLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<PowerStationStatusData> powerStationStatusDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        powerStationStatusDataLambdaQueryWrapper.eq(PowerStationStatusData::getTime, new Date()).last("limit 1");
        powerStationStatusDataService.list(powerStationStatusDataLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ParsingLog> parsingLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        parsingLogLambdaQueryWrapper.eq(ParsingLog::getCreateTime, new Date()).last("limit 1");
        parsingLogService.list(parsingLogLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<NwpCloud> nwpCloudLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nwpCloudLambdaQueryWrapper.eq(NwpCloud::getTime, new Date()).last("limit 1");
        nwpCloudService.list(nwpCloudLambdaQueryWrapper);

        // 读取已有分表，进行缓存
        LambdaQueryWrapper<NwpStation> nwpStationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nwpStationLambdaQueryWrapper.eq(NwpStation::getTime, new Date()).last("limit 1");
        nwpStationService.list(nwpStationLambdaQueryWrapper);



        log.info(">>>>>>>>>> 【ShardingTablesLoadRunner】缓存已有分表成功 <<<<<<<<<<");
    }
}
