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

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
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.BizFamilyRanch;

/**
 * 家庭牧场数据填报校验
 */
public final class FamilyRanchValidation
{
    public static final String DEFAULT_ENTERPRISE_TYPE = "个体工商户";

    public static final String DEFAULT_SALES_CHANNEL = "其他";

    public static final String[] ENTERPRISE_TYPE_OPTIONS = {
            "个体工商户", "个人独资企业", "有限责任公司", "合伙企业", "其他"
    };

    public static final String[] SALES_CHANNEL_OPTIONS = {
            "其他", "线上销售", "线下集市", "企事业单位", "商超门店"
    };

    private static final Set<String> ENTERPRISE_TYPE_SET = new HashSet<>(Arrays.asList(ENTERPRISE_TYPE_OPTIONS));

    private static final Set<String> SALES_CHANNEL_SET = new HashSet<>(Arrays.asList(SALES_CHANNEL_OPTIONS));

    private FamilyRanchValidation()
    {
    }

    public static void validateRowForSave(BizFamilyRanch row, TownDeptSupport townDeptSupport)
    {
        if (row == null)
        {
            throw new ServiceException("参数不能为空");
        }
        YakHerdInventoryMonthSupport.validateStatPeriod(row.getStatYear(), row.getStatMonth());
        if (row.getVillageDeptId() == null)
        {
            throw new ServiceException("所属村不能为空");
        }
        if (StringUtils.isEmpty(row.getEnterpriseName()))
        {
            throw new ServiceException("企业名称不能为空");
        }
        row.setEnterpriseName(row.getEnterpriseName().trim());
        if (row.getEnterpriseName().length() > 200)
        {
            throw new ServiceException("企业名称不能超过200字");
        }
        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());

        if (StringUtils.isNotEmpty(row.getLegalRepresentative()) && row.getLegalRepresentative().length() > 64)
        {
            throw new ServiceException("法定代表人不能超过64字");
        }
        if (StringUtils.isNotEmpty(row.getEstablishDate()) && row.getEstablishDate().length() > 32)
        {
            throw new ServiceException("成立时间不能超过32字");
        }

        if (StringUtils.isEmpty(row.getEnterpriseType()))
        {
            row.setEnterpriseType(DEFAULT_ENTERPRISE_TYPE);
        }
        else
        {
            String type = row.getEnterpriseType().trim();
            if (!ENTERPRISE_TYPE_SET.contains(type))
            {
                throw new ServiceException("企业类型取值不合法");
            }
            row.setEnterpriseType(type);
        }

        if (StringUtils.isEmpty(row.getSalesChannel()))
        {
            row.setSalesChannel(DEFAULT_SALES_CHANNEL);
        }
        else
        {
            String channel = row.getSalesChannel().trim();
            if (!SALES_CHANNEL_SET.contains(channel))
            {
                throw new ServiceException("产品销售渠道取值不合法");
            }
            row.setSalesChannel(channel);
        }

        row.setHasSubsidy(FamilyRanchDerivedSupport.normalizeYesNo(row.getHasSubsidy(), "是否享受补助"));
        row.setHasTrademark(FamilyRanchDerivedSupport.normalizeYesNo(row.getHasTrademark(), "是否有注册商标"));
        row.setHasQualityCert(FamilyRanchDerivedSupport.normalizeYesNo(row.getHasQualityCert(), "是否通过农产品质量认证"));

        validateNonNegative(row.getLivestockLaborCount(), "从事畜牧业生产经营劳力人员");
        validateNonNegative(row.getCattleYearEndStock(), "牛年底存栏");
        validateNonNegative(row.getCattleBreedingFemaleStock(), "牛能繁母畜");
        validateNonNegative(row.getSheepYearEndStock(), "羊年底存栏");
        validateNonNegative(row.getSheepBreedingFemaleStock(), "羊能繁母畜");
        validateNonNegative(row.getCattleOutbound(), "牛出栏");
        validateNonNegative(row.getSheepOutbound(), "羊出栏");
        validateNonNegative(row.getDrivenPopulation(), "带动人数");
        validateNonNegative(row.getSubsidyYear(), "补助年份");

        validateNonNegativeDecimal(row.getRegisteredCapital(), "注册资本");
        validateNonNegativeDecimal(row.getGrassBalanceArea(), "草畜平衡面积");
        validateNonNegativeDecimal(row.getTransferGrassArea(), "流转经营草地面积");
        validateNonNegativeDecimal(row.getApprovedCarryingCapacity(), "核定年末草畜平衡载畜量");
        validateNonNegativeDecimal(row.getYearEndStockSheepUnit(), "年底牲畜存栏绵羊单位");
        validateNonNegativeDecimal(row.getIncome(), "年度经营总收入");
        validateNonNegativeDecimal(row.getExpense(), "年度经营总支出");
        validateNonNegativeDecimal(row.getSubsidyAmount(), "补贴资金");
        validateNonNegativeDecimal(row.getWagePaid(), "发放工资");

        FamilyRanchDerivedSupport.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 + "不能为负数");
        }
    }
}
