package com.tofly.codegen.util;

/**
 * @author tuonbed
 * @date 2020/3/13  9:21
 */
public class MySQLDataTypeSqlInfo implements DataTypeSqlInfo{


    String TABLEGEN_TABLENAME="tf_sms.TF_SMPN_TABLEGEN_X";

    String TABLEDETAILGEN_TABLENAME="tf_sms.TF_SMPN_TABLEDETAILGEN_X";

    private final String TABLE_COMMENT_MYSQL_SQL="SELECT\n" +
            "\tupper(table_name) TABLENAME,\n" +
            "\tTABLE_COMMENT COMMENTS,\n" +
            "\tdate_format(create_time,'%Y-%m-%d %H:%i:%s') CREATED,\n" +
            "\tdate_format(update_time,'%Y-%m-%d %H:%i:%s') LASTTIME\n" +
            "FROM\n" +
            "\tinformation_schema. TABLES\n" +
            "WHERE\n" +
            "\ttable_schema = (SELECT DATABASE())\n" +
            "\tand table_name = ? \n"+
            "order by lasttime desc";


    private final String TABLE_COMMENT_ALL_MYSQL_SQL="SELECT\n" +
            "\tupper(table_name) TABLENAME,\n" +
            "\tTABLE_COMMENT COMMENTS,\n" +
            "\tdate_format(create_time,'%Y-%m-%d %H:%i:%s') CREATED,\n" +
            "\tdate_format(update_time,'%Y-%m-%d %H:%i:%s') LASTTIME\n" +
            "FROM\n" +
            "\tinformation_schema. TABLES\n" +
            "WHERE\n" +
            "\ttable_schema = (SELECT DATABASE())\n" +
            "order by IFNULL(lasttime,CREATED) desc";



    private final String TABLECOLUM_COMMENT_MYSQL_SQL="select column_name COLUMNNAME,COLUMN_COMMENT COMMENTS,\n" +
            "    (case when (DATA_TYPE='int' or DATA_TYPE ='bigint' or DATA_TYPE='decimal')  and numeric_scale=0 and numeric_PRECISION<=4 then 'Short'   \n" +
            "                when (DATA_TYPE='int' or DATA_TYPE ='bigint' or DATA_TYPE='decimal') and numeric_scale=0 and numeric_PRECISION<=9 and numeric_PRECISION>=5   then 'Integer' \n" +
            "                when (DATA_TYPE='int' or DATA_TYPE ='bigint' or DATA_TYPE='decimal') and numeric_scale=0 and numeric_PRECISION<=18 and  numeric_PRECISION>=10 then 'Long' \n" +
            "                when (DATA_TYPE='int' or DATA_TYPE ='bigint' or DATA_TYPE='decimal')  and (numeric_PRECISION is null or numeric_PRECISION>=19) then 'BigDecimal' \n" +
            "                when (DATA_TYPE='int' or DATA_TYPE ='bigint' or DATA_TYPE='decimal') and numeric_scale>0 and numeric_PRECISION<=9  then 'Float' \n" +
            "                when (DATA_TYPE='int' or DATA_TYPE ='bigint' or DATA_TYPE='decimal') and numeric_scale>0 and numeric_PRECISION>=9  then 'Double'\n" +
            "                else data_type  end\n" +
            "                ) DATATYPE,\n" +
            "         COLUMN_KEY iskey\n" +
            " from information_schema.columns where table_name = ?";

    private final String QUERYMAXSQL="select IFNULL(max(id),0)+1 from  ";


    private  String  INSERT_TABLEGEN_SQL="INSERT INTO "+TABLEGEN_TABLENAME+"(id,table_name,create_time,create_user,numbers)"+
            " values (?,?,now(),?,?)";


    private String  BATCHINSERT_TABLEDETAILGEN_SQL="INSERT INTO "+TABLEDETAILGEN_TABLENAME+"(" +
            "id,table_name, create_time, create_user, numbers, type, tablegen_id, column_name, default_value, query_like, code_key,customsql)"+
            " values (?,?,now(),?,?,?,?,?,?,?,?,?)";

    @Override
    public String dataType() {
        return "mysql";
    }

    @Override
    public String getTABLE_COMMENT_SQL() {
        return TABLE_COMMENT_MYSQL_SQL;
    }

    @Override
    public String getTABLE_COMMENT_ALL_SQL() {
        return TABLE_COMMENT_ALL_MYSQL_SQL;
    }

    @Override
    public String getTABLECOLUM_COMMENT_SQL() {
        return TABLECOLUM_COMMENT_MYSQL_SQL;
    }

    @Override
    public String getQUERYMAXSQL() {
        return QUERYMAXSQL;
    }

    @Override
    public String getINSERT_TABLEGEN_SQL() {
        return INSERT_TABLEGEN_SQL;
    }

    @Override
    public String getBATCHINSERT_TABLEDETAILGEN_SQL() {
        return BATCHINSERT_TABLEDETAILGEN_SQL;
    }

    @Override
    public String getTABLEGEN_TABLENAME() {
        return TABLEGEN_TABLENAME;
    }

    @Override
    public String getTABLEDETAILGEN_TABLENAME() {
        return TABLEDETAILGEN_TABLENAME;
    }

    @Override
    public String getDELETE_TABLEGEN_SQL(){
        return "DELETE from "+getTABLEGEN_TABLENAME()+"  where TABLE_NAME=? ";
    }
    @Override
    public String getDELETE_TABLEDETAILGEN_SQL(){
        return "DELETE from "+getTABLEDETAILGEN_TABLENAME()+"  where TABLE_NAME=?";
    }
}
