package com.bootdo.datas.tools;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.datas.handler.WaterMarkHandler;
import com.bootdo.datas.tools.gm.FileCryptTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @date 2022/3/16 11:39
 * @author wangp
 * @description
 */
public class ExcelUtils {
    private static Logger log = LoggerFactory.getLogger(ExcelUtils.class);

    private final static String excelName = "备案数据专家审核信息";

    private static String sheetName = "备案数据表";

    /**
     * @param response
     * @param clazz
     * @param list
     * @Description: 导出Excel(07版.xlsx)到web
     * @Author: wangp
     * @Date: 2022/3/16 11:45
     * @Return: void
     */
    public static void export(HttpServletResponse response, Class clazz, List list, String fileName) throws Exception {
        if (StringUtils.isBlank(fileName)) {
            fileName = excelName;
        }
        OutputStream os = null;
        try {
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码
            String excelNames = URLEncoder.encode(fileName, "UTF-8") + "-" + DateUtils.getTimeMS();
            response.setHeader("Content-disposition",
                    "attachment;filename=" + excelNames + ExcelTypeEnum.XLSX.getValue());
            os = response.getOutputStream();
            EasyExcel.write(os, clazz).inMemory(true) // 注意，此项配置不能少
                .registerWriteHandler(new WaterMarkHandler(sheetName)).sheet(sheetName)
                .doWrite(list);
        } catch (IOException e) {
            log.error("export error:", e);
        } finally {
            os.flush();
            os.close();
            response.flushBuffer();
        }

    }


    /**
     * @description:  加密导出
     * @param:
     * @return: void
     * @author xhl
     * @date: 2022/9/6 18:20
     */
    public static void encryptExport(HttpServletResponse response, Class clazz, List list,String crtPrival, String fileName,String encName) throws Exception {
        OutputStream os = null;
        String excelNames = "数据备案加密导出信息";
        try {
            EasyExcel.write(fileName, clazz).inMemory(true) // 注意，此项配置不能少
                    .registerWriteHandler(new WaterMarkHandler(sheetName)).sheet(sheetName)
                    .doWrite(list);
            //文件加密
            FileCryptTool.encryptFile(crtPrival, fileName, encName);
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码
            excelNames = URLEncoder.encode(excelNames, "UTF-8") + "-" + DateUtils.getTimeMS();
            response.setHeader("Content-disposition",
                    "attachment;filename=" + excelNames + ExcelTypeEnum.XLSX.getValue());
            os = response.getOutputStream();
            File file = new File(encName);
            InputStream is = new FileInputStream(file);
            byte[] bytes = new byte[2048];
            int len;
            while ((len = is.read(bytes))>0){
                os.write(bytes,0,len);
            }
        } catch (Exception e) {
            log.error("export error:", e);
        } finally {
            os.flush();
            os.close();
            FileUtil.deleteFile(fileName);
            FileUtil.deleteFile(encName);
            response.flushBuffer();
        }

    }

}
