package com.shkj.web.core.config;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;

/**
 * @description: 数据源配置类
 */
@Configuration
public class DataSourceConfig {

//    /**
//     *  1、bean的名称必须为minidaoDataSource，否则不生效
//     *  2、jeecg.minidao-datasource对应的是yml中的jeecg下的minidao-datasource，可自定义
//     */
//    @Bean(name="minidaoDataSource")
//    @ConfigurationProperties(prefix = "jeecg.minidao-datasource")
//    public DataSource dataSource(){
//        return DataSourceBuilder.create().build();
//    }

    public static void main(String[] args) throws Exception {

        String inputFilePath = "G:\\1_wms\\db\\base_goods.xlsx";
        int rowBegin = 1;
        FileInputStream fileInput = new FileInputStream(inputFilePath);//创建文件输入流
        XSSFWorkbook wb = new XSSFWorkbook(fileInput);//由输入流文件得到工作簿对象
        XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
        int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
        StringBuffer buffer = new StringBuffer();
        sheet.getSheetName();
        buffer.append("CREATE TABLE `base_package` (");
        //rowBegin代表要开始读取的行号，下面这个循环的作用是读取每一行内容
        for (int i = rowBegin; i <= lastRowNum; ++i) {
            XSSFRow row = sheet.getRow(i);//获取每一行
            int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号，即总列数


            for (int j = 0; j < columnNum; ++j) {
                XSSFCell cell = row.getCell(j);//获取每个单元格
                if (j > 1) {
                    if (row.getCell(j - 1).getStringCellValue().equals("id")) {
                        buffer.append("`" + row.getCell(j - 1).getStringCellValue() + "` " + cell.getStringCellValue()
                                + "  NOT NULL AUTO_INCREMENT COMMENT '主键',");
                        break;
                    }
                    buffer.append("`" + row.getCell(j - 1).getStringCellValue() + "` " + cell.getStringCellValue()
                            + "  DEFAULT NULL COMMENT '" + row.getCell(j + 6).getStringCellValue() + "',");
                    break;
                }
            }
        }
        buffer.append("  PRIMARY KEY (`id`) USING BTREE )");
        System.out.println(buffer.toString());
        wb.close();
        fileInput.close();

    }

}
