package com.gyee.frame.util.excel;

import com.gyee.frame.util.task.TaskTemplateCallable;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * EXCEL导出工具
 */
public class ExcelExportUtils {

    private static Logger logger = LoggerFactory.getLogger(ExcelExportUtils.class);

    /**
     *
     * @param voList 数据
     * @param sheetName sheet名称
     * @param titles 单元格表头
     * @param path 导出路径
     * @param fileName 导出文件名
     * @return
     * @throws Exception
     */
    public static boolean exelcExportTem(Map<String, List<Object>> voList, String sheetName, List<String> titles, String path, String fileName) throws Exception {

        boolean check = checkExist(path, fileName + ".xls");
        if (check) {
            //获取d://test.xls,建立数据的输入通道
            FileInputStream in = new FileInputStream(path + fileName + ".xls");
            //使用POI提供的方法得到excel的信息
            POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in);
            //得到文档对象
            HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem);
            //根据name获取sheet表
            HSSFSheet sheet = workbook.getSheet(sheetName);
            if (null == sheet) {
                sheet = workbook.createSheet(sheetName);
                HSSFRow row = sheet.createRow(0);
                //创建单元格，设置表头
                HSSFCell cell = row.createCell(0);
                for (int i = 1; i <= titles.size(); i++) {
                    cell.setCellValue(titles.get(i - 1));
                    cell = row.createCell(i);
                }
            }
            FileOutputStream out = new FileOutputStream(path + fileName + ".xls");  //向d://test.xls中写数据
            for (int i = 0; i < voList.get("F1").size(); i++) {
                HSSFRow row = sheet.createRow((short) (sheet.getLastRowNum() + 1)); //对总行数减1，就是倒数行数加数据
                //创建单元格设值
                switch (titles.size()) {
                    case 2:
                        row.createCell(0).setCellValue(voList.get("F1").get(i).toString());
                        row.createCell(1).setCellValue(Double.valueOf(voList.get("F2").get(i).toString()));
                        break;
                    case 5:
                        row.createCell(0).setCellValue(voList.get("F1").get(i).toString());
                        if (Double.valueOf(voList.get("F3").get(i).toString())<=0){
                            row.createCell(1).setCellValue(TaskTemplateCallable.balance());
                         }else {
                            row.createCell(1).setCellValue(Double.valueOf(voList.get("F2").get(i).toString()));
                        }
                        row.createCell(2).setCellValue(Double.valueOf(voList.get("F3").get(i).toString()));
                        row.createCell(3).setCellValue(Double.valueOf(voList.get("F4").get(i).toString()));
                        row.createCell(4).setCellValue(Double.valueOf(voList.get("F5").get(i).toString()));
                        break;
                    case 6:
                        row.createCell(0).setCellValue(voList.get("F1").get(i).toString());
                        row.createCell(1).setCellValue(Double.valueOf(voList.get("F2").get(i).toString()));
                        row.createCell(2).setCellValue(Double.valueOf(voList.get("F3").get(i).toString()));
                        row.createCell(3).setCellValue(Double.valueOf(voList.get("F4").get(i).toString()));
                        row.createCell(4).setCellValue(Double.valueOf(voList.get("F5").get(i).toString()));
                        row.createCell(5).setCellValue(Double.valueOf(voList.get("F6").get(i).toString()));
                        break;
                    default:
                        break;
                }
            }

            try {
                workbook.write(out);
                out.flush();
                logger.info(fileName + "-文件写入成功");
                return true;
            } catch (Exception e) {
                logger.error("文件写入失败", e);
                return false;
            } finally {
                if (null != out) {
                    out.close();
                }
                if (null != workbook) {
                    workbook.close();
                }
            }

        } else {

            //创建一个workbook对应一个excel文件
            HSSFWorkbook workbook = new HSSFWorkbook();
            //在workbook中创建一个sheet对应excel中的sheet
            HSSFSheet sheet = workbook.createSheet(sheetName);
            //在sheet表中添加表头第0行，老版本的poi对sheet的行列有限制
            HSSFRow row0 = sheet.createRow(0);
            //创建单元格，设置表头
            HSSFCell cell = row0.createCell(0);
            for (int i = 1; i <= titles.size(); i++) {
                cell.setCellValue(titles.get(i - 1));
                cell = row0.createCell(i);
            }
            //写入数据
            for (int i = 0; i < voList.get("F1").size(); i++) {
                HSSFRow row1 = sheet.createRow(i + 1);
                //创建单元格设值
                switch (titles.size()) {
                    case 2:
                        row1.createCell(0).setCellValue(voList.get("F1").get(i).toString());
                        row1.createCell(1).setCellValue(Double.valueOf(voList.get("F2").get(i).toString()));
                        break;
                    case 5:
                        row1.createCell(0).setCellValue(voList.get("F1").get(i).toString());
                        if (Double.valueOf(voList.get("F3").get(i).toString())<=0){
                            row1.createCell(1).setCellValue(TaskTemplateCallable.balance());
                        }else {
                            row1.createCell(1).setCellValue(Double.valueOf(voList.get("F2").get(i).toString()));
                        }
                        row1.createCell(2).setCellValue(Double.valueOf(voList.get("F3").get(i).toString()));
                        row1.createCell(3).setCellValue(Double.valueOf(voList.get("F4").get(i).toString()));
                        row1.createCell(4).setCellValue(Double.valueOf(voList.get("F5").get(i).toString()));
                        break;
                    case 6:
                        row1.createCell(0).setCellValue(voList.get("F1").get(i).toString());
                        row1.createCell(1).setCellValue(Double.valueOf(voList.get("F2").get(i).toString()));
                        row1.createCell(2).setCellValue(Double.valueOf(voList.get("F3").get(i).toString()));
                        row1.createCell(3).setCellValue(Double.valueOf(voList.get("F4").get(i).toString()));
                        row1.createCell(4).setCellValue(Double.valueOf(voList.get("F5").get(i).toString()));
                        row1.createCell(5).setCellValue(Double.valueOf(voList.get("F6").get(i).toString()));
                        break;
                    default:
                        break;
                }
            }
            //将文件保存到指定的位置

            FileOutputStream out = null;
            try {
                out = new FileOutputStream(path + fileName + ".xls");
                workbook.write(out);
                out.flush();
                logger.info(fileName + "-文件写入成功");
                return true;
            } catch (Exception e) {
                logger.error("文件写入失败", e);
                return false;
            } finally {
                if (null != out) {
                    out.close();
                }
            }
        }

    }


    /**
     * 判断文件及目录是否存在，若目录不存在则创建目录,文件不存 在返回false
     *
     * @param filepath
     * @param fileName
     * @return tag
     * @throws Exception
     */
    public static boolean checkExist(String filepath, String fileName) throws Exception {

        boolean tag = false;
        File path = new File(filepath);
        File name = new File(filepath + fileName);
        //判断目录是否存在
        if (path.exists()) {
            //判断文件是否存在
            if (name.exists()) {
                tag = true;
            }
        } else {
            //创建目录
            path.mkdirs();
            //检查目录是否存在
            if (name.exists()) {
                tag = true;
            }
        }
        return tag;
    }

}
