package com.scm.contract.tools;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

import java.util.Date;
import java.util.Objects;


/**
 * @author zuo
 * @date 2023/4/19 14:30
 */

public class QcExcelUtil {

    public static CellStyle getStyle(Workbook wb) {
        //创建样式对象
        CellStyle style = wb.createCellStyle();
        //设置样式对齐方式：水平垂直居中
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setFillForegroundColor((short) 22);
        Font font = wb.createFont();
        font.setFontName("等线");
        font.setBold(true);
        style.setFont(font);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }

    public static CellStyle getStyle1(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        //设置样式对齐方式：水平垂直居中
        style.setAlignment(HorizontalAlignment.RIGHT);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setFillForegroundColor((short) 22);
        Font font = wb.createFont();
        font.setBold(true);
        style.setFont(font);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }

    public static CellStyle getStyle2(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        //设置样式对齐方式：水平垂直居中
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }

    public static CellStyle getStyle3(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        //设置样式对齐方式：水平垂直居中
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        DataFormat format = wb.createDataFormat();
        style.setDataFormat(format.getFormat("yyyy/mm/dd hh:mm:ss"));
        return style;
    }

    public static CellStyle getStyle4(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        //设置样式对齐方式：水平垂直居中
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setWrapText(true);
        return style;
    }

    public static void setCellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol, Sheet sh) {
        CellRangeAddress cellAddresses = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
        //为合并单元格添加边框
        RegionUtil.setBorderTop(BorderStyle.THIN, cellAddresses, sh);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellAddresses, sh);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellAddresses, sh);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellAddresses, sh);
        sh.addMergedRegion(cellAddresses);
    }

    public static void setCell(Row row, int column, Float value, CellStyle style) {
        Cell cell = row.createCell(column);
        if (Objects.isNull(value)) {
            cell.setCellValue("/");
        } else {
            cell.setCellValue(Double.parseDouble(value.toString()));
        }
        cell.setCellStyle(style);
    }

    public static void setCell(Row row, int column, Double value, CellStyle style) {
        Cell cell = row.createCell(column);
        if (Objects.isNull(value)) {
            cell.setCellValue("/");
        } else {
            cell.setCellValue(value);
        }
        cell.setCellStyle(style);
    }

    public static void setCell(Row row, int column, String value, CellStyle style) {
        Cell cell = row.createCell(column);
        cell.setCellValue(StringUtils.isBlank(value) ? "" : value);
        cell.setCellStyle(style);
    }

    public static void setCell(Row row, int column, int value, CellStyle style) {
        Cell cell = row.createCell(column);
        cell.setCellValue(value);
        cell.setCellStyle(style);
    }

    public static void setCell(Row row, int column, Date value, CellStyle style) {
        Cell cell = row.createCell(column);
        cell.setCellValue(value);
        cell.setCellStyle(style);
    }
}
