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

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

import java.io.ByteArrayInputStream;
import java.util.Collections;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.industryservice.domain.BizLargeLivestockFarmer;

@DisplayName("LargeLivestockFarmerExcelExporter")
class LargeLivestockFarmerExcelExporterTest
{
    private final LargeLivestockFarmerExcelExporter exporter = new LargeLivestockFarmerExcelExporter();

    @Test
    @DisplayName("导出填表时间默认为系统当月，且标题统计月与填表月可不同")
    void exportFillerDateDefaultsToCurrentMonth() throws Exception
    {
        java.time.LocalDate today = java.time.LocalDate.now();
        String expectedFill = today.getYear() + "年" + today.getMonthValue() + "月";
        int exportMonth = today.getMonthValue() == 1 ? 12 : today.getMonthValue() - 1;
        int exportYear = today.getMonthValue() == 1 ? today.getYear() - 1 : today.getYear();

        BizLargeLivestockFarmer row = new BizLargeLivestockFarmer();
        row.setTownName("雅安镇");
        row.setVillageDeptId(201L);
        row.setVillageName("雅安镇荣嘎村");
        row.setEnterpriseName("测试大户");

        byte[] bytes = exporter.export(exportYear, exportMonth, Collections.singletonList(row), "测试用户");
        try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
        {
            Sheet sheet = LargeLivestockFarmerExcelParser.resolveSheet(wb);
            Row fillerRow = sheet.getRow(1);
            assertNotNull(fillerRow);
            String fillerText = LargeLivestockFarmerExcelParser.cellString(fillerRow, 0);
            assertNotNull(fillerText);
            assertTrue(fillerText.contains("测试用户"), fillerText);
            assertTrue(fillerText.contains(expectedFill), "填表时间应为当月: " + fillerText);
            assertTrue(!fillerText.contains("X月"), fillerText);
            String title = LargeLivestockFarmerExcelParser.cellString(sheet.getRow(0), 0);
            assertTrue(title.contains(exportYear + "年"), title);
            assertTrue(title.contains("（" + exportMonth + "月）"), title);
        }
    }
}
