package com.sy.coinage.core.util;

import java.io.File;

import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

/**
 * -操作文件类
 *
 * @author: lug 2022年5月12日
 */
public class FileUtil {

    /**
     * -创建文件
     *
     * @param defPath 根路径
     * @param defUrl 文件夹名
     * @param name 文件名
     * @return
     */
    public static File createFile(String defPath, String defUrl, String name) {
        File path = new File(defPath + defUrl);
        if (!path.exists()) {
            path.mkdirs();
        }
        File file = new File(path + "/" + name);
        if (file.exists()) {
            file.delete();
        }
        return file;
    }


    /**
     * - 头样式 带背景色
     *
     * @param wb
     * @return
     */
    public static XSSFCellStyle cellStyleHead(XSSFWorkbook wb) {
        XSSFCellStyle style = wb.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());// 设置单元格背景色
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置单元格背景色
        style.setBorderBottom(BorderStyle.THIN); // 下边框
        style.setBorderLeft(BorderStyle.THIN);// 左边框
        style.setBorderTop(BorderStyle.THIN);// 上边框
        style.setBorderRight(BorderStyle.THIN);// 右边框
        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 创建垂直居中格式
        return style;
    }

    /**
     * -标题样式
     *
     * @param wb
     * @return
     */
    public static XSSFCellStyle cellStyleTitle(XSSFWorkbook wb, int size) {
        // 设置cell边框
        XSSFCellStyle style = wb.createCellStyle(); // 样式对象
        XSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) size);
        style.setFont(font);
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 创建垂直居中格式
        return style;
    }

    /**
     * -内容样式 无背景色
     *
     * @param wb
     * @return
     */
    public static XSSFCellStyle cellStyle(XSSFWorkbook wb) {
        // 设置cell边框
        XSSFCellStyle style = wb.createCellStyle(); // 样式对象
        style.setBorderBottom(BorderStyle.THIN); // 下边框
        style.setBorderLeft(BorderStyle.THIN);// 左边框
        style.setBorderTop(BorderStyle.THIN);// 上边框
        style.setBorderRight(BorderStyle.THIN);// 右边框
        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 创建垂直居中格式
        return style;
    }

    public static XSSFCellStyle cellStyleRed(XSSFWorkbook wb) {
        // 设置cell边框
        XSSFCellStyle style = wb.createCellStyle(); // 样式对象
        style.setBorderBottom(BorderStyle.THIN); // 下边框
        style.setBorderLeft(BorderStyle.THIN);// 左边框
        style.setBorderTop(BorderStyle.THIN);// 上边框
        style.setBorderRight(BorderStyle.THIN);// 右边框
        style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 创建垂直居中格式
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.RED.getIndex());
        font.setFontName("宋体");
        style.setFont(font);
        return style;
    }

    public static String getFileContent(MultipartFile file) {
        StringBuffer sb = new StringBuffer();
        try (InputStream is = file.getInputStream()) {
            byte[] buffer = new byte[1024];
            while (is.read(buffer, 0, 1024) != -1) {
                sb.append(new String(buffer));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}

