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

import java.math.BigDecimal;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.industryservice.domain.BizFarmerHousehold;

/**
 * 农牧户数据填报校验
 */
public final class FarmerHouseholdValidation
{
    private FarmerHouseholdValidation()
    {
    }

    public static void validateRowForSave(BizFarmerHousehold row, TownDeptSupport townDeptSupport)
    {
        if (row == null)
        {
            throw new ServiceException("参数不能为空");
        }
        YakHerdInventoryMonthSupport.validateStatPeriod(row.getStatYear(), row.getStatMonth());
        if (row.getVillageDeptId() == null)
        {
            throw new ServiceException("所属村不能为空");
        }
        SysDept village = townDeptSupport.requireReportVillage(row.getVillageDeptId());
        if (village == null)
        {
            throw new ServiceException("所属村不合法");
        }
        SysDept town = townDeptSupport.requireReportTown(village.getParentId());
        if (town == null)
        {
            throw new ServiceException("所属村的上级乡镇不合法");
        }
        row.setVillageName(village.getDeptName());
        row.setTownDeptId(town.getDeptId());
        row.setTownName(town.getDeptName());

        row.setIsPureHerder(FarmerHouseholdDerivedSupport.normalizeYesNo(row.getIsPureHerder(), "是否是纯牧户"));
        row.setIsNoLivestock(FarmerHouseholdDerivedSupport.normalizeYesNo(row.getIsNoLivestock(), "是否无畜户"));
        row.setIsExtinctHousehold(
                FarmerHouseholdDerivedSupport.normalizeYesNo(row.getIsExtinctHousehold(), "是否绝户"));
        row.setIsOverloadHerder(
                FarmerHouseholdDerivedSupport.normalizeYesNo(row.getIsOverloadHerder(), "是否超载牧户"));
        row.setNeedCap(FarmerHouseholdDerivedSupport.normalizeYesNo(row.getNeedCap(), "是否需要封顶"));
        row.setNeedFloor(FarmerHouseholdDerivedSupport.normalizeYesNo(row.getNeedFloor(), "是否需要保底"));

        validateNonNegative(row.getHouseholdCount(), "农牧户数");
        validateNonNegative(row.getPopulationCount(), "农牧人数");
        validateNonNegativeDecimal(row.getGrazingBanArea(), "禁牧面积");
        validateNonNegativeDecimal(row.getGrassBalanceArea(), "草畜平衡面积");
        validateNonNegativeDecimal(row.getApprovedCarryingCapacity(), "核定年末草畜平衡载畜量");
        validateNonNegativeDecimal(row.getYearEndStockSheepUnit(), "年底牲畜存栏");
        validateNonNegativeDecimal(row.getGrazingBanSubsidy(), "禁牧补助");
        validateNonNegativeDecimal(row.getGrassBalanceReward(), "草畜平衡奖励");
        validateNonNegativeDecimal(row.getAfterCapFloorSubsidy(), "封顶保底后享受补助奖励总资金");
        validateNonNegativeDecimal(row.getVillageSupervisorSubsidy(), "村级草原监督员补助");

        FarmerHouseholdDerivedSupport.applyDerived(row);

        if (StringUtils.isNotEmpty(row.getRemark()) && row.getRemark().length() > 500)
        {
            throw new ServiceException("备注不能超过500字");
        }
    }

    private static void validateNonNegative(Integer value, String label)
    {
        if (value != null && value < 0)
        {
            throw new ServiceException(label + "不能为负数");
        }
    }

    private static void validateNonNegativeDecimal(BigDecimal value, String label)
    {
        if (value != null && value.compareTo(BigDecimal.ZERO) < 0)
        {
            throw new ServiceException(label + "不能为负数");
        }
    }
}
