
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.SqlInfoCommentModel;
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.model.SqlFormatModel;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import net.sf.jsqlparser.JSQLParserException;

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


/**
 * 备注 相关
 */
public class BuildComment implements ISQLExpression<List<SqlFormatModel>> {


    @Override
    public List<SqlFormatModel> interpret(SQLInterpretContext context) throws JSQLParserException {
        SqlInfoCommentModel commentModel = context.getCommentModel();
        if (FuncBase.isEmpty(commentModel)) {
            return null;
        }

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


        DbColunmTypesEntity dbColunmTypes = context.getDbColunmTypes();

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

        String schemaName = FuncBase.concatSymbol(context.getSchemaName(), symbol);
        String tableName = FuncBase.concatSymbol(commentModel.getTableName(), symbol, upperFlag);
        String tableAlias = commentModel.getTableAlias();
        List<FieldModel> fieldModelList = commentModel.getFieldModelList();

        List<String> ddlList = new ArrayList<>();
        String tableDdl = this.tableComment(schemaName, tableName, tableAlias);
        if (FuncBase.isNotEmpty(tableDdl)) {
            ddlList.add(tableDdl);
        }
        List<String> columnDdlList = this.columnComment(dbColunmTypes, schemaName, tableName, fieldModelList);
        if (FuncBase.isNotEmpty(columnDdlList)) {
            ddlList.addAll(columnDdlList);
        }

        return PlusUtils.ddl2SqlFormatModel(ddlList);
    }

    /**
     * 表备注
     *
     * @return
     */
    private String tableComment(String schemaName, String tableName, String tableAlias) {
        if (FuncBase.isEmpty(tableAlias)) {
            return null;
        }
        String str = "COMMENT ON TABLE %s.%s IS '%s';";
        String ddl = String.format(str, schemaName, tableName, tableAlias);
        return ddl;
    }

    private List<String> columnComment(DbColunmTypesEntity dbColunmTypes, String schemaName, String tableName, List<FieldModel> fieldModelList) {

        if (FuncBase.isEmpty(fieldModelList)) {
            return null;
        }
        String symbol = dbColunmTypes.getSymbol();
        Boolean upperFlag = dbColunmTypes.getUpperFlag();

        List<String> ddlList = new ArrayList<>();
        for (FieldModel fieldModel : fieldModelList) {
            String fieldCode = FuncBase.concatSymbol(fieldModel.getFieldCode(), symbol, upperFlag);
            String fieldName = fieldModel.getFieldName();

            String str = "COMMENT ON COLUMN %s.%s.%s IS '%s';";
            String ddl = String.format(str, schemaName, tableName, fieldCode, fieldName);
            ddlList.add(ddl);
        }
        return ddlList;
    }

}
