package com.app.util.file;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

@Slf4j
@Component
public class ExcelUtil {

    /**
     * 功能描述：
     *
     * @param filePath
     * @param fileName
     * @param sheetName
     * @param titleList
     * @param excelData
     */
    public void createExcel(String filePath, String fileName, String sheetName, List<String> titleList, List<List<String>> excelData, HttpServletRequest request, HttpServletResponse response) throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet xssfSheet = createSheet(workbook, sheetName);
        createTitle(workbook, xssfSheet, titleList);
        createData(xssfSheet, excelData);
        createFile(filePath, fileName, workbook);
    }

    /**
     * 功能描述：
     *
     * @param workbook
     * @param sheetName
     * @return
     */
    private XSSFSheet createSheet(XSSFWorkbook workbook, String sheetName) {
        return workbook.createSheet(sheetName);
    }

    /**
     * 功能描述：
     *
     * @param workbook
     * @param sheet
     * @param titleList todo 需要封装为列对象、列名称、列属性。这样比较通用。或者找找有没有类官方的第三方包
     */
    private void createTitle(XSSFWorkbook workbook, XSSFSheet sheet, List<String> titleList) {
        XSSFRow row = sheet.createRow(0);

        /* 设置列宽，setColumnWidth的第二个参数要乘以256，这个参数的单位是1/256个字符宽度 */
        sheet.setColumnWidth(0, 18 * 256);
        sheet.setColumnWidth(1, 18 * 256);
        sheet.setColumnWidth(2, 18 * 256);
        sheet.setColumnWidth(3, 18 * 256);
        sheet.setColumnWidth(4, 18 * 256);
        sheet.setColumnWidth(5, 18 * 256);
        sheet.setColumnWidth(6, 18 * 256);
        sheet.setColumnWidth(7, 18 * 256);
        sheet.setColumnWidth(8, 18 * 256);
        sheet.setColumnWidth(9, 18 * 256);
        sheet.setColumnWidth(10, 24 * 256);
        sheet.setColumnWidth(11, 32 * 256);
        sheet.setColumnWidth(12, 32 * 256);
        sheet.setColumnWidth(13, 32 * 256);
        sheet.setColumnWidth(14, 32 * 256);
        sheet.setColumnWidth(15, 32 * 256);
        sheet.setColumnWidth(16, 32 * 256);

        /* 设置为居中加粗 */
        XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 14); // 字体高度
        font.setFontName(" 黑体 "); // 字体
        font.setBold(true); // 加粗
        XSSFCellStyle style = workbook.createCellStyle();

        /* 居中 */
        style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中

        /* 设置背景 */
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setFillForegroundColor(IndexedColors.YELLOW.index);

        /* 加边框 */
        style.setBorderBottom(BorderStyle.THIN); // 下边框
        style.setBorderLeft(BorderStyle.THIN); // 左边框
        style.setBorderRight(BorderStyle.THIN); // 右边框
        style.setBorderTop(BorderStyle.THIN); // 上边框

        style.setFont(font);

        /* 生成列头 */
        XSSFCell cell;
        for (int i = 0; i < titleList.size(); i++) {
            cell = row.createCell(i);
            cell.setCellValue(titleList.get(i));
            cell.setCellStyle(style);
        }
    }

    /**
     * 功能描述：
     *
     * @param sheet
     * @param excelData
     */
    private void createData(XSSFSheet sheet, List<List<String>> excelData) {
        for (int rowIndex = 0; rowIndex < excelData.size(); rowIndex++) {
            Integer rowNum = rowIndex + 1; // 第一行为标题行

            XSSFRow row = sheet.createRow(rowNum);

            for (int cellIndex = 0; cellIndex < excelData.get(rowIndex).size(); cellIndex++) {
                XSSFCell cell = row.createCell(cellIndex);
                cell.setCellValue(excelData.get(rowIndex).get(cellIndex));
            }

        }
    }

    /**
     * 功能描述：
     *
     * @param filePath
     * @param fileName
     * @param workbook
     * @throws Exception
     */
    private void createFile(String filePath, String fileName, XSSFWorkbook workbook) throws Exception {
        File downloadPathFolder = new File(filePath);
        if (!downloadPathFolder.exists()) {
            downloadPathFolder.mkdirs();
        }

        FileOutputStream fileOutputStream = new FileOutputStream(filePath + File.separator + fileName);
        workbook.write(fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

}
