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

import java.math.BigDecimal;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityAchievement;

/**
 * 共同富裕成果入参校验。
 */
public final class CommonProsperityAchievementValidation
{
    private CommonProsperityAchievementValidation()
    {
    }

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

    public static void validateStatPeriod(String statPeriod)
    {
        if (StringUtils.isEmpty(statPeriod))
        {
            throw new ServiceException("统计范围不能为空");
        }
        if (!CommonProsperityAchievementRules.isStatPeriodFormatValid(statPeriod))
        {
            throw new ServiceException("统计范围格式须为YYYY-MM（如2026-05）");
        }
    }

    public static void validateCollectiveEconomyIncome(BigDecimal income)
    {
        validateDecimalNonNegative(income, "集体经济收入", 2);
    }

    public static void validateNewGreenArea(BigDecimal area)
    {
        validateDecimalNonNegative(area, "新增绿化面积", 2);
    }

    public static void validateNonNegativeInteger(Integer value, String fieldLabel)
    {
        if (value == null)
        {
            throw new ServiceException(fieldLabel + "不能为空");
        }
        if (value < 0)
        {
            throw new ServiceException(fieldLabel + "须为大于等于0的整数");
        }
    }

    public static void validateRate(BigDecimal rate, String fieldLabel)
    {
        if (rate == null)
        {
            throw new ServiceException(fieldLabel + "不能为空");
        }
        if (rate.compareTo(BigDecimal.ZERO) < 0 || rate.compareTo(BigDecimal.ONE) > 0)
        {
            throw new ServiceException(fieldLabel + "须在0～1之间");
        }
        if (rate.scale() > 4)
        {
            throw new ServiceException(fieldLabel + "最多保留4位小数");
        }
    }

    public static void validateRowForSave(BizCommonProsperityAchievement row)
    {
        if (row == null)
        {
            throw new ServiceException("参数不能为空");
        }
        trimFieldsForSave(row);
        validateStatPeriod(row.getStatPeriod());
        validateCollectiveEconomyIncome(row.getCollectiveEconomyIncome());
        validateNonNegativeInteger(row.getEmploymentDrivenCount(), "带动就业人数");
        validateNonNegativeInteger(row.getNewJobPositions(), "新增就业岗位");
        validateNonNegativeInteger(row.getEnvProjectCount(), "环境治理项目数");
        validateNewGreenArea(row.getNewGreenArea());
        validateNonNegativeInteger(row.getCulturalHeritageCount(), "文化传承人数");
        validateNonNegativeInteger(row.getEthnicIntegrationProjectCount(), "民族融合项目数");
        validateNonNegativeInteger(row.getEthnicUnityActivityCount(), "民族团结活动数");
        validateRate(row.getDigitalPlatformUsageRate(), "数字平台使用率");
        validateNonNegativeInteger(row.getRegionalPublicBrandCount(), "区域公共品牌数");
        validateRate(row.getBrandPremiumRate(), "品牌产品溢价率");
    }

    private static void validateDecimalNonNegative(BigDecimal value, String fieldLabel, int maxScale)
    {
        if (value == null)
        {
            throw new ServiceException(fieldLabel + "不能为空");
        }
        if (value.compareTo(BigDecimal.ZERO) < 0)
        {
            throw new ServiceException(fieldLabel + "须大于等于0");
        }
        if (value.scale() > maxScale)
        {
            throw new ServiceException(fieldLabel + "最多保留" + maxScale + "位小数");
        }
    }
}
