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

import java.math.BigDecimal;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.dto.GrassLivestockBalanceCalculateRequest;

/**
 * 草畜平衡计算入参校验。
 */
public final class GrassLivestockBalanceValidation
{
    private static final BigDecimal ZERO = BigDecimal.ZERO;

    private static final BigDecimal ONE = BigDecimal.ONE;

    private GrassLivestockBalanceValidation()
    {
    }

    public static void validateCalculateRequest(GrassLivestockBalanceCalculateRequest request)
    {
        if (request == null)
        {
            throw new ServiceException("请求参数不能为空");
        }
        if (request.getGrasslandId() == null)
        {
            throw new ServiceException("请选择草场");
        }
        validateAreaMu(request.getAreaMu());
        validateGrassYieldPerMu(request.getGrassYieldPerMu());
        validateUtilizationRate(request.getUtilizationRate());
        validatePositive(request.getSheepUnitAnnualForageKg(), "羊单位年需草量须大于0");
        validatePositive(request.getSheepUnitConvertCoeff(), "羊单位折算系数须大于0");
        validateBreedingHeadCount(request.getBreedingHeadCount());
    }

    public static void validateAreaMu(BigDecimal areaMu)
    {
        if (areaMu == null || areaMu.compareTo(ZERO) <= 0)
        {
            throw new ServiceException("草场面积须大于0（亩）");
        }
    }

    public static void validateGrassYieldPerMu(BigDecimal grassYieldPerMu)
    {
        if (grassYieldPerMu == null || grassYieldPerMu.compareTo(ZERO) < 0)
        {
            throw new ServiceException("亩产草量不能为负数");
        }
    }

    public static void validateUtilizationRate(BigDecimal utilizationRate)
    {
        if (utilizationRate == null || utilizationRate.compareTo(ZERO) < 0
                || utilizationRate.compareTo(ONE) > 0)
        {
            throw new ServiceException("利用率须在0～1之间");
        }
    }

    public static void validatePositive(BigDecimal value, String message)
    {
        if (value == null || value.compareTo(ZERO) <= 0)
        {
            throw new ServiceException(message);
        }
    }

    public static void validateBreedingHeadCount(Integer breedingHeadCount)
    {
        if (breedingHeadCount == null || breedingHeadCount < 0)
        {
            throw new ServiceException("养殖数量须为不小于0的整数");
        }
    }
}
