package easydo.technology.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.DateUtils;
import easydo.technology.exception.BizException;
import org.apache.commons.codec.Charsets;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liy
 * @since 2022-03-31
 **/
public class ExcelUtil {
    private static DecimalFormat df1 = new DecimalFormat("0");
    private static DecimalFormat df2 = new DecimalFormat("0.00");

    public static <T> List<T> read(MultipartFile excel, int sheetNo, Class<T> clazz) {
        return read(excel, sheetNo, 1, clazz);
    }

    public static <T> List<T> read(MultipartFile excel, int sheetNo, int headRowNumber, Class<T> clazz) {
        DataListener<T> dataListener = new DataListener();
        ExcelReaderBuilder builder = getReaderBuilder(excel, dataListener, clazz);
        if (builder == null) {
            return null;
        } else {
            ((ExcelReaderSheetBuilder) builder.sheet(sheetNo).headRowNumber(headRowNumber)).doRead();
            return dataListener.getDataList();
        }
    }

    public static <T> ExcelReaderBuilder getReaderBuilder(MultipartFile excel, ReadListener<T> readListener, Class<T> clazz) {
        String filename = excel.getOriginalFilename();
        if (StringUtils.isEmpty(filename)) {
            throw new BizException("请上传文件!");
        } else if (!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx")) {
            throw new BizException("请上传正确的excel文件!");
        } else {
            try {
                InputStream inputStream = new BufferedInputStream(excel.getInputStream());
                return EasyExcel.read(inputStream, clazz, readListener);
            } catch (IOException var6) {
                var6.printStackTrace();
                return null;
            }
        }
    }

    public static <T> void export(HttpServletResponse response, List<T> dataList, Class<T> clazz) throws IOException {
        try {
            export(response, DateUtils.format(new Date(), "yyyyMMddHHmmss"), "导出数据", dataList, clazz);
        } catch (Throwable var4) {
            throw var4;
        }
    }

    public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) throws IOException {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(dataList);
        } catch (Throwable var6) {
            throw var6;
        }
    }

    private static String getValue(XSSFCell xssfCell, boolean isString) {
        if (xssfCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(xssfCell.getBooleanCellValue()).trim();
        } else if (xssfCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            if (isString) {
                return df1.format(xssfCell.getNumericCellValue()).trim();
            } else {
                return df2.format(xssfCell.getNumericCellValue()).trim();
            }
        } else {
            return String.valueOf(xssfCell.getStringCellValue()).trim();
        }
    }

    private static String getValue(HSSFCell hssfCell, boolean isString) {
        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(hssfCell.getBooleanCellValue()).trim();
        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
            if (isString) {
                return df1.format(hssfCell.getNumericCellValue()).trim();
            } else {
                return df2.format(hssfCell.getNumericCellValue()).trim();
            }
        } else {
            return String.valueOf(hssfCell.getStringCellValue().trim());
        }
    }
}
  