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

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

/**
 * 实验室检测业务规则与检出率计算。
 */
public final class LabDetectionRules
{
    private static final int RATE_SCALE = 4;

    private LabDetectionRules()
    {
    }

    public static boolean isSampleSourceValid(Integer sampleSource)
    {
        return sampleSource != null && sampleSource >= 1 && sampleSource <= 4;
    }

    public static boolean isNonNegativeInteger(Integer value)
    {
        return value != null && value >= 0 && value == value.intValue();
    }

    /**
     * 检出率 = positiveCount / testQuantity；testQuantity=0 时为 0。
     */
    public static BigDecimal calcDetectionRate(int testQuantity, int positiveCount)
    {
        if (testQuantity <= 0)
        {
            return BigDecimal.ZERO.setScale(RATE_SCALE, RoundingMode.HALF_UP);
        }
        return BigDecimal.valueOf(positiveCount)
                .divide(BigDecimal.valueOf(testQuantity), RATE_SCALE, RoundingMode.HALF_UP);
    }
}
