package com.ruoyi.web.modules.trading.support;

import java.math.BigDecimal;
import java.math.RoundingMode;
import com.ruoyi.common.exception.ServiceException;

/**
 * 均价 = (最低价 + 最高价) / 2，保留 2 位小数。
 */
public final class AvgPriceCalculator
{
    private AvgPriceCalculator()
    {
    }

    public static BigDecimal calculate(BigDecimal priceMin, BigDecimal priceMax)
    {
        if (priceMin == null || priceMax == null)
        {
            throw new ServiceException("最低价与最高价不能为空");
        }
        if (priceMin.compareTo(BigDecimal.ZERO) <= 0 || priceMax.compareTo(BigDecimal.ZERO) <= 0)
        {
            throw new ServiceException("价格须为正数");
        }
        if (priceMin.compareTo(priceMax) > 0)
        {
            throw new ServiceException("最低价不能大于最高价");
        }
        return priceMin.add(priceMax).divide(new BigDecimal("2"), 2, RoundingMode.HALF_UP);
    }
}
