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

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

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.industryservice.support.FarmerHouseholdExcelParser.ParsedRow;

@DisplayName("FarmerHouseholdExcelParser")
class FarmerHouseholdExcelParserTest
{
    @Test
    @DisplayName("识别表头并解析模板中的 2 条示例数据行")
    void parseTemplateSampleRows() throws Exception
    {
        try (InputStream in = getClass().getClassLoader()
                .getResourceAsStream(FarmerHouseholdExcelParser.TEMPLATE_CLASSPATH))
        {
            assertNotNull(in, "classpath 模板不存在");
            List<ParsedRow> rows = FarmerHouseholdExcelParser.parse(in);
            assertEquals(2, rows.size());

            ParsedRow first = rows.get(0);
            assertEquals("雅安镇", first.getTownName());
            assertEquals("雅安镇荣嘎村", first.getVillageName());
            assertEquals(165, first.getHouseholdCount());
            assertEquals(1155, first.getPopulationCount());
            assertEquals(0, new BigDecimal("1151.32").compareTo(first.getGrazingBanArea()));
            assertEquals(0, new BigDecimal("8151.32").compareTo(first.getGrassBalanceArea()));
            assertEquals(0, new BigDecimal("1000").compareTo(first.getApprovedCarryingCapacity()));
            assertEquals(0, new BigDecimal("980").compareTo(first.getYearEndStockSheepUnit()));
            assertEquals("是", first.getIsPureHerder());
            assertEquals("否", first.getIsNoLivestock());
            assertEquals(0, new BigDecimal("8000").compareTo(first.getGrazingBanSubsidy()));
            assertEquals(0, new BigDecimal("8900").compareTo(first.getGrassBalanceReward()));
            assertEquals(0, new BigDecimal("1000").compareTo(first.getAfterCapFloorSubsidy()));
            assertEquals(0, new BigDecimal("200").compareTo(first.getVillageSupervisorSubsidy()));

            ParsedRow second = rows.get(1);
            assertEquals("雅安镇", second.getTownName());
            assertEquals("雅安镇公随村", second.getVillageName());
            assertEquals(136, second.getHouseholdCount());
            assertEquals("是", second.getIsPureHerder());
        }
    }

    @Test
    @DisplayName("识别表头行且 HEADER_SPAN=3")
    void detectHeaderRow() throws Exception
    {
        try (InputStream in = getClass().getClassLoader()
                .getResourceAsStream(FarmerHouseholdExcelParser.TEMPLATE_CLASSPATH);
                org.apache.poi.ss.usermodel.Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(in))
        {
            org.apache.poi.ss.usermodel.Sheet sheet = FarmerHouseholdExcelParser.resolveSheet(wb);
            int header = FarmerHouseholdExcelParser.detectHeaderRow(sheet);
            assertEquals(3, header);
            assertEquals(3, FarmerHouseholdExcelParser.HEADER_SPAN);
        }
    }
}
