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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.math.BigDecimal;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.industryservice.domain.BizDisposableIncome;

@DisplayName("DisposableIncomeGrowthSupport")
class DisposableIncomeGrowthSupportTest
{
    @Test
    @DisplayName("未填时按公式补齐增幅与增速")
    void fillMissingWhenEmpty()
    {
        BizDisposableIncome current = row(2020, "13346");
        BizDisposableIncome previous = row(2019, "11886");
        DisposableIncomeGrowthSupport.fillMissingGrowth(current, previous);
        assertEquals(0, new BigDecimal("1460").compareTo(current.getIncomeIncrease()));
        assertEquals(0, new BigDecimal("12.28").compareTo(current.getIncomeGrowthRate()));
    }

    @Test
    @DisplayName("已填写时保留手工值")
    void keepManualValues()
    {
        BizDisposableIncome current = row(2020, "13346");
        current.setIncomeIncrease(new BigDecimal("1000"));
        current.setIncomeGrowthRate(new BigDecimal("8.00"));
        BizDisposableIncome previous = row(2019, "11886");
        DisposableIncomeGrowthSupport.fillMissingGrowth(current, previous);
        assertEquals(new BigDecimal("1000"), current.getIncomeIncrease());
        assertEquals(new BigDecimal("8.00"), current.getIncomeGrowthRate());
    }

    @Test
    @DisplayName("仅缺增速时用已填增幅计算")
    void fillRateFromManualIncrease()
    {
        BizDisposableIncome current = row(2020, "13346");
        current.setIncomeIncrease(new BigDecimal("1460"));
        BizDisposableIncome previous = row(2019, "11886");
        DisposableIncomeGrowthSupport.fillMissingGrowth(current, previous);
        assertEquals(new BigDecimal("1460"), current.getIncomeIncrease());
        assertEquals(0, new BigDecimal("12.28").compareTo(current.getIncomeGrowthRate()));
    }

    @Test
    @DisplayName("无上年数据时不补齐")
    void noPreviousKeepsNull()
    {
        BizDisposableIncome current = row(2019, "11886");
        DisposableIncomeGrowthSupport.fillMissingGrowth(current, null);
        assertNull(current.getIncomeIncrease());
        assertNull(current.getIncomeGrowthRate());
    }

    @Test
    @DisplayName("上年可支配收入为0时仅补齐增幅")
    void previousZeroOnlyIncrease()
    {
        BizDisposableIncome current = row(2021, "100");
        BizDisposableIncome previous = row(2020, "0");
        DisposableIncomeGrowthSupport.fillMissingGrowth(current, previous);
        assertEquals(new BigDecimal("100"), current.getIncomeIncrease());
        assertNull(current.getIncomeGrowthRate());
    }

    @Test
    @DisplayName("强制重算会覆盖已有增幅增速")
    void recalculateOverwritesManual()
    {
        BizDisposableIncome current = row(2022, "13346");
        current.setIncomeIncrease(new BigDecimal("1"));
        current.setIncomeGrowthRate(new BigDecimal("1.00"));
        BizDisposableIncome previous = row(2021, "11886");
        DisposableIncomeGrowthSupport.recalculateGrowth(current, previous);
        assertEquals(0, new BigDecimal("1460").compareTo(current.getIncomeIncrease()));
        assertEquals(0, new BigDecimal("12.28").compareTo(current.getIncomeGrowthRate()));
    }

    private static BizDisposableIncome row(int year, String income)
    {
        BizDisposableIncome row = new BizDisposableIncome();
        row.setStatYear(year);
        row.setRuralDisposableIncome(new BigDecimal(income));
        return row;
    }
}
