package com.elephant.framework.galaxy.aroad.trafficbasic.utils;

import com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeBridge;
import com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeHighWay;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 解析excel获取单元格的值封装成javabean
 * @Param:
 * @return:
 * @Author: dongjili
 * @Date:
 */
public class ExcelUtil_fanshe {
    public static FileInputStream fw;
    public static XSSFWorkbook wb;
    public static XSSFSheet sheet;

    /**
     * @Description: 根据桥梁明细表路径获取桥梁值的集合
     * @Param:
     * @return: List<LuKeBridge> luKeBridges
     * @Author: dongjili
     * @Date:
     */
    public static List<LuKeBridge> readPathBridge(String path) throws Exception {
        //要想把excel中的每一行数据转换成javabean对象则用反射技术
//javabean中的属性个数要与excel中的列数一样，不然可能报错,属性顺序与列的顺序也要一样
        fw = new FileInputStream(new File(path));
        // 读取的文件
        //（1）判断文件后缀名是xls，还是xlsx
        //（2）如果是xls，使用HSSFWorkbook；如果是xlsx，使用XSSFWorkbook
        wb = new XSSFWorkbook(fw);
        XSSFRow row = null;
        List<LuKeBridge> luKeBridges = new ArrayList<LuKeBridge>();
        //循环excel中所有的 sheet
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            sheet = wb.getSheetAt(i);
            //循环每一个sheet中的每一行
            for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                //LuKeHighWay luKeHighWay = new LuKeHighWay();
                //获取该类在堆区的Class对象
                Class<?> clazz = Class.forName("com.pinxiang.domain.LuKeBridge");
                //获取私有的构造方法
                Constructor<?> constructor = clazz.getDeclaredConstructor();
                //暴力破解
                constructor.setAccessible(true);
                //实例化对象
                LuKeBridge luKeBridge = (LuKeBridge) constructor.newInstance();
                //获取javabean的各个属性
                Field[] beanFiled = clazz.getDeclaredFields();
                //循环每一行中的所有列,就是得到单元格中的数据
                for (int z = 0; z < row.getLastCellNum(); z++) {
                    try {
                        //强制反射,让private 的属性也可以访问
                        beanFiled[z].setAccessible(true);
                        //把得到的属性进行赋值，就是把读取到的单元格中的数据赋给对应的属性
                        //CREATE_NULL_AS_BLANK 如何为空就创建一个空串
                        if (beanFiled[z].getType().equals(String.class)) {
                            beanFiled[z].set(luKeBridge, ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK)));
                        } else if (beanFiled[z].getType().equals(Integer.class)) {
                            beanFiled[z].set(luKeBridge, Integer.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled[z].getType().equals(Long.class.getName())) {
                            beanFiled[z].set(luKeBridge, Long.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled[z].getType().equals(Float.class)) {
                            beanFiled[z].set(luKeBridge, Float.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled[z].getType().equals(Double.class)) {
                            beanFiled[z].set(luKeBridge, Double.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled[z].getType().equals(Byte.class)) {
                            beanFiled[z].set(luKeBridge, Byte.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled[z].getType().equals(Boolean.class)) {
                            beanFiled[z].set(luKeBridge, Boolean.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled[z].getType().equals(Date.class)) {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = null;
                            try {
                                date = sdf.parse(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK)));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            beanFiled[z].set(luKeBridge, date);
                        } else {
                            beanFiled[z].set(luKeBridge, ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK)));
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                luKeBridges.add(luKeBridge);
            }
        }
        return luKeBridges;

    }

    /**
     * @Description: 根据路段明细表路径获取路段值的集合
     * @Param:
     * @return: List<LuKeHighWay> luKeHighWays
     * @Author: dongjili
     * @Date:
     */
    public static List<LuKeHighWay> readPathHighWay(String path) throws Exception {
//要想把excel中的每一行数据转换成javabean对象则用反射技术
//javabean中的属性个数要与excel中的列数一样，不然可能报错,属性顺序与列的顺序也要一样
        fw = new FileInputStream(new File(path));
        // 读取的文件
        //（1）判断文件后缀名是xls，还是xlsx
        //（2）如果是xls，使用HSSFWorkbook；如果是xlsx，使用XSSFWorkbook
        wb = new XSSFWorkbook(fw);
        XSSFRow row = null;
        List<LuKeHighWay> luKeHighWays = new ArrayList<LuKeHighWay>();
        //循环excel中所有的 sheet
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            sheet = wb.getSheetAt(i);
            //循环每一个sheet中的每一行
            for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                //LuKeHighWay luKeHighWay = new LuKeHighWay();
                Class<?> clazz = Class.forName("com.pinxiang.domain.LuKeHighWay");
                Constructor<?> constructor = clazz.getDeclaredConstructor();
                //暴力破解
                constructor.setAccessible(true);
                LuKeHighWay luKeHighWay = (LuKeHighWay) constructor.newInstance();
                Field[] beanFiled2 = clazz.getDeclaredFields();
                //循环每一行中的所有列,就是得到单元格中的数据
                for (int z = 0; z < row.getLastCellNum(); z++) {
                    try {
                        //强制反射,让private 的属性也可以访问
                        beanFiled2[z].setAccessible(true);
                        //把得到的属性进行赋值，就是把读取到的单元格中的数据赋给对应的属性
                       /* Row.MissingCellPolicy RETURN_NULL_AND_BLANK
                        Row.MissingCellPolicy RETURN_BLANK_AS_NULL
                        Row.MissingCellPolicy CREATE_NULL_AS_BLANK*/
                        if (beanFiled2[z].getType().equals(String.class)) {
                            beanFiled2[z].set(luKeHighWay, ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK)));
                        } else if (beanFiled2[z].getType().equals(Integer.class)) {
                            beanFiled2[z].set(luKeHighWay, Integer.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled2[z].getType().equals(Long.class.getName())) {
                            beanFiled2[z].set(luKeHighWay, Long.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled2[z].getType().equals(Float.class)) {
                            beanFiled2[z].set(luKeHighWay, Float.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled2[z].getType().equals(Double.class)) {
                            beanFiled2[z].set(luKeHighWay, Double.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled2[z].getType().equals(Byte.class)) {
                            beanFiled2[z].set(luKeHighWay, Byte.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled2[z].getType().equals(Boolean.class)) {
                            beanFiled2[z].set(luKeHighWay, Boolean.valueOf(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK))));
                        } else if (beanFiled2[z].getType().equals(Date.class)) {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = null;
                            try {
                                date = sdf.parse(ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK)));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            beanFiled2[z].set(luKeHighWay, date);
                        } else {
                            beanFiled2[z].set(luKeHighWay, ExcelUtil_fanshe.getCellValue(row.getCell(z, Row.CREATE_NULL_AS_BLANK)));
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                luKeHighWays.add(luKeHighWay);
            }
        }
        return luKeHighWays;

    }

    /**
     * @Description: 传入单元格值转换成string字符串输出
     * @Param:
     * @return: string类型的单元格值
     * @Date:
     */
    private static String getCellValue(Cell cell) {
        Object result = "";
        if (cell != null) {
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING://字符串
                    result = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC://数字
                    if (DateUtil.isCellDateFormatted(cell)) {
                        Date theDate = cell.getDateCellValue();
                        SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        result = dff.format(theDate);
                    } else {
                        //DecimalFormat数字格式化,获取单元格中真正的数据类型
                        DecimalFormat df = new DecimalFormat("#.###########");
                        result = df.format(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN://boolean
                    result = cell.getBooleanCellValue();
                    break;
                case Cell.CELL_TYPE_FORMULA://公式
                    result = cell.getCellFormula();
                    break;
                case Cell.CELL_TYPE_ERROR://故障
                    result = cell.getErrorCellValue();
                    break;
                case Cell.CELL_TYPE_BLANK://空值
                    break;
                default:
                    break;
            }
        }
        return result.toString();
    }

}
