package com.gyee.generation.util.math;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Service
public class PowerIntegration {

    /**
     * @param powerData    0, 100.0,
     *                     1, 110.0,
     *                     2, 95.0,
     *                     3, 105.0
     * @param timeInterval 时间间隔，单位为小时
     * @return
     */
    // 计算电量
    public double calculateEnergy(Map<Integer, Double> powerData, double timeInterval) {
        double energy = 0.0;
        try {

            for (int time = 0; time < powerData.size() - 1; time++) {
                double power1 = powerData.get(time);
                double power2 = powerData.get(time + 1);
                double averagePower = new BigDecimal(power1 + power2).divide(new BigDecimal(2), 2, BigDecimal.ROUND_HALF_UP).doubleValue();

                double timeIntervalHours = timeInterval;
                double intervalEnergy = new BigDecimal(averagePower).multiply(new BigDecimal(timeIntervalHours)).doubleValue();
                energy += intervalEnergy;
            }
        } catch (Exception e) {
            System.out.println("");
        }

        return energy;
    }
}
