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

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

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

@DisplayName("YakHerdInventoryExcelParser")
class YakHerdInventoryExcelParserTest
{
    @Test
    @DisplayName("ZCZX-MYCLTB-UT-007 识别表头并解析 10 乡镇")
    void ut007_parseTemplate() throws Exception
    {
        try (InputStream in = getClass().getResourceAsStream("/excel/yak_herd_inventory_2026.xlsx"))
        {
            List<ParsedRow> rows = YakHerdInventoryExcelParser.parse(in);
            assertEquals(10, rows.size());
            assertEquals(21687, rows.get(0).getYakTotal());
            assertEquals(13430, rows.get(0).getCowTotal());
            assertEquals(26, rows.get(0).getVillageCount());
        }
    }

    @Test
    @DisplayName("ZCZX-MYCLTB-UT-010 不含合计行")
    void ut010_noTotalRow() throws Exception
    {
        try (InputStream in = getClass().getResourceAsStream("/excel/yak_herd_inventory_2026.xlsx"))
        {
            List<ParsedRow> rows = YakHerdInventoryExcelParser.parse(in);
            assertTrue(rows.stream().noneMatch(r -> r.getTownName().contains("合计")));
        }
    }

    @Test
    @DisplayName("表头行检测")
    void detectHeaderRow() throws Exception
    {
        try (InputStream in = getClass().getResourceAsStream("/excel/yak_herd_inventory_2026.xlsx"))
        {
            org.apache.poi.ss.usermodel.Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(in);
            int header = YakHerdInventoryExcelParser.detectHeaderRow(wb.getSheetAt(0));
            assertEquals(2, header);
            wb.close();
        }
    }
}
