package com.ai.da.common.utils;

import lombok.Data;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Data
public class ExcelReader {
    public static List<List<String>> readExcel(String filePath) throws IOException {
        List<List<String>> data = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            int numberOfColumns = sheet.getRow(0).getLastCellNum();

            for (int i = 0; i < numberOfColumns; i++) {
                List<String> columnData = new ArrayList<>();
                for (Row row : sheet) {
                    Cell cell = row.getCell(i);
                    if (cell != null) {
                        switch (cell.getCellType()) {
                            case STRING:
                                columnData.add(cell.getStringCellValue());
                                break;
                            case NUMERIC:
                                columnData.add(String.valueOf(cell.getNumericCellValue()));
                                break;
                            case BOOLEAN:
                                columnData.add(String.valueOf(cell.getBooleanCellValue()));
                                break;
                            default:
                                // 跳过空单元格或其他类型（比如错误类型）
                                break;
                        }
                    }
                }
                // 只有当这一列有数据时，才添加到结果列表中
                if (!columnData.isEmpty()) {
                    data.add(columnData);
                }
            }
        }
        return data;
    }
}
