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

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.math.BigDecimal;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.prosperity.ProsperityAchievementTestSamples;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityAchievement;

/**
 * GTFY-CG-UT-003～008
 */
@DisplayName("CommonProsperityAchievementValidation")
class CommonProsperityAchievementValidationTest
{

    @Test
    @DisplayName("GTFY-CG-UT-003 集体经济收入")
    void ut003_collectiveIncome()
    {
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateCollectiveEconomyIncome(new BigDecimal("-1")));
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateCollectiveEconomyIncome(new BigDecimal("1.234")));
        assertDoesNotThrow(() -> CommonProsperityAchievementValidation.validateCollectiveEconomyIncome(BigDecimal.ZERO));
        assertDoesNotThrow(
                () -> CommonProsperityAchievementValidation.validateCollectiveEconomyIncome(new BigDecimal("0.01")));
        assertDoesNotThrow(() -> CommonProsperityAchievementValidation
                .validateCollectiveEconomyIncome(new BigDecimal("999999.99")));
    }

    @Test
    @DisplayName("GTFY-CG-UT-004 整数项非负")
    void ut004_integerFields()
    {
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateNonNegativeInteger(-1, "带动就业人数"));
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateNonNegativeInteger(null, "带动就业人数"));
        assertDoesNotThrow(
                () -> CommonProsperityAchievementValidation.validateNonNegativeInteger(0, "带动就业人数"));
        assertDoesNotThrow(
                () -> CommonProsperityAchievementValidation.validateNonNegativeInteger(100, "带动就业人数"));
    }

    @Test
    @DisplayName("GTFY-CG-UT-005 绿化面积")
    void ut005_greenArea()
    {
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateNewGreenArea(new BigDecimal("-0.1")));
        assertDoesNotThrow(() -> CommonProsperityAchievementValidation.validateNewGreenArea(BigDecimal.ZERO));
        assertDoesNotThrow(
                () -> CommonProsperityAchievementValidation.validateNewGreenArea(new BigDecimal("12.5")));
    }

    @Test
    @DisplayName("GTFY-CG-UT-006 数字平台使用率")
    void ut006_digitalRate()
    {
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateRate(new BigDecimal("-0.01"), "数字平台使用率"));
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateRate(new BigDecimal("1.01"), "数字平台使用率"));
        assertDoesNotThrow(
                () -> CommonProsperityAchievementValidation.validateRate(BigDecimal.ZERO, "数字平台使用率"));
        assertDoesNotThrow(
                () -> CommonProsperityAchievementValidation.validateRate(new BigDecimal("0.85"), "数字平台使用率"));
        assertDoesNotThrow(
                () -> CommonProsperityAchievementValidation.validateRate(BigDecimal.ONE, "数字平台使用率"));
    }

    @Test
    @DisplayName("GTFY-CG-UT-007 品牌产品溢价率")
    void ut007_brandRate()
    {
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateRate(new BigDecimal("-0.1"), "品牌产品溢价率"));
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateRate(new BigDecimal("2"), "品牌产品溢价率"));
        assertDoesNotThrow(
                () -> CommonProsperityAchievementValidation.validateRate(new BigDecimal("0.5"), "品牌产品溢价率"));
    }

    @Test
    @DisplayName("GTFY-CG-UT-008 必填项非空")
    void ut008_required()
    {
        BizCommonProsperityAchievement noPeriod = sample();
        noPeriod.setStatPeriod(null);
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateRowForSave(noPeriod));
        BizCommonProsperityAchievement noCount = sample();
        noCount.setEmploymentDrivenCount(null);
        assertThrows(ServiceException.class,
                () -> CommonProsperityAchievementValidation.validateRowForSave(noCount));
    }

    private static BizCommonProsperityAchievement sample()
    {
        return ProsperityAchievementTestSamples.sample();
    }
}
