package com.jiayue.ipfcst.dataexchange.service;

import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import com.jiayue.ipfcst.common.data.entity.ProtocolGatherDataPoint;
import com.jiayue.ipfcst.common.data.entity.WeatherStationInfo;
import com.jiayue.ipfcst.common.data.entity.WindTowerInfo;
import com.jiayue.ipfcst.console.service.WeatherStationInfoService;
import com.jiayue.ipfcst.console.service.WindTowerInfoService;
import com.jiayue.ipfcst.dataexchange.dataprocesser.EquipmentStatisticalDataCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 计算设备属性的统计数据的服务类
 *
 * @author: xiuwei
 * @version:
 */
@Service
public class EquipmentStatisticalDataCalculatorService {

  List<EquipmentStatisticalDataCalculator> equipmentStatisticalDataCalculators = null;

  @Autowired
  WeatherStationInfoService weatherStationInfoService;

  @Autowired
  WindTowerInfoService windTowerInfoService;

  @Autowired
  ProtocolGatherDataPointService protocolGatherDataPointService;

  /**
   * 初始化
   */
  public void init() {
    equipmentStatisticalDataCalculators = new ArrayList<>();
    List<ProtocolGatherDataPoint> protocolGatherDataPoints;
    EquipmentStatisticalDataCalculator equipmentStatisticalDataCalculator;
    List<WeatherStationInfo> weatherStationInfos = weatherStationInfoService.getAll();
    for (WeatherStationInfo w : weatherStationInfos) {
      protocolGatherDataPoints = protocolGatherDataPointService.getByEquipmentId(w.getId(), w.getEquipmentType());
      if (protocolGatherDataPoints != null && protocolGatherDataPoints.size() > 0) {
        equipmentStatisticalDataCalculator = new EquipmentStatisticalDataCalculator(w.getEquipmentType(), w.getId(), protocolGatherDataPoints);
        equipmentStatisticalDataCalculators.add(equipmentStatisticalDataCalculator);
      }
    }
    List<WindTowerInfo> windTowerInfos = windTowerInfoService.getAll();
    for (WindTowerInfo w : windTowerInfos) {
      protocolGatherDataPoints = protocolGatherDataPointService.getByEquipmentId(w.getId(), w.getEquipmentType());
      if (protocolGatherDataPoints != null && protocolGatherDataPoints.size() > 0) {
        equipmentStatisticalDataCalculator = new EquipmentStatisticalDataCalculator(w.getEquipmentType(), w.getId(), protocolGatherDataPoints);
        equipmentStatisticalDataCalculators.add(equipmentStatisticalDataCalculator);
      }
    }
    protocolGatherDataPoints = protocolGatherDataPointService.getByEquipmentId(null, EquipmentTypeEnum.POWERSTATION);
    equipmentStatisticalDataCalculator = new EquipmentStatisticalDataCalculator(EquipmentTypeEnum.POWERSTATION, 0, protocolGatherDataPoints);
    equipmentStatisticalDataCalculators.add(equipmentStatisticalDataCalculator);
  }


  public void updateAndSaveData() {
    if (equipmentStatisticalDataCalculators == null) {
      init();
    }
    for (EquipmentStatisticalDataCalculator e : equipmentStatisticalDataCalculators) {
      e.updateAndSave();
    }
  }


}
