package com.huatek.busi.task.transformer.part;

import com.huatek.busi.common.CacMonitorTypeCode;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.SiteConstants.TransfromerTreeEnum;
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.transformer.CwzzMonitoringStatisticsService;
import com.huatek.busi.service.transformer.CwzzTemperatureService;
import com.huatek.busi.service.transformer.TransformerInfraredMonitoringStatisticsService;
import com.huatek.busi.service.transformer.TransformerInfraredTemperatureService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.busi.vo.CwzzTemperatureStatisticsVo;
import com.huatek.busi.vo.InfraredTemperatureStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

/**
 * 变压器红外统计
 * 
 * @author anker_cheng
 * @date 2019/10/28
 */
@Component("transformerCwzzTask")
public class TransformerCwzzTask extends AbstractBusiTaskExecutor {
//    @Autowired
//    private TransformerInfraredTemperatureService transformerInfraredTemperatureService;
    @Autowired
    private CwzzTemperatureService cwzzTemperatureService;

    @Autowired
    private SiteUploadCycleService siteUploadCycleService;
    @Autowired
    private FailureRateService failureRateService;
    @Autowired
    private CwzzMonitoringStatisticsService cwzzMonitoringStatisticsService;
    @Autowired
    private BusiSensorService busiSensorService;

    @Override
    public void execute(LocalDate day) {
        List<BusiSensorVo> sensorList = this.findSensorList(TransfromerTreeEnum.CW_EQM.getValue());
        if (CollectionUtils.isEmpty(sensorList)) {
            return;
        }
        String cacMonitorTypeCode = CacMonitorTypeCode.TRANSFORMER_CW.getCode();
        for (BusiSensorVo sensor : sensorList) {
            // 上传周期
            BigDecimal cycle = siteUploadCycleService.getCycle(sensor.getSiteId(), ConfigField.totalcwzz);
            // 数据统计
            CwzzTemperatureStatisticsVo item =
                    cwzzTemperatureService.getListEquInfo(cacMonitorTypeCode, day, sensor.getSensorCode());
            // 保存缺失率
            failureRateService.saveSensorFailureRate(sensor, day, cycle, item == null ? 0 : item.getDataCount());
            //TODO 保存统计信息
            if(item != null) {
                cwzzMonitoringStatisticsService.saveStatistics(day, item);
            }
        }
    }

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