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

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.BizLivestockOwnershipHousehold;

/**
 * 有无畜户数据填报校验：年月、村反推乡镇、计数字段非负。
 */
public final class LivestockOwnershipHouseholdValidation
{
    private LivestockOwnershipHouseholdValidation()
    {
    }

    public static void validateRowForSave(BizLivestockOwnershipHousehold 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());
        validateNonNegative(row.getWithLivestockHouseholdCount(), "有畜户户数");
        validateNonNegative(row.getWithLivestockPopulationCount(), "有畜户人数");
        validateNonNegative(row.getWithLivestockLaborCount(), "有畜户劳动力");
        validateNonNegative(row.getWithLivestockAnimalCount(), "有畜户牲畜数");
        validateNonNegative(row.getWithoutLivestockHouseholdCount(), "无畜户户数");
        validateNonNegative(row.getWithoutLivestockPopulationCount(), "无畜户人数");
        validateNonNegative(row.getWithoutLivestockLaborCount(), "无畜户劳动力");
        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 + "不能为负数");
        }
    }
}
