
package com.lideeyunji.tool.framework.sql.build.buildmodel.wrapper;

import com.lideeyunji.tool.framework.sql.SqlHelper;
import com.lideeyunji.tool.framework.sql.build.ISQLExpression;
import com.lideeyunji.tool.framework.sql.build.build.AndExpression;
import com.lideeyunji.tool.framework.sql.build.build.dql.BuildInsert;
import com.lideeyunji.tool.framework.sql.build.buildmodel.SQLInterpretContext;
import com.lideeyunji.tool.framework.sql.build.buildmodel.dql.SqlInfoInsertModel;
import com.lideeyunji.tool.framework.sql.core.model.SqlFormatModel;
import com.lideeyunji.tool.framework.yunji.enums.LideeYunJiFieldTypeEnum;

import java.util.*;

/**
 * 新增-封装器
 */
public class SqlInfoInsertWrapper {

    public static Wrapper wrapper(String dbSchemaName){
        return new Wrapper(dbSchemaName);
    }
    public static class Wrapper extends BaseWrapper {

        private String dbSchemaName;
        private SqlInfoInsertModel.Builder builderInsert= SqlInfoInsertModel.builder();

        public Wrapper(String dbSchemaName) {
            this.dbSchemaName = dbSchemaName;
        }

        public Wrapper setTableName(String tableName){
            builderInsert.setTableName(tableName);
            return this;
        }
        public Wrapper setColumn(String column, Object val){
            builderInsert.addColumn(column,val);
            initSet.add(BUILDER_INSERT);
            return this;
        }

        public Wrapper setMap(Map<String,Object> insertMap){
            builderInsert.addMap(insertMap);
            initSet.add(BUILDER_INSERT);
            return this;
        }
        public Wrapper setFieldType(Map<String, LideeYunJiFieldTypeEnum> fieldTypeEnumMap){
            builderInsert.setFieldType(fieldTypeEnumMap);
            return this;
        }
        public Wrapper build() {
            return this;
        }


        public SqlFormatModel buildSql() {
            SQLInterpretContext context = new SQLInterpretContext(dbSchemaName);
            SqlInfoInsertModel<BuildInsert> buildInsert = builderInsert.build();

            List<ISQLExpression> expressionList=new ArrayList<>();
            if(initSet.contains(BUILDER_INSERT)){
                context.setInsertModel(buildInsert);
                expressionList.add(new BuildInsert());
            }

            ISQLExpression expression = new AndExpression(expressionList);
            List<SqlFormatModel> sqlFormatModelList = SqlHelper.sqlExpression(context, expression);
            return sqlFormatModelList.get(0);
        }
    }


}
