package com.bonus.canteen.core.utils.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.bonus.common.core.exception.ServiceException;
import org.springframework.web.multipart.MultipartFile;
import com.bonus.canteen.core.common.config.excel.ExcelListener;

import java.util.List;

public class EasyExcelUtil {
   public static <T> List<T> readSingleExcel(MultipartFile excel, T t, int headRowNumber) throws Exception {
      String filename = excel.getOriginalFilename();
      if (filename != null && (filename.toLowerCase().endsWith(".xls") || filename.toLowerCase().endsWith(".xlsx"))) {
         try {
            return ((ExcelReaderSheetBuilder)EasyExcel.read(excel.getInputStream(), t.getClass(), new ExcelListener()).sheet().headRowNumber(headRowNumber)).doReadSync();
         } catch (ExcelDataConvertException var8) {
            int rowIndex = var8.getRowIndex();
            CellData<?> cellData = var8.getCellData();
            String stringValue = cellData.getStringValue();
            throw new ServiceException("第" + rowIndex + "行【" + stringValue + "】数据格式错误！");
         } catch (Exception var9) {
            throw new ServiceException("文件读取失败,请检查重试！"+ var9.getMessage());
         }
      } else {
         throw new ServiceException("文件格式错误！");
      }
   }
}
