
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.SqlInfoAlterModel;
import com.lideeyunji.tool.framework.sql.entity.DbColunmTypesEntity;
import com.lideeyunji.tool.framework.sql.entity.FieldModel;
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.model.SqlFormatModel;
import com.lideeyunji.tool.framework.yunji.enums.LideeYunJiFieldTypeEnum;
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 BuildAlter implements ISQLExpression<List<SqlFormatModel>> {

    @Override
    public List<SqlFormatModel> interpret(SQLInterpretContext context) throws JSQLParserException {
        DbColunmTypesEntity dbColunmTypes = context.getDbColunmTypes();
        SqlInfoAlterModel alterModel = context.getAlterModel();

        if(FuncBase.isEmpty(alterModel)){
            return null;
        }
        //存在自定义ddl
        List<String> executeDdlList = alterModel.getExecuteDdlList();
        if(FuncBase.isNotEmpty(executeDdlList)){
            return PlusUtils.ddl2SqlFormatModel(executeDdlList);
        }

        String ddl="";
        switch (alterModel.getAlterType()) {
            case ADD_COLUMN:
                ddl = getAddColumnDDL(context.getSchemaName(),alterModel.getTableName(),alterModel.getFieldModel(),dbColunmTypes);
                break;
            case UPDATE_COLUMN:
                ddl = getModifyColumnDDL(context.getSchemaName(),alterModel.getTableName(),alterModel.getFieldModel(),dbColunmTypes);
                break;
        }
        return PlusUtils.ddl2SqlFormatModel(ddl);
    }


    //获取增加字段ddl
    private String getAddColumnDDL(String schemaName,String tableName, FieldModel fieldModel, DbColunmTypesEntity dbColunmTypes) {
        String symbol = dbColunmTypes.getSymbol();
        Boolean upperFlag = dbColunmTypes.getUpperFlag();

        tableName= FuncBase.concatSymbol(tableName,symbol,upperFlag);
        schemaName= FuncBase.concatSymbol(schemaName,symbol);

        LideeYunJiFieldTypeEnum fieldTypeEnum = fieldModel.getFieldType();
        DbColunmTypesEntity.TypeEntity typeEntity = PlusUtils.lideeyunJiType2DbType(fieldTypeEnum, dbColunmTypes);
        String fieldType = PlusUtils.getDbType(fieldTypeEnum, typeEntity.getDbType(), fieldModel.getFieldLen(), fieldModel.getFieldPointLen(),typeEntity.getDbMaxLen());
        String defaultValSql = PlusUtils.getDefaultValSql(fieldModel.getFieldDefaultVal(), fieldModel.getIsNull(),fieldModel.getDbNowIsNull());
        String fieldCode = FuncBase.concatSymbol(fieldModel.getFieldCode(),symbol,upperFlag);

        lideeYunJiAlterTable alterTable = new lideeYunJiAlterTable();
        alterTable.setTableName(schemaName+ StringPool.DOT+tableName);
        alterTable.addCoulumn(fieldCode, fieldType, defaultValSql);
        String ddl = alterTable.getFullSQL();
        return ddl;
    }

    //修改字段
    private String getModifyColumnDDL(String schemaName,String tableName, FieldModel fieldModel, DbColunmTypesEntity dbColunmTypes) {
        String symbol = dbColunmTypes.getSymbol();
        Boolean upperFlag = dbColunmTypes.getUpperFlag();


        tableName=FuncBase.concatSymbol(tableName,symbol,upperFlag);
        schemaName= FuncBase.concatSymbol(schemaName,symbol);

        LideeYunJiFieldTypeEnum fieldTypeEnum = fieldModel.getFieldType();
        DbColunmTypesEntity.TypeEntity typeEntity = PlusUtils.lideeyunJiType2DbType(fieldTypeEnum, dbColunmTypes);
        String fieldType = PlusUtils.getDbType(fieldTypeEnum, typeEntity.getDbType(), fieldModel.getFieldLen(), fieldModel.getFieldPointLen(),typeEntity.getDbMaxLen());
        String defaultValSql = PlusUtils.getDefaultValSql(fieldModel.getFieldDefaultVal(), fieldModel.getIsNull(),fieldModel.getDbNowIsNull());
        String fieldCode = fieldModel.getFieldCode();

        lideeYunJiAlterTable alterTable = new lideeYunJiAlterTable();
        alterTable.setTableName(schemaName+ StringPool.DOT+tableName);
        alterTable.modifyColumn(FuncBase.concatSymbol(fieldCode,symbol,upperFlag), fieldType, defaultValSql);
        return alterTable.getFullSQL();
    }

}
