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

import java.math.BigDecimal;
import java.util.Date;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.industryservice.domain.BizGrassland;

/**
 * 草场管理入参校验。除草场名称、面积外均为选填；有值时校验格式与区间。
 */
public final class GrasslandValidation
{
    private static final BigDecimal ZERO = BigDecimal.ZERO;

    private static final BigDecimal ONE = BigDecimal.ONE;

    private GrasslandValidation()
    {
    }

    public static void trimFieldsForSave(BizGrassland row)
    {
        if (row == null)
        {
            return;
        }
        if (row.getGrasslandName() != null)
        {
            row.setGrasslandName(row.getGrasslandName().trim());
        }
        if (row.getRemark() != null)
        {
            String remark = row.getRemark().trim();
            row.setRemark(remark.isEmpty() ? null : remark);
        }
    }

    public static void validateGrasslandName(String grasslandName)
    {
        if (StringUtils.isEmpty(grasslandName) || grasslandName.length() < 1 || grasslandName.length() > 10)
        {
            throw new ServiceException("草场名称须为1～10个字符");
        }
    }

    public static void validateDegradationLevel(Integer degradationLevel)
    {
        if (degradationLevel == null)
        {
            return;
        }
        if (degradationLevel < GrasslandRules.DEGRADATION_NONE
                || degradationLevel > GrasslandRules.DEGRADATION_EXTREME)
        {
            throw new ServiceException("退化等级须为1～5（1未退化5极重度）");
        }
    }

    public static void validateUtilizationType(Integer utilizationType)
    {
        if (utilizationType == null)
        {
            return;
        }
        if (utilizationType < GrasslandRules.UTILIZATION_BAN
                || utilizationType > GrasslandRules.UTILIZATION_YEAR_ROUND)
        {
            throw new ServiceException("利用类型须为1～3（1禁牧2轮牧3全年放牧）");
        }
    }

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

    public static void validateOptionalPositiveArea(BigDecimal area, String fieldLabel)
    {
        if (area == null)
        {
            return;
        }
        if (area.compareTo(ZERO) <= 0)
        {
            throw new ServiceException(fieldLabel + "须为大于0的数字（亩）");
        }
    }

    public static void validateGrassYieldPerMu(BigDecimal grassYieldPerMu)
    {
        if (grassYieldPerMu == null)
        {
            return;
        }
        if (grassYieldPerMu.compareTo(ZERO) <= 0)
        {
            throw new ServiceException("亩产草量须为大于0的数字（kg/亩）");
        }
    }

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

    public static void validateGrazingBanDates(Date start, Date end)
    {
        if (start == null && end == null)
        {
            return;
        }
        if (start == null || end == null)
        {
            throw new ServiceException("禁牧开始、结束日期须同时填写或同时留空");
        }
        if (start.after(end))
        {
            throw new ServiceException("禁牧开始日期不能晚于结束日期");
        }
    }

    public static void validateRotationPlotCount(Integer rotationPlotCount)
    {
        if (rotationPlotCount == null)
        {
            return;
        }
        if (rotationPlotCount < 1)
        {
            throw new ServiceException("轮牧小区数量须为大于等于1的整数（块）");
        }
    }

    public static void validateRotationGrazingDays(Integer rotationGrazingDays)
    {
        if (rotationGrazingDays == null)
        {
            return;
        }
        if (rotationGrazingDays < 1)
        {
            throw new ServiceException("小区放牧天数须为大于等于1的整数（天）");
        }
    }

    public static void validateRotationCycleDays(Integer rotationCycleDays)
    {
        if (rotationCycleDays == null)
        {
            return;
        }
        if (rotationCycleDays < 1)
        {
            throw new ServiceException("轮牧周期须为大于等于1的整数（天）");
        }
    }

    public static void validateRemark(String remark)
    {
        if (remark != null && remark.length() > 100)
        {
            throw new ServiceException("备注不能超过100个字符");
        }
    }

    public static void validateRowForSave(BizGrassland row)
    {
        trimFieldsForSave(row);
        validateGrasslandName(row.getGrasslandName());
        validateAreaMu(row.getAreaMu());
        validateOptionalPositiveArea(row.getGrazingBanArea(), "禁牧面积");
        validateOptionalPositiveArea(row.getGrassBalanceArea(), "草畜平衡面积");
        validateDegradationLevel(row.getDegradationLevel());
        validateUtilizationType(row.getUtilizationType());
        validateGrassYieldPerMu(row.getGrassYieldPerMu());
        validateUtilizationRate(row.getUtilizationRate());
        validateGrazingBanDates(row.getGrazingBanStartDate(), row.getGrazingBanEndDate());
        validateRotationPlotCount(row.getRotationPlotCount());
        validateRotationGrazingDays(row.getRotationGrazingDays());
        validateRotationCycleDays(row.getRotationCycleDays());
        validateRemark(row.getRemark());
    }
}
