package com.nantian.cloud.dbcapp.tool;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.excel.EasyExcel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ExcelWriteUtil {

    /**
     * excel列表导出
     *
     * @param response
     * @param data
     * @param fileName
     * @return
     */
    public static boolean fileEasyExport(HttpServletResponse response, List<?> data,Class type, String fileName, String title) {
        OutputStream out = null;
        try {
            // 构建导出文件名
            String exportFileName = new String((fileName).getBytes("UTF-8"), "ISO-8859-1") + ".xlsx";
            // 构建响应header
            response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
            // 下载到浏览器默认地址
            out = response.getOutputStream();
            if (null != data && data.size() > 0) {
                EasyExcel.write(out,type).sheet(title).doWrite(data);
            }else {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            try {
                out.flush();
                out.close();
                //此处记得关闭输出Servlet流
                IoUtil.close(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /***
     * 根据用户设置的条件，导出并下载
     * @param contentName 夸行的标题
     * @param dataList 数据
     * @param headerAliasList 表头
     * @param response 下载
     */
    public static void excelWrite(String contentName, List<?> dataList, List<HeaderAliasVo> headerAliasList, HttpServletResponse response){
        List<Map<Object, Object>> rows = new LinkedList<>();

        dataList.forEach(t->{
            Map<Object,Object> map = new HashMap<>();
            headerAliasList.forEach(h->{
                map.put(h.getCode(), ReflectUtil.getFieldValue(t,h.getCode()));
            });
            rows.add(map);
        });

        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(true);
        // // 合并单元格后的标题行，使用默认标题样式
        //writer.merge(headerAliasList.size()-1, contentName);
        headerAliasList.forEach(t->writer.addHeaderAlias(t.getCode(),t.getName()));
        // 一次性写出内容，使用默认样式
        writer.write(rows, true);
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        response.setHeader("Content-Disposition","attachment;filename="+ contentName +".xlsx");
        ServletOutputStream out= null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

}
