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

import cn.superdesk.uniorder.common.DateJsonValueProcessor;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

import java.math.BigDecimal;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Description: 此Utils类达一两万数据就不行了
 * @Author: linjinyu
 * @CreateDate: 7/20/20 4:25 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 7/20/20 4:25 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class ExcelExportModePoiHssfUtils {

	/**
	 * 导出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> HSSFWorkbook createSheetByWorkbook(List<T> dataList, String[] fieldZhCn, String[] field,
	                                                     HSSFWorkbook workbook, String sheetName) throws Exception {

		//HSSFWorkbook     workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(sheetName);

		return getWorkbook(dataList, fieldZhCn, field, workbook, sheet);
	}

	/**
	 * 合成
	 *
	 * @param dataList
	 * @param fieldZhCn
	 * @param field
	 * @param workbook
	 * @param sheet
	 * @param <T>
	 * @return
	 */
	private static <T> HSSFWorkbook getWorkbook(List<T> dataList, String[] fieldZhCn, String[] field, HSSFWorkbook workbook, HSSFSheet sheet) {

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

		// 设置标题字体
		Font fontTitle = workbook.createFont();
		fontTitle.setFontHeightInPoints((short) 11); //字体大小
		//fontTitle.setColor();; //字体颜色
		fontTitle.setFontName("宋体"); //字体
		//fontTitle.setBold(true); //粗体显示
		//font.setItalic(true); //是否使用斜体
		//font.setStrikeout(true); //是否使用划线


		/****************************************** 设置表头 start ******************************************/

		// 设置标题单元格类型
		CellStyle cellStyleTitle = workbook.createCellStyle();
		cellStyleTitle.setFont(fontTitle);
		cellStyleTitle.setFillForegroundColor(IndexedColors.LIME.getIndex());
		//cellStyleTitle.setFillPattern(FillPatternType);
		cellStyleTitle.setAlignment(HorizontalAlignment.CENTER); //水平布局:居中
		cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
		//cellStyleTitle.setWrapText(true);//设置自动换行
		cellStyleTitle.setBorderBottom(BorderStyle.THIN); //下边框
		cellStyleTitle.setBorderLeft(BorderStyle.THIN);//左边框
		cellStyleTitle.setBorderTop(BorderStyle.THIN);//上边框
		cellStyleTitle.setBorderRight(BorderStyle.THIN);//右边框
		cellStyleTitle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyleTitle.setRightBorderColor(IndexedColors.BLACK.getIndex());

		HSSFRow rowtitle = sheet.createRow(0);
		rowtitle.setHeightInPoints(20);

		for (int i = 0; i < fieldZhCn.length; i++) {
			String             item = fieldZhCn[i];
			HSSFCell           cell = rowtitle.createCell(i);
			HSSFRichTextString text = new HSSFRichTextString(item);
			cell.setCellValue(text);
			cell.setCellStyle(cellStyleTitle);
		}

		/****************************************** 设置内容 start ******************************************/

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

			HSSFRow row = sheet.createRow(i + 1);

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

			JSONObject obj = JSONObject.fromObject(dataList.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);
				}

				cell.setCellStyle(cellStyleTitle);
			}
		}
		for (int j = 0; j < field.length; j++) {
			sheet.autoSizeColumn((short) j);
		}
		return workbook;
	}

}
