package com.jiayue.ipfcst.dataexchange.dataprocesser;

import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import com.jiayue.ipfcst.common.data.entity.ProtocolDataType;
import com.jiayue.ipfcst.common.data.entity.ProtocolGatherDataPoint;
import org.joda.time.DateTime;

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

/**
 * 设备的统计数据计算器
 *
 * @author: xiuwei
 * @version:
 */
public class EquipmentStatisticalDataCalculator {


  DateTime date = DateTime.now().plusDays(1).withMillisOfDay(0);

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


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

  /**
   * 具体的设备计算器
   */
  List<StatisticalDataCalculator> calculators = new ArrayList<>();


  public EquipmentStatisticalDataCalculator(EquipmentTypeEnum equipmentType, Integer equipmentNo, List<ProtocolGatherDataPoint> dataPoints) {
    this.equipmentType = equipmentType;
    this.equipmentNo = equipmentNo;
    for (ProtocolGatherDataPoint p : dataPoints) {
      if (!p.getProtocolPointType().equals(ProtocolDataType.A)) {
        //不是遥信的遥测数据才能有统计数据
        calculators.add(new StatisticalDataCalculator(p));
      }
    }
  }

  /**
   * 更新和保存数据
   */
  public void updateAndSave() {
    boolean flag = false;
    if (DateTime.now().isAfter(date)) {
      flag = true;
      this.date = DateTime.now().plusDays(1).withMillisOfDay(0);
    }
    for (StatisticalDataCalculator c : calculators) {
      if (flag) {
        c.reset();
      }
      c.updateData();
      c.saveData();
    }
  }

}
