package cn.superdesk.uniorder.common.excel;

import cn.superdesk.uniorder.common.excel.utils.ExcelExportModePoiHssfUtils;
import cn.superdesk.uniorder.common.numerical.AnDateHelper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

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

/**
 * 三种类的接口及方法:
 * <p>
 * HSSF:HSSFWorkbook、HSSFSheet、HSSFRow、HSSFCell……
 * XSSF:XSSFWorkbook、XSSFSheet、XSSFRow、XSSFCell……
 * SXSSF:SXSSFWorkbook、Sheet、Row、Cell……
 */

/**
 * @Description: 未完成
 * @Author: linjinyu
 * @CreateDate: 2021/2/3 上午9:55
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/2/3 上午9:55
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class ExcelResponse {

	/**
	 * 较为通用的
	 *
	 * @param dataList
	 * @param fieldZhCn
	 * @param field
	 * @param sheetName
	 * @param fileName
	 * @param response
	 * @param <T>
	 */
	public static <T> void out(List<T> dataList, String[] fieldZhCn, String[] field, String sheetName, String fileName, HttpServletResponse response) throws Exception {

		// 合成 Excel
		HSSFWorkbook workbook = ExcelExportModePoiHssfUtils.createWorkbook(dataList, fieldZhCn, field, sheetName);

		// 准备将Excel的输出流通过response输出到页面下载
		response.reset();
		// 二进制输出流
		response.setContentType("application/octet-stream");
		response.setCharacterEncoding("UTF-8");

		// 设置导出Excel的名称
		response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + addTail() + ".xls");
		// 刷新缓冲
		response.flushBuffer();

		// workbook将Excel写入到response的输出流中,供页面下载
		workbook.write(response.getOutputStream());

	}

	public static void out(HSSFWorkbook workbook, String fileName, HttpServletResponse response) throws Exception {

		// getWriter() has already been called for this response // 通过reset的处理办法不优 再考量

		// 准备将Excel的输出流通过response输出到页面下载
		response.reset();
		// 二进制输出流
		response.setContentType("application/octet-stream");
		// 这后面可以设置导出Excel的名称
		response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + addTail() + ".xls");
		// 刷新缓冲
		response.flushBuffer();

		// workbook将Excel写入到response的输出流中,供页面下载
		workbook.write(response.getOutputStream());
	}

	public static String addTail() {

		// AnDateHelper.getCurrentDateStr("yyyyMMddHHmm") // 202103021503
		// AnDateHelper.getCurrentDateTimeStr()           // 2021-03-02 14/56/36
		return "_createAt_" + AnDateHelper.getCurrentDateStr("yyyyMMddHHmm");
	}

//	public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) {
//		ExportParams exportParams = new ExportParams(title, sheetName);
//		exportParams.setCreateHeadRows(isCreateHeader);
//		defaultExport(list, pojoClass, fileName, response, exportParams);
//
//	}
//
//	public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) {
//		defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
//	}
//
//	public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
//		defaultExport(list, fileName, response);
//	}
//
//	private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
//		Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
//		if (workbook != null) ;
//		downLoadExcel(fileName, response, workbook);
//	}
//
//	private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
//		try {
//			response.setCharacterEncoding("UTF-8");
//			response.setHeader("content-Type", "application/vnd.ms-excel");
//			response.setHeader("Content-Disposition",
//					"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//			workbook.write(response.getOutputStream());
//		} catch (IOException e) {
//			throw new NormalException(e.getMessage());
//		}
//	}
//
//	private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
//		Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
//		if (workbook != null) ;
//		downLoadExcel(fileName, response, workbook);
//	}


}
