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.FamilyRanchExcelParser.ParsedRow;

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

            ParsedRow first = rows.get(0);
            assertEquals("雅安镇", first.getTownName());
            assertEquals("恰沃卡村", first.getVillageName());
            assertEquals("那曲市巴青县仁琼家庭牧场", first.getEnterpriseName());
            assertEquals("布嘎", first.getLegalRepresentative());
            assertEquals("个体工商户", first.getEnterpriseType());
            assertEquals(0, new BigDecimal("50").compareTo(first.getRegisteredCapital()));
            assertEquals("2024-11-04", first.getEstablishDate());
            assertEquals(4, first.getLivestockLaborCount());
            assertEquals(116, first.getCattleYearEndStock());
            assertEquals(75, first.getCattleBreedingFemaleStock());
            assertEquals(0, first.getSheepYearEndStock());
            assertEquals(19, first.getCattleOutbound());
            assertEquals(0, new BigDecimal("8151.32").compareTo(first.getGrassBalanceArea()));
            assertEquals(0, new BigDecimal("624.9").compareTo(first.getApprovedCarryingCapacity()));
            assertEquals(0, new BigDecimal("411.8").compareTo(first.getYearEndStockSheepUnit()));
            assertEquals(0, new BigDecimal("10").compareTo(first.getIncome()));
            assertEquals(0, new BigDecimal("3.8").compareTo(first.getExpense()));
            assertEquals(2026, first.getSubsidyYear());
            assertEquals("是", first.getHasSubsidy());
            assertEquals("否", first.getHasTrademark());
            assertEquals("是", first.getHasQualityCert());
            assertEquals("其他", first.getSalesChannel());

            ParsedRow second = rows.get(1);
            assertEquals("雅安镇", second.getTownName());
            assertEquals("恰沃卡村", second.getVillageName());
            assertEquals("那曲市巴青县吉祥如意家庭牧场", second.getEnterpriseName());
            assertEquals("曲萨", second.getLegalRepresentative());
            assertEquals("2024-10-31", second.getEstablishDate());
            assertEquals(5, second.getLivestockLaborCount());
            assertEquals(102, second.getCattleYearEndStock());
            assertEquals(63, second.getCattleBreedingFemaleStock());
            assertEquals(7, second.getCattleOutbound());
            assertEquals(0, new BigDecimal("15").compareTo(second.getIncome()));
            assertEquals(0, new BigDecimal("5.4").compareTo(second.getExpense()));
        }
    }

    @Test
    @DisplayName("识别表头行（乡镇名称+村居名称）且 HEADER_SPAN=3")
    void detectHeaderRow() throws Exception
    {
        try (InputStream in = getClass().getClassLoader()
                .getResourceAsStream(FamilyRanchExcelParser.TEMPLATE_CLASSPATH);
                org.apache.poi.ss.usermodel.Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(in))
        {
            org.apache.poi.ss.usermodel.Sheet sheet = FamilyRanchExcelParser.resolveSheet(wb);
            int header = FamilyRanchExcelParser.detectHeaderRow(sheet);
            assertEquals(2, header);
            assertEquals(3, FamilyRanchExcelParser.HEADER_SPAN);
        }
    }
}
