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

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 风险因子权重业务规则。
 */
public final class RiskFactorWeightRules
{
    public static final long SINGLETON_ID = 1L;

    public static final BigDecimal SUM_TOLERANCE = new BigDecimal("0.0001");

    private static final int WEIGHT_SCALE = 4;

    private RiskFactorWeightRules()
    {
    }

    public static boolean isPositiveWeight(BigDecimal weight)
    {
        return weight != null && weight.compareTo(BigDecimal.ZERO) > 0;
    }

    public static BigDecimal calcSum(BigDecimal... weights)
    {
        BigDecimal sum = BigDecimal.ZERO;
        if (weights == null)
        {
            return sum.setScale(WEIGHT_SCALE, RoundingMode.HALF_UP);
        }
        for (BigDecimal w : weights)
        {
            if (w != null)
            {
                sum = sum.add(w);
            }
        }
        return sum.setScale(WEIGHT_SCALE, RoundingMode.HALF_UP);
    }

    public static boolean isSumEqualsOne(BigDecimal... weights)
    {
        BigDecimal sum = calcSum(weights);
        return sum.subtract(BigDecimal.ONE).abs().compareTo(SUM_TOLERANCE) <= 0;
    }
}
