package cn.superdesk.uniorder.common.excel.utils;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.DateJsonValueProcessor;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 理论上100M都是可以的 基于 List<Bean> 来导出 要指定 key -> lable
 * @Author: linjinyu
 * @CreateDate: 7/20/20 4:25 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 7/20/20 4:25 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class ExcelExportModeEasyExcelUtils {


	/**
	 * 导出EXCEL
	 *
	 * @param dataList  model数组
	 * @param fieldZhCn 中文列名集合
	 * @param field     列名集合
	 * @param sheetName 表名
	 * @return
	 * @throws Exception
	 */
//	public static <T> HSSFWorkbook createWorkbook(List<T> dataList, String[] fieldZhCn, String[] field, String sheetName) throws Exception {
//
//
//		HSSFWorkbook workbook = new HSSFWorkbook();
//		HSSFSheet    sheet    = workbook.createSheet(sheetName);
//
//		return getWorkbook(dataList, fieldZhCn, field, workbook, sheet);
//	}


	/**
	 * 在workbook 基础上 增加一个 sheet
	 *
	 * @param dataList
	 * @param fieldZhCn
	 * @param field
	 * @param workbook
	 * @param sheetName
	 * @param <T>
	 * @return
	 * @throws Exception
	 */
	public static <T> void createSheetByWorkbook(List<T> dataList, String[] fieldZhCn, String[] field, ExcelWriter excelWriter, Integer sheetNo, String sheetName) throws Exception {


		// 创建一个sheet

		/****************************************** 设置表头 start ******************************************/
		WriteSheet writeSheet = EasyExcel
				.writerSheet(sheetNo)
				.sheetName(sheetName)
				.head(getHead(fieldZhCn))
				.build();

		/****************************************** 设置内容 start ******************************************/
		excelWriter.write(getData(dataList, field), writeSheet);


	}


	private static List<List<String>> getHead(String[] headArray) {

		log.info("getHead input {}", AnJsonUtils.toJson(headArray));

		List<List<String>> head = new ArrayList<>();

		List<String> itemList = Arrays.asList(headArray);

		itemList.forEach(item -> {
			head.add(Arrays.asList(item));
		});

		log.info("getHead output {}", AnJsonUtils.toJson(head));

		return head;

	}

	/**
	 * 合成
	 *
	 * @param inDataList
	 * @param field
	 * @param <T>
	 * @return
	 */
	private static <T> List<List<Object>> getData(List<T> inDataList, String[] field) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

		List<List<Object>> outDataList = new ArrayList<>();


		for (int i = 0; i < inDataList.size(); i++) {

			List<Object> row = new ArrayList<>();

			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));

			JSONObject obj = JSONObject.fromObject(inDataList.get(i), jsonConfig);

			for (int j = 0; j < field.length; j++) {

				Object param = "";

				if (StringUtils.isEmpty(field[j])) {
					param = "";
				} else {
					param = obj.get(field[j]);
				}
//
//				HSSFCell cell = row.createCell(j);
//				if (field[j].equals("createTime")) {
//
//					//System.out.println(param);
//				}
//				if (param instanceof Integer) {
//					int value = ((Integer) param).intValue();
//					cell.setCellType(CellType.NUMERIC);
//					cell.setCellValue(value);
//				} else if (param instanceof String) {
//					String s = (String) param;
//					cell.setCellType(CellType.STRING);
//					cell.setCellValue(s);
//				} else if (param instanceof Double) {
//					double d = ((Double) param).doubleValue();
//					cell.setCellType(CellType.NUMERIC);
//					cell.setCellValue(d);
//				} else if (param instanceof Float) {
//					float f = ((Float) param).floatValue();
//					cell.setCellType(CellType.NUMERIC);
//					cell.setCellValue(f);
//				} else if (param instanceof Long) {
//					long l = ((Long) param).longValue();
//					cell.setCellType(CellType.NUMERIC);
//					cell.setCellValue(l);
//				} else if (param instanceof Boolean) {
//					boolean b = ((Boolean) param).booleanValue();
//					cell.setCellType(CellType.BOOLEAN);
//					cell.setCellValue(b);
//				} else if (param instanceof Date) {
//					Date d = (Date) param;
//					cell.setCellType(CellType.STRING);
//					cell.setCellValue(sdf.format(d));
//				} else if (param instanceof BigDecimal) {
//					BigDecimal b = new BigDecimal(param.toString());
//					double     d = b.doubleValue();
//					cell.setCellType(CellType.NUMERIC);
//					cell.setCellValue(d);
//				}

				row.add(param);

//				cell.setCellStyle(cellStyleTitle);
			}

			outDataList.add(row);
		}


		return outDataList;
	}


}
