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

@DisplayName("YakOutboundReportExcelParser")
class YakOutboundReportExcelParserTest
{
    @Test
    @DisplayName("ZCZX-MYCNCL-UT-007 识别表头并解析村行")
    void ut007_parseTemplate() throws Exception
    {
        try (InputStream in = getClass().getResourceAsStream("/excel/yak_outbound_report_2026.xlsx"))
        {
            List<ParsedRow> rows = YakOutboundReportExcelParser.parse(in);
            assertEquals(2, rows.size());
            assertEquals("雅安镇", rows.get(0).getTownName());
            assertEquals("雅安镇荣嘎村", rows.get(0).getVillageName());
            assertEquals(120, rows.get(0).getFarmerHouseholdCount());
            assertEquals(822, rows.get(0).getFarmerPopulationCount());
            assertEquals(5670, rows.get(0).getYakOutboundCount());
            assertEquals(1677, rows.get(0).getSelfConsumptionCattleCount());
        }
    }

    @Test
    @DisplayName("ZCZX-MYCNCL-UT-008 识别表头行")
    void ut008_detectHeaderRow() throws Exception
    {
        try (InputStream in = getClass().getResourceAsStream("/excel/yak_outbound_report_2026.xlsx"))
        {
            org.apache.poi.ss.usermodel.Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(in);
            org.apache.poi.ss.usermodel.Sheet sheet = YakOutboundReportExcelParser.resolveSheet(wb);
            int header = YakOutboundReportExcelParser.detectHeaderRow(sheet);
            assertEquals(3, header);
            wb.close();
        }
    }

    @Test
    @DisplayName("ZCZX-MYCNCL-UT-011 跳过合计行")
    void ut011_noTotalRow() throws Exception
    {
        try (InputStream in = getClass().getResourceAsStream("/excel/yak_outbound_report_2026.xlsx"))
        {
            List<ParsedRow> rows = YakOutboundReportExcelParser.parse(in);
            assertTrue(rows.stream().noneMatch(r -> r.getTownName() != null && r.getTownName().contains("合计")));
        }
    }
}
