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

import java.math.BigDecimal;
import java.math.RoundingMode;
import com.ruoyi.web.modules.industryservice.domain.BizVaccinationData;

/**
 * 疫苗接种派生字段。
 * <p>免疫率 = (实免数 / 应免数) × 100，保留 2 位小数（应免数 &gt; 0 且实免数非空时）。</p>
 */
public final class VaccinationDataDerivedSupport
{
    private VaccinationDataDerivedSupport()
    {
    }

    public static void applyDerived(BizVaccinationData row)
    {
        if (row == null)
        {
            return;
        }
        Integer should = row.getShouldVaccinateCount();
        Integer actual = row.getActualVaccinateCount();
        if (should == null || should <= 0 || actual == null)
        {
            row.setImmunityRate(null);
            return;
        }
        row.setImmunityRate(BigDecimal.valueOf(actual.longValue())
                .multiply(BigDecimal.valueOf(100))
                .divide(BigDecimal.valueOf(should.longValue()), 2, RoundingMode.HALF_UP));
    }

    /** 导出/展示用百分比文案，如 98.33%（库内已是百分数值） */
    public static String formatImmunityRatePercent(BigDecimal rate)
    {
        if (rate == null)
        {
            return "";
        }
        return rate.setScale(2, RoundingMode.HALF_UP).toPlainString() + "%";
    }
}
