package com.fdkankan.ucenter.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.fdkankan.fyun.face.FYunFileServiceInterface;
import com.fdkankan.ucenter.entity.Invoice;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;


public class MyExcelUtil {

    //根据模板导出excel
    public static String excelTemplteToPdf(String localPath, String templateName, Object obj) {
        String fileName =  UUID.randomUUID().toString().replaceAll("-","") + ".xlsx";

        InputStream inputStream = MyExcelUtil.class.getClassLoader().getResourceAsStream("template/"+templateName+ ".xlsx");

        ExcelWriter excelWriter = null;

        String filePath = localPath+File.separator + fileName;
        if(!new File(filePath).getParentFile().exists()){
            new File(filePath).getParentFile().mkdirs();
        }
        excelWriter = EasyExcel.write(filePath)
                .withTemplate(inputStream)
                .autoCloseStream(true)
                .build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        if(excelWriter != null && obj != null){
            excelWriter.fill(obj, fillConfig, writeSheet);
        }
        assert excelWriter != null;
        excelWriter.finish();
        TestForExcel2PDF.excelToPdf(filePath,filePath.replace(".xlsx",".pdf"));
        return filePath.replace(".xlsx",".pdf");
    }

    public static void main(String[] args) {
        Invoice invoice = new Invoice();
        invoice.setInvoiceNumber("11111111111");
        MyExcelUtil.excelTemplteToPdf("D:\\abc\\1212\\111","invoiceTemplate",invoice);
    }

}
