package com.ruoyi.cms.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  // 改为XSSFWorkbook

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelToObject {

	public static <T> List<T> readExcelToObjects(String filePath, Class<T> clazz) throws Exception {
		List<T> resultList = new ArrayList<>();

		try (FileInputStream fileInputStream = new FileInputStream(filePath);
			 Workbook workbook = new XSSFWorkbook(fileInputStream)) {  // 使用XSSFWorkbook处理 .xlsx 文件

			Sheet sheet = workbook.getSheetAt(0);
			Row headerRow = sheet.getRow(0);

			// 获取表头名称和列索引的映射
			Map<String, Integer> headerMap = new HashMap<>();
			for (Cell cell : headerRow) {
				headerMap.put(cell.getStringCellValue(), cell.getColumnIndex());
			}

			// 处理每一行数据
			for (int i = 1; i <= sheet.getLastRowNum(); i++) {
				Row dataRow = sheet.getRow(i);
				T obj = clazz.newInstance();

				for (Field field : clazz.getDeclaredFields()) {
					String fieldName = field.getName();
					if (headerMap.containsKey(fieldName)) {
						int columnIndex = headerMap.get(fieldName);
						Cell cell = dataRow.getCell(columnIndex);

						// 将表格的值赋给Java对象的字段
						field.setAccessible(true);
						setFieldValue(field, obj, cell);
					}
				}

				resultList.add(obj);
			}
		}

		return resultList;
	}

	private static void setFieldValue(Field field, Object obj, Cell cell) throws IllegalAccessException {
		if (cell == null) return;

		switch (cell.getCellType()) {
			case STRING:
				if (field.getType() == String.class) {
					field.set(obj, cell.getStringCellValue());
				}
				break;
			case NUMERIC:
				if (field.getType() == int.class || field.getType() == Integer.class) {
					field.set(obj, (int) cell.getNumericCellValue());
				} else if (field.getType() == double.class || field.getType() == Double.class) {
					field.set(obj, cell.getNumericCellValue());
				} else if (field.getType() == String.class) {
					// Special handling for phone numbers and other numeric fields as strings
					BigDecimal bigDecimalValue = new BigDecimal(cell.getNumericCellValue());
					field.set(obj, bigDecimalValue.toPlainString());
				}
				break;
			case BOOLEAN:
				if (field.getType() == boolean.class || field.getType() == Boolean.class) {
					field.set(obj, cell.getBooleanCellValue());
				}
				break;
			default:
				break;
		}
	}

//	public static void main(String[] args) {
//		try {
//			List<MyClass> objects = readExcelToObjects("path/to/your/excel.xlsx", MyClass.class);
//			objects.forEach(System.out::println);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}

//// 示例Java对象
//class MyClass {
//	private String name;
//	private int age;
//	private double salary;
//
//	// getter和setter方法
//
//	@Override
//	public String toString() {
//		return "MyClass{" +
//			"name='" + name + '\'' +
//			", age=" + age +
//			", salary=" + salary +
//			'}';
//	}
//}
