package com.huatek.busi.task;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import com.huatek.busi.service.gis.*;
import com.huatek.busi.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.huatek.busi.common.CacMonitorTypeCode;
import com.huatek.busi.common.SiteConstants.GisTreeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.UploadCycle.ConfigField;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.FailureRateService;
import com.huatek.busi.service.HwTemperIncreaseRateService;
import com.huatek.busi.service.SiteUploadCycleService;
import com.huatek.busi.service.arrester.RptGisMonitorService;
import com.huatek.busi.service.substation.SubstationMicGisService;
import com.huatek.busi.service.transformer.TransformerInfraredTemperatureService;
import com.huatek.busi.service.transformer.TransformerMeteorologicalService;
import com.huatek.busi.service.transformer.TransformerPdService;
import com.huatek.busi.service.transformer.TransformerPdStatisticsService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: GIS 开关定时器
 * @FullClassPath: com.frame.job.task.GisKaiGuanTask
 * @Description: 中标任务
 * @author: hwadley
 * @date:
 * @version: 1.0
 */
@Slf4j
@Component("gisKaiGuanTask")
public class GisKaiGuanTask extends AbstractBusiTaskExecutor {

    @Autowired
    private RptGisTemperSummaryService rptGisTemperSummaryService;
    @Autowired
    private RptGisMonitorSf6ChartsService rptGisMonitorSf6ChartsService;
    @Autowired
    private FailureRateService failureRateService;
    @Autowired
    private SiteUploadCycleService siteUploadCycleService;
    @Autowired
    private TransformerInfraredTemperatureService transformerInfraredTemperatureService;
    @Autowired
    private GisSf6PressureService gisSf6PressureService;
    @Autowired
    private HwTemperIncreaseRateService hwTemperIncreaseRateService;
    @Autowired
    private TransformerMeteorologicalService transformerMeteorologicalService;
    @Autowired
    private SubstationMicGisService substationMicGisService;
    @Autowired
    private TransformerPdService transformerPdService;
    @Autowired
    private TransformerPdStatisticsService transformerPdStatisticsService;
    @Autowired
    private BusiSensorService busiSensorService;
    @Autowired
    private GisReadTableService gisReadTableService;
    @Autowired
    private GisSf6IncreaseRateCacheService gisSf6IncreaseRateCacheService;

    @Autowired
    private GisSf6MonitoringStatisticsService gisSf6MonitoringStatisticsService;

    @Autowired
    private RptGisMonitorService rptGisMonitorService;

    @Autowired
    private Sf6EnvService sf6EnvService;
    @Override
    public void execute() throws Exception {
        super.execute();
    }

    @Override
    public void execute(LocalDate day) {
        log.info("gis开关机评测开始时间------------------------------------------------{} start...", day);

        // gis局放
        gispd(day);

        /**
         * 机械特性(断路器特性)-- 每天数据汇总 （原始数据表为gis_mechanical_properties）(从原始数据表 插入到每日汇总表 ) SF6 日泄漏率 -- 每日 按照设备编号(sersorid)
         * 汇总一条数据(从原始数据表 插入到每日汇总表 ) SF6 每日 按照设备编号(sersorid) 、温度、微水、压力 汇总一条数据 (从原始数据表 插入到每日汇总表 )
         */
        rptGisMonitorService.updaeGisMonitorByDay(day);
        
        rptGisMonitorSf6ChartsService.saveRptGisMonitorSf6Charts(day);
        // GIS-SF6: 缺失率、增长率统计
        gisSf6PressureStatistics(day);

        // 统计GIS--红外测温信息 每天数据汇总
        rptGisTemperSummaryService.inserData2Summary(day);

        // GIS-红外测温：缺失率、增长率统计
        gisInfraredTemperatureStatistics(day);

        // gis气象
        qiXiang(day);

        // gis图片读表数据
        gisRead(day);

        //GIS SF6ENV
        gisSf6EnvStatistics(day);

        log.info("{} end!", day);
    }

    /**
     * gis局放
     * 
     * @param day
     */
    private void gispd(LocalDate day) {
        List<TransformerPdCountVo> list = transformerPdService.getListEquInfo(CacMonitorTypeCode.GIS_PD.getCode(), day);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (TransformerPdCountVo item : list) {
            transformerPdStatisticsService.saveTransformerPdStatistics(item, day);
        }
    }

    /**
     * gis图片读表数据
     * 
     * @param day
     */
    private void gisRead(LocalDate day) {
        List<BusiSensorVo> sensorList = findSensorList(GisTreeEnum.TP_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        LocalDateTime startTime = day.atStartOfDay();
        LocalDateTime endTime = day.plusDays(1).atStartOfDay();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.pzzz);
            // 数据统计
            ReadTableCountVo item =
                gisReadTableService.getListEquInfo(sensor.getSiteId(), sensor.getSensorCode(), startTime, endTime);
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
        }
    }

    private void gisSf6PressureStatistics(LocalDate day) {
        List<BusiSensorVo> sensorList = this.findSensorList(GisTreeEnum.SF6_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        String cacMonitorTypeCode = CacMonitorTypeCode.GIS_SF6Pressure.getCode();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.sf6);
            // 数据统计
            GisSf6PressureStatisticsVo item =
                gisSf6PressureService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
            // 保存增长率
            if(item != null) {
                gisSf6IncreaseRateCacheService.saveGisSf6PressureGrowthRate(day, item);
            }
        }
    }
    private void gisSf6EnvStatistics(LocalDate day) {
        List<BusiSensorVo> sensorList = this.findSensorList(GisTreeEnum.HJJC_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        String cacMonitorTypeCode = CacMonitorTypeCode.GIS_SF6ENV.getCode();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.sf6jczz);
            // 数据统计

            Sf6EnvStatisticsVo listEquInfo = sf6EnvService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, listEquInfo == null ? 0 : listEquInfo.getDataCount());
            // 保存增长率
            if(listEquInfo != null) {
                gisSf6MonitoringStatisticsService.saveGisSf6MonitoringStatistics( listEquInfo,day);
            }
        }
    }
    private void gisInfraredTemperatureStatistics(LocalDate day) {
        List<BusiSensorVo> sensorList = this.findSensorList(GisTreeEnum.HW_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        String cacMonitorTypeCode = CacMonitorTypeCode.GIS_InfraredTemperature.getCode();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.gishw);
            // 数据统计
            InfraredTemperatureStatisticsVo item =
                transformerInfraredTemperatureService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
            // TODO 增长率 也得改
            if (item != null) {
                hwTemperIncreaseRateService.saveInfraredTemperatureGrowthRate(item, day);
            }
        }
    }

    private void qiXiang(LocalDate day) {
        List<BusiSensorVo> sensorList = this.findSensorList(GisTreeEnum.QX_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        String cacMonitorTypeCode = CacMonitorTypeCode.GIS_METEOROLOGICAL.getCode();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.gisqx);
            // 数据统计
            MeteorologicalStatisticsVo item =
                transformerMeteorologicalService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
            // TODO 保存统计信息
            if (item != null) {
                substationMicGisService.saveSubstationMicGis(item, day);
            }
        }
    }

    private List<BusiSensorVo> findSensorList(String equipmentType) {
        return busiSensorService.findSensorList(SiteTreeEnum.GIS, new String[] {equipmentType});
    }
}