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 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.ArresterTreeEnum;
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.SiteUploadCycleService;
import com.huatek.busi.service.arrester.ArresterReadTableService;
import com.huatek.busi.service.arrester.LightningrodImService;
import com.huatek.busi.service.arrester.RptAretMonitorSummaryService;
import com.huatek.busi.service.arrester.RptAretTemperSummaryChaobiaolvService;
import com.huatek.busi.service.arrester.RptAretTemperSummaryService;
import com.huatek.busi.service.capacitance.CapacitanceEquipmentImService;
import com.huatek.busi.service.capacitance.CapacitanceEquipmentSummaryService;
import com.huatek.busi.service.substation.SubstationMicBlqService;
import com.huatek.busi.service.transformer.TransformerInfraredTemperatureService;
import com.huatek.busi.service.transformer.TransformerMeteorologicalService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.busi.vo.CapacitanceEquipmentImStatisticsVo;
import com.huatek.busi.vo.InfraredTemperatureStatisticsVo;
import com.huatek.busi.vo.LightningrodImStatisticsVo;
import com.huatek.busi.vo.MeteorologicalStatisticsVo;
import com.huatek.busi.vo.ReadTableCountVo;

import lombok.extern.slf4j.Slf4j;

/**
 * 避雷器定时器
 * 
 * @author buck_guo
 * @date 2019/07/16
 */
@Slf4j
@Component("beileiqiFormTask")
public class BileiqiFormTask extends AbstractBusiTaskExecutor {
    @Autowired
    private RptAretMonitorSummaryService rptAretMonitorSummaryService;
    @Autowired
    private RptAretTemperSummaryService rptAretTemperSummaryService;
    @Autowired
    private FailureRateService failureRateService;
    @Autowired
    private TransformerInfraredTemperatureService transformerInfraredTemperatureService;
    @Autowired
    private RptAretTemperSummaryChaobiaolvService rptAretTemperSummaryChaobiaolvService;
    @Autowired
    private SiteUploadCycleService siteUploadCycleService;
    @Autowired
    private CapacitanceEquipmentSummaryService capacitanceEquipmentSummaryService;
    @Autowired
    private SubstationMicBlqService substationMicBlqService;
    @Autowired
    private TransformerMeteorologicalService transformerMeteorologicalService;
    @Autowired
    private LightningrodImService lightningrodImService;
    @Autowired
    private CapacitanceEquipmentImService capacitanceEquipmentImService;
    @Autowired
    private BusiSensorService busiSensorService;
    @Autowired
    private ArresterReadTableService arresterReadTableService;

    @Override
    public void execute() throws Exception {
        super.execute();
    }

    @Override
    public void execute(String params) {
        LocalDate day = toLocalDate(params);
        execute(day);
    }

    @Override
    public void execute(LocalDate day) {
        log.info("{} start...", day);

        //// 统计避雷器基础信息-- 每天数据汇总
        rptAretMonitorSummaryService.inserData2Summary(day);

        // 避雷器：绝缘监测统计
        lightningrodImStatistics(day);

        //// 统计避雷器--红外测温信息 每天数据汇总
        rptAretTemperSummaryService.inserData2Summary(day);
        //// 统计避雷器--红外测温信息缺失率(故障率) 每天数据汇总
        blqInfraredTemperatureStatistics(day);

        // 避雷器容性每天汇总
        capacitanceEquipmentSummaryService.capacitranceSummary(day);
        // 避雷器：容性统计
        capacitanceEquipmentImStatistics(day);

        // 避雷器气象每天汇总
        qiXiang(day);

        //// 统计避雷器--温度超标率-- 每天数据汇总
        rptAretTemperSummaryChaobiaolvService.inserData2Summary(day);

        // 避雷器读表数据汇总
        arresterReadTable(day);

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

    /**
     * 避雷器读表数据汇总
     * 
     * @param day
     */
    private void arresterReadTable(LocalDate day) {
        List<BusiSensorVo> sensorList = findSensorList(ArresterTreeEnum.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 =
                arresterReadTableService.getListEquInfo(sensor.getSiteId(), sensor.getSensorCode(), startTime, endTime);
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
        }
    }

    private void lightningrodImStatistics(LocalDate day) {
        List<BusiSensorVo> sensorList = this.findSensorList(ArresterTreeEnum.JY_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        String cacMonitorTypeCode = CacMonitorTypeCode.LIGHTNINGROD_IM.getCode();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.blqzz);
            // 数据统计
            LightningrodImStatisticsVo item =
                lightningrodImService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
        }
    }

    private void blqInfraredTemperatureStatistics(LocalDate day) {
        List<BusiSensorVo> sensorList = this.findSensorList(ArresterTreeEnum.HW_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        String cacMonitorTypeCode = CacMonitorTypeCode.LIGHTNINGROD_InfraredTemperature.getCode();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.blqhw);
            // 数据统计
            InfraredTemperatureStatisticsVo item =
                transformerInfraredTemperatureService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
        }
    }

    /**
     * 避雷器-容性缺失率
     * 
     * @param day
     */
    private void capacitanceEquipmentImStatistics(LocalDate day) {
        List<BusiSensorVo> sensorList = this.findSensorList(ArresterTreeEnum.RX_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        String cacMonitorTypeCode = CacMonitorTypeCode.CAPACITIVE_EQUIPMENT_IM.getCode();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.blqrx);
            // 数据统计
            CapacitanceEquipmentImStatisticsVo item =
                capacitanceEquipmentImService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
        }
    }

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

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