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

/**
 * 牦牛出栏数据填报校验
 */
public final class YakOutboundReportValidation
{
    private YakOutboundReportValidation()
    {
    }

    public static void validateRowForSave(BizYakOutboundReport 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.getFarmerHouseholdCount(), "农牧户户数");
        validateNonNegative(row.getFarmerPopulationCount(), "农牧户人数");
        validateNonNegative(row.getYakOutboundCount(), "牦牛出栏数");
        validateNonNegative(row.getCattleOutboundCount(), "黄牛/奶牛/犏牛出栏数");
        validateNonNegative(row.getSheepOutboundCount(), "绵羊出栏数");
        validateNonNegative(row.getGoatOutboundCount(), "山羊出栏数");
        validateNonNegative(row.getHorseMuleOutboundCount(), "马骡出栏数");
        validateNonNegative(row.getDonkeyOutboundCount(), "驴出栏数");
        validateNonNegative(row.getSelfConsumptionCattleCount(), "自食牛头数");
        validateNonNegative(row.getSelfConsumptionSheepCount(), "自食绵羊只数");
        validateNonNegative(row.getSelfConsumptionGoatCount(), "自食山羊只数");
        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 + "不能为负数");
        }
    }

    public static String buildSelfConsumptionWarn(BizYakOutboundReport row)
    {
        String warn = null;
        int cattleOutbound = safe(row.getYakOutboundCount()) + safe(row.getCattleOutboundCount());
        if (row.getSelfConsumptionCattleCount() != null && row.getSelfConsumptionCattleCount() > cattleOutbound)
        {
            warn = "自食牛头数大于牦牛与黄牛等出栏数合计";
        }
        if (row.getSheepOutboundCount() != null && row.getSelfConsumptionSheepCount() != null
                && row.getSelfConsumptionSheepCount() > row.getSheepOutboundCount())
        {
            warn = appendWarn(warn, "自食绵羊只数大于绵羊出栏数");
        }
        if (row.getGoatOutboundCount() != null && row.getSelfConsumptionGoatCount() != null
                && row.getSelfConsumptionGoatCount() > row.getGoatOutboundCount())
        {
            warn = appendWarn(warn, "自食山羊只数大于山羊出栏数");
        }
        return warn;
    }

    private static int safe(Integer value)
    {
        return value == null ? 0 : value;
    }

    private static String appendWarn(String existing, String message)
    {
        if (StringUtils.isEmpty(existing))
        {
            return message;
        }
        return existing + "；" + message;
    }
}
