package com.gz.utils;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

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

/**
 * DTO生成工具类
 * @author LiuchangLan
 * @date 2020/6/28 10:32
 */
public class TableUtils {

    private static String yml;
    private static String url;
    private static String user;
    private static String password;
    private static String drive;

    static {
        // 读取yml
        yml = (String) getYml("spring.profiles.active");
        if (yml != null && !"".equals(yml)) {
            yml = "-" + yml;
        } else {
            yml = "";
        }
        url = String.valueOf(getYml("spring.datasource.url"));
        user = String.valueOf(getYml("spring.datasource.username"));
        password = String.valueOf(getYml("spring.datasource.password"));
        drive = String.valueOf(getYml("spring.datasource.driver-class-name"));

        //1.加载驱动程序
        try {
            Class.forName(drive);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        // 跳过固定字段
        List<String> cloumns = new ArrayList<String>(){{
            add("create_time");
            add("update_time");
            add("created");
            add("updated");
            add("deleted");
            add("id");
        }};
        Connection connection = DriverManager.getConnection(url, user , password);
        Scanner scanner = new Scanner(System.in);
        String tableName = scanner("表名");
        String tableSql = "select COLUMN_NAME,column_comment,data_type  from INFORMATION_SCHEMA.Columns where table_name='" + tableName + "'  ";
        PreparedStatement ps = connection.prepareStatement(tableSql);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()){
            String clumn = resultSet.getString("COLUMN_NAME");
            if(cloumns.indexOf(clumn) != -1){
                continue;
            }
            String comment = resultSet.getString("column_comment");
            String type = "";
            switch (resultSet.getString("data_type")){
                case "varchar":
                    type = "String";
                    break;
                case "datetime":
                    type = "String";
                    break;
                case "date":
                    type = "String";
                    break;
                case "decimal":
                    type = "Double";
                    break;
                case "int":
                    type = "Integer";
                    break;
            }
            int index = clumn.indexOf("_");
            while (index != -1){
                clumn = clumn.substring(0,index) + clumn.substring(index + 1,index + 2).toUpperCase() + clumn.substring(index + 2);
                index = clumn.indexOf("_");
            }
            System.out.println("// " + comment);
            System.out.println("private " + type + " " + clumn + ";");
            System.out.println();
        }
    }


    /**
     * 根据传入的yml获取yml中key的属性值
     *
     * @param key
     * @param yml
     * @return
     */
    public static Object getYml(Object key, String yml) {
        Resource resource = new ClassPathResource(yml);
        Properties properties;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties = yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return properties.get(key);
    }

    /**
     * 自动根据 application中激活的配置文件,选择对应的yml读取属性
     *
     * @param key
     * @return
     */
    public static Object getYml(Object key) {
        Object value = getYml(key, "application.yml");
        if (value != null) {
            return value;
        }
        if (!yml.contains("application")) {
            yml = "application" + yml + ".yml";
        }
        return getYml(key, yml);
    }


    /**
     * 读取控制台输入
     * @author WangJixin
     * @since 2019/9/24 11:53
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            return scanner.next();
        }
        throw new RuntimeException("输入有误");
    }

}
