package com.huatek.busi.task;

import com.huatek.busi.common.CacMonitorTypeCode;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.UploadCycle.ConfigField;
import com.huatek.busi.service.SiteUploadCycleService;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.FailureRateService;
import com.huatek.busi.service.cable.BusiCableCirculationService;
import com.huatek.busi.service.cable.CableCirculationStatisticsService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;
import com.huatek.busi.vo.BusiSensorVo;

import com.huatek.busi.vo.InfraredTemperatureStatisticsVo;
import com.huatek.busi.vo.cable.BusiCableCirculationStatisticsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

@Slf4j
@Component("cableCirculationStatisticsTask")
public class CableCirculationStatisticsTask extends AbstractBusiTaskExecutor {
    @Autowired
    CableCirculationStatisticsService cableCirculationStatisticsService;
    @Autowired
    private SiteUploadCycleService siteUploadCycleService;
    @Autowired
    private FailureRateService failureRateService;
    @Autowired
    private BusiSensorService busiSensorService;
    @Autowired
    private BusiCableCirculationService busiCableCirculationService;

    @Override
    public void execute() throws Exception {
        super.execute();
    }
    @Override
    public void execute(LocalDate day) {
        log.info("电缆环流每天汇总前一天监测数据开始时间------------------------------------------------{} start...", day);

        // 电缆环流每天汇总前一天监测数据
        cableCirculationStatisticsService.saveCableCirculationStatistics(day);
        // 查询其他电缆环流Sensor
        List<BusiSensorVo> sensorListOther = busiSensorService.findSensorList(SiteConstants.SiteTreeEnum.OTHER_EQM, new String[] {SiteConstants.OtherTreeEnum.DLHL_EQM.getValue()});
        if (!CollectionUtils.isEmpty(sensorListOther)) {
            String cacMonitorTypeCode = CacMonitorTypeCode.OTHER_DLHL.getCode();
            for (BusiSensorVo sensor : sensorListOther) {
                // 上传周期
                BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.qtdlhl);
                // 数据统计
                BusiCableCirculationStatisticsVo item =
                        busiCableCirculationService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
                // 保存缺失率
                failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
            }
        }

        // 查询电缆电缆环流Sensor
        List<BusiSensorVo> sensorListCable = busiSensorService.findSensorList(SiteConstants.SiteTreeEnum.DL_EQM, new String[] {SiteConstants.CableTreeEnum.DLHL_EQM.getValue()});
        if (!CollectionUtils.isEmpty(sensorListCable)) {
            String cacMonitorTypeCode = CacMonitorTypeCode.DL_DLHL.getCode();
            for (BusiSensorVo sensor : sensorListCable) {
                // 上传周期
                BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.dldlhl);
                // 数据统计
                BusiCableCirculationStatisticsVo item =
                        busiCableCirculationService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
                // 保存缺失率
                failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
            }
        }
        log.info("{} end!", day);

    }

}
