package com.zbkj.common.utils.excel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;

import java.math.BigDecimal;
import java.util.*;

public class ExcelUtil {
    public static Log logger = LogFactory.getLog(ExcelUtil.class);

    /**
     * 生成excel
     * @param list
     * @param titles
     * @return
     */
    public static XSSFWorkbook exportDataList(List<List<?>> list, String[] titles, List<String> titles2, Integer beginRow, String sheeetName) {
        List<Map<String,Object>> titleList = new ArrayList<>();
        if (titles.length > 0) {
            Map<String, Object> map = null;
            for (String title : titles) {
                map = new HashMap<>();
                map.put("content", title);
                titleList.add(map);
            }
        }
        return exportDataList(list, titleList, titles2, beginRow, sheeetName);
    }

    /**
     * 生成excel
     * @param list
     * @param titles
     * @return
     */
    public static XSSFWorkbook exportDataList(List<List<?>> list, List<Map<String,Object>> titles, List<String> titles2, Integer beginRow, String sheeetName) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet(null == sheeetName ? "sheet1" : sheeetName);
        sheet.setColumnWidth(0,20*256);
        sheet.setColumnWidth(1,20*256);
        sheet.setColumnWidth(2,20*256);
        sheet.setColumnWidth(7,20*256);
        XSSFRow titleRow = sheet.createRow(0);

        XSSFCellStyle boderStyle = workbook.createCellStyle();
        boderStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        boderStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        boderStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        boderStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        boderStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        boderStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        boderStyle.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); //填充背景色
        boderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充模式
        int temp = 0;
        for (int i = 0; i < titles.size(); i++) {
            XSSFCell cell = titleRow.createCell(i);
            cell.setCellValue(titles.get(i).get("content").toString());
			/*XSSFCell cell = titleRow.createCell(temp);
			cell.setCellStyle(boderStyle);

			Map<String,Object> m = titles.get(i);
			cell.setCellValue(m.get("content").toString());

			Integer v1 = null == m.get("v1") ? 0 : Integer.parseInt(m.get("v1").toString()); //第一行
			Integer v2 = null == m.get("v2") ? 0 : Integer.parseInt(m.get("v2").toString()); //最后一行
			Integer v3 = null == m.get("v3") ? temp : Integer.parseInt(m.get("v3").toString()); //第一列
			Integer v4 = null == m.get("v4") ? temp : Integer.parseInt(m.get("v4").toString())+temp; //最后一列
			System.out.println(v1+"="+v2+"="+v3+"="+v4);
			temp=v4+1;

			sheet.autoSizeColumn((short) v3.intValue());
			sheet.addMergedRegion(new CellRangeAddress(v1,v2,v3,v4));*/
        }
        int contentBeginRow = 1;
        if(null != titles2){
            contentBeginRow++;
            XSSFRow titleRow2 = sheet.createRow(1);
            for(int i = 0; i < titles2.size(); i++){
                XSSFCell cell = titleRow2.createCell(beginRow++);
                cell.setCellStyle(boderStyle);
                cell.setCellValue(titles2.get(i));
            }
        }

        boderStyle = workbook.createCellStyle();
        boderStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        boderStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        boderStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        boderStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        boderStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        boderStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        for (int i = contentBeginRow; i < list.size() + contentBeginRow; i++) {
            XSSFRow row = sheet.createRow(i);
            List<?> datas = list.get(i - contentBeginRow);
            for (int j = 0; j < datas.size(); j++) {
                try {
                    XSSFCell cell = row.createCell(j);
                    cell.setCellStyle(boderStyle);
                    Object value = datas.get(j);
                    if (value != null) {
                        if (value instanceof Integer) {
                            cell.setCellValue(((Integer) value).doubleValue());
                        } else if (value instanceof Float) {
                            cell.setCellValue(((Float) value).doubleValue());
                        } else if (value instanceof Long) {
                            cell.setCellValue(((Long) value).doubleValue());
                        } else if (value instanceof Double) {
                            cell.setCellValue(((Double) value).doubleValue());
                        } else if (value instanceof Boolean) {
                            cell.setCellValue((Boolean) value);
                        } else if (value instanceof Date) {
                            cell.setCellValue((Date) value);
                        } else if (value instanceof String) {
                            cell.setCellValue((String) value);
                        } else if(value instanceof BigDecimal){
                            cell.setCellValue(value.toString());
                        }
                    }
                } catch (Exception e) {
                    logger.error(">>>>>系统异常>>>>>>", e);
                }
            }
        }
        return workbook;
    }

}
