package com.izouma.codegenerator;

import com.izouma.awesomeadmin.model.GenCode;
import com.izouma.awesomeadmin.model.TableField;
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
import org.apache.commons.lang.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class ReadTableGenerator {

    public static void ReadTable(GenCode model) throws Exception {
        Connection conn = null;
        try {

            String[] colnames; // 列名数组
            String[] colTypes; //列名类型数组
            String[] colComments;//列名注释数组mybatis-generator-core

            int[] colSizes; //列名大小数组

            String PK_NAME = "id";//主键名

            //查要生成实体类的表
            String sql = "select * from " + model.getTableName();
            PreparedStatement pStemt = null;

            if ("Mysql".equals(model.getDataBaseType())) {
                Class.forName("com.mysql.jdbc.Driver");
                System.out.println("成功加载MySQL驱动！");

                PropertiesFileLoader propertiesFileLoader = PropertiesFileLoader.getInstance();
                propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.url");

                String url = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.url");
                String user = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.username");
                String password = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.password");
                conn = DriverManager.getConnection(url, user, password);

                System.out.println("成功连接到数据库！");
            } else if ("SqlServer".equals(model.getDataBaseType())) {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                System.out.println("成功加载SQLServer驱动！");

                PropertiesFileLoader propertiesFileLoader = PropertiesFileLoader.getInstance();
                propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.url");

                String url = propertiesFileLoader.getProperties("properties/jdbc.properties", "sqlserver.url");
                String user = propertiesFileLoader.getProperties("properties/jdbc.properties", "sqlserver.user");
                String password = propertiesFileLoader.getProperties("properties/jdbc.properties", "sqlserver.password");
                conn = DriverManager.getConnection(url, user, password);
            }


            pStemt = conn.prepareStatement(sql);

            ResultSetMetaData rsmd = pStemt.getMetaData();

            ResultSet pkRSet = conn.getMetaData().getPrimaryKeys(conn.getCatalog().toUpperCase(),
                    null, model.getTableName());

            while (pkRSet.next()) {
                System.out.println(pkRSet.getString("COLUMN_NAME"));
                PK_NAME = pkRSet.getString("COLUMN_NAME");
            }


            int size = rsmd.getColumnCount();   //统计列
            colComments = new String[size];
            colnames = new String[size];
            colTypes = new String[size];
            colSizes = new int[size];
            for (int i = 0; i < size; i++) {
                colnames[i] = rsmd.getColumnName(i + 1);
                colTypes[i] = rsmd.getColumnTypeName(i + 1);
                colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
            }


            if ("Mysql".equals(model.getDataBaseType())) {
                //获取注释
                ResultSet rs = pStemt.executeQuery("show full columns from " + model.getTableName());
                int j = 0;
                while (rs.next()) {
                    colComments[j] = (rs.getString("Comment"));
                    j++;
                }
            }
            List<TableField> tableFields = new ArrayList<>();


            for (int i = 0; i < colnames.length; i++) {
                TableField tableField = new TableField();

                tableField.setName(colnames[i]);
                tableField.setJdbcType(colTypes[i]);
                tableField.setNotNull(false);
                tableField.setPrimaryKey(PK_NAME.equals(colnames[i]) ? true : false);
                tableField.setAutoIncrease(PK_NAME.equals(colnames[i]) ? true : false);
                tableField.setRemark((StringUtils.isNotEmpty(colComments[i]) ? colComments[i] : colnames[i]));
                tableField.setShowInList(true);
                tableField.setShowInForm("ID".equalsIgnoreCase(colnames[i]) ? false : true);
                tableField.setFormType("singleLineText");

                tableFields.add(tableField);
            }

            model.setFields(tableFields);

            System.out.println("读取数据表成功");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            conn.close();
        }

    }

}

