package com.izouma.codegenerator;

import com.izouma.awesomeadmin.model.GenCode;
import com.izouma.awesomeadmin.model.TableField;
import org.apache.commons.text.CaseUtils;

import java.util.ArrayList;
import java.util.List;

public class GeneratorTool {

    /**
     * 类型转换和引入相关类
     *
     * @param model
     * @return
     */
    public static List<String> getImports(GenCode model) {
        List<String> imports = new ArrayList<>();
        for (TableField field : model.getFields()) {

            field.setModelName(CaseUtils.toCamelCase(field.getName(), false, '_'));
            field.setSqlType(JdbcType2sqlType(field.getJdbcType()));
            switch (field.getJdbcType()) {
                case "bit":
                    field.setJavaType("Boolean");
                    break;
                case "tinyint":
                    field.setJavaType("byte");
                    break;
                case "smallint":
                    field.setJavaType("short");
                    break;
                case "int":
                    field.setJavaType("Integer");
                    break;
                case "bigint":
                    field.setJavaType("Long");
                    break;
                case "float":
                case "double":
                    field.setJavaType("Double");
                    break;
                case "decimal":
                case "numeric":
                    field.setJavaType("BigDecimal");
                    imports.add("import java.math.BigDecimal;");
                    break;
                case "char":
                case "varchar":
                case "text":
                case "longtext":
                    field.setJavaType("String");
                    break;
                case "timestamp":
                case "datetime":
                case "date":
                    field.setJavaType("Date");
                    break;

                default:
                    field.setJavaType("String");
            }
        }
        return imports;
    }


    public static String JdbcType2sqlType(String jdbcType) {

        if (jdbcType.equalsIgnoreCase("int")) {
            return "INTEGER";
        } else if (jdbcType.equalsIgnoreCase("datetime")) {
            return "TIMESTAMP";
        } else if (jdbcType.equalsIgnoreCase("nvarchar")) {
            return "VARCHAR";
        }
        return jdbcType.toUpperCase();
    }

    public static List removeDuplicated(List list) {
        List newList = new ArrayList();
        for (Object obj : list) {
            if (!newList.contains(obj)) {
                newList.add(obj);
            }
        }
        return newList;
    }


    public static String SqlServerJdbcType2Mysql(String jdbcType) {

        if (jdbcType.equalsIgnoreCase("int identity")) {
            return "int";
        } else if (jdbcType.equalsIgnoreCase("nvarchar")) {
            return "varchar";
        } else if (jdbcType.equalsIgnoreCase("ntext")) {
            return "text";
        } else if (jdbcType.equalsIgnoreCase("nchar")) {
            return "char";
        }
        return jdbcType;
    }

    public static void main(String[] args) {

    }


}
