package com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.iml;

import com.jiayue.ipfcst.common.core.util.SpringContextHolder;
import com.jiayue.ipfcst.common.data.constant.enums.AbnormalCode;
import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import com.jiayue.ipfcst.common.data.entity.ProtocolGatherDataPoint;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.NumOrBool;
import com.jiayue.ipfcst.dataexchange.exception.DataExchangeException;
import com.jiayue.ipfcst.dataexchange.service.EnvironmentDataAnyTimeGenerator;
import com.jiayue.ipfcst.dataexchange.service.ProtocolGatherDataPointService;
import com.jiayue.ipfcst.dataexchange.service.equipmentstatusdata.WeatherStationStatusDataService;
import com.jiayue.ipfcst.dataexchange.service.equipmentstatusdata.WindTowerStatusDataService;

import java.math.BigDecimal;

/**
 * 测风塔或气象站数据获取器
 *
 * @author xiuwei
 * @date 2021/11/18
 */
public class WindTowerOrWeatherStationDataGainer extends GetDataFromContainer<BigDecimal> {


  private WindTowerStatusDataService windTowerStatusDataService = SpringContextHolder.getBean(WindTowerStatusDataService.class);

  private EnvironmentDataAnyTimeGenerator environmentDataAnyTimeGenerator = SpringContextHolder.getBean(EnvironmentDataAnyTimeGenerator.class);

  private WeatherStationStatusDataService weatherStationStatusDataService = SpringContextHolder.getBean(WeatherStationStatusDataService.class);

  private ProtocolGatherDataPointService gatherDataPointService = SpringContextHolder.getBean(ProtocolGatherDataPointService.class);


  /**
   * 设备类型
   */
  private EquipmentTypeEnum equipmentType;

  /**
   * 设备编号
   */
  private Integer equipmentNo;

  /**
   * 字段名
   */
  private String fieldName;

  public WindTowerOrWeatherStationDataGainer(NumOrBool expectedType, Integer sourcePoint, Double magnification) {
    super(expectedType, sourcePoint, magnification);
    ProtocolGatherDataPoint dataPoint = gatherDataPointService.getDataPointById(sourcePoint);
    this.equipmentType = dataPoint.getEquipmentType();
    if(!(EquipmentTypeEnum.WINDTOWER==this.equipmentType ||EquipmentTypeEnum.WEATHERSTATION==this.equipmentType)){
      throw new RuntimeException("该点位非测风塔或气象站点位");
    }
    this.equipmentNo = dataPoint.getEquipmentId();
    this.fieldName = dataPoint.getEquipmentAttribute().getFieldName();
  }


  @Override
  public BigDecimal getData() throws DataExchangeException {
    if (EquipmentTypeEnum.WINDTOWER == equipmentType) {
      if (AbnormalCode.NORMAL!=windTowerStatusDataService.getIsAbnormalData(equipmentNo)) {
        try {
          return environmentDataAnyTimeGenerator.getWindTowerDataGenerator(this.equipmentNo).generateWindTowerData().getNumberValueByFiled(fieldName);
        } catch (IllegalAccessException e) {
          return BigDecimal.ZERO;
        }
      }
    }
    if (EquipmentTypeEnum.WEATHERSTATION == equipmentType) {
      //TODO 气象站
    }
    return super.getData();
  }


}
