
package com.lideeyunji.tool.framework.sql.build.build.ddl;

import com.lideeyunji.tool.framework.sql.build.ISQLExpression;
import com.lideeyunji.tool.framework.sql.build.buildmodel.SQLInterpretContext;
import com.lideeyunji.tool.framework.sql.build.buildmodel.ddl.SqlInfoDropModel;
import com.lideeyunji.tool.framework.sql.entity.DbColunmTypesEntity;
import com.lideeyunji.tool.framework.sql.utils.PlusUtils;
import com.lideeyunji.tool.framework.sql.core.ddl.alter.lideeYunJiAlterTable;
import com.lideeyunji.tool.framework.sql.core.ddl.drop.lideeYunJiDropTable;
import com.lideeyunji.tool.framework.sql.core.model.SqlFormatModel;
import com.lideeyunji.tool.framework.yunji.tool.StringPool;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import net.sf.jsqlparser.JSQLParserException;

import java.util.List;

/**
 * 删除表，字段，索引
 */
public class BuildDrop implements ISQLExpression<List<SqlFormatModel>> {


    @Override
    public List<SqlFormatModel> interpret(SQLInterpretContext context) throws JSQLParserException {
        SqlInfoDropModel dropModel = context.getDropModel();
        if (FuncBase.isEmpty(dropModel)) {
            return null;
        }
        //存在自定义ddl
        List<String> executeDdlList = dropModel.getExecuteDdlList();
        if(FuncBase.isNotEmpty(executeDdlList)){
            return PlusUtils.ddl2SqlFormatModel(executeDdlList);
        }

        DbColunmTypesEntity dbColunmTypes = context.getDbColunmTypes();
        String symbol = dbColunmTypes.getSymbol();
        Boolean upperFlag = dbColunmTypes.getUpperFlag();

        String tableName = FuncBase.concatSymbol(dropModel.getTableName(), symbol, upperFlag);
        String indexName = FuncBase.concatSymbol(dropModel.getIndexName(), symbol, upperFlag);
        String cloumnCode = FuncBase.concatSymbol(dropModel.getCloumnCode(), symbol, upperFlag);
        String schemaName = FuncBase.concatSymbol(context.getSchemaName(), symbol);

        String ddl = "";
        switch (dropModel.getDropType()) {
            case DROP_TABLE:
                ddl = this.dropTable(schemaName, tableName);
                break;
            case DROP_COLUMN:
                ddl = this.dropColumn(schemaName, tableName, cloumnCode,dropModel.getCloumnFormat());
                break;
            case DROP_INDEX:
                ddl = this.dropIndex(schemaName, indexName);
                break;
        }

        return PlusUtils.ddl2SqlFormatModel(ddl);
    }

    /**
     * 删除表
     *
     * @param schemaName
     * @param tableName
     * @return
     */
    private String dropTable(String schemaName, String tableName) {
        lideeYunJiDropTable dropTable = new lideeYunJiDropTable();
        dropTable.setTableName(schemaName + StringPool.DOT + tableName);
        return dropTable.getFullSQL();
    }

    /**
     * 删除字段
     *
     * @param schemaName
     * @param tableName
     * @param cloumnCode
     * @return
     */
    private String dropColumn(String schemaName, String tableName, String cloumnCode,String cloumnFormat) {
        if(FuncBase.isNotEmpty(cloumnFormat)){
            cloumnCode =String.format(cloumnFormat,cloumnCode);
        }
        lideeYunJiAlterTable dropColumn = new lideeYunJiAlterTable();
        dropColumn.setTableName(schemaName + StringPool.DOT + tableName);
        dropColumn.dropColumn(cloumnCode);
        return dropColumn.getFullSQL();
    }

    /**
     * 删除索引
     * @param schemaName
     * @param indexName
     * @return
     */
    private String dropIndex(String schemaName, String indexName) {
        String str = "DROP INDEX %s.%s";
        String ddl = String.format(str, schemaName, indexName);
        return ddl;
    }

}
