
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.buildmodel.SQLInterpretContext;
import com.lideeyunji.tool.framework.sql.build.buildmodel.dql.SqlInfoUpdateModel;
import com.lideeyunji.tool.framework.sql.build.buildmodel.dql.SqlInfoWhereModel;
import com.lideeyunji.tool.framework.sql.core.model.SqlFormatModel;
import com.lideeyunji.tool.framework.sql.build.build.dql.BuildUpdate;
import com.lideeyunji.tool.framework.sql.build.build.dql.BuildWhere;
import com.lideeyunji.tool.framework.yunji.enums.LideeYunJiFieldTypeEnum;

import java.util.*;
import java.util.function.Consumer;

/**
 * 更新-封装器
 */
public class SqlInfoUpdateWrapper {

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

        private String dbSchemaName;
        private SqlInfoUpdateModel.Builder builderUpdate=SqlInfoUpdateModel.builder();;
        private SqlInfoWhereModel.Builder builderWhere = SqlInfoWhereModel.builder();

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

        //update 相关
        public Wrapper setTableName(String tableName) {
            builderUpdate.setTableName(tableName);
            initSet.add(BUILDER_UPDATE);
            return this;
        }

        public Wrapper setColumn(String column, Object val){
            builderUpdate.addColumn(column,val);
            initSet.add(BUILDER_UPDATE);
            return this;
        }

        public Wrapper setMap(Map<String,Object> updateMap){
            builderUpdate.addMap(updateMap);
            initSet.add(BUILDER_UPDATE);
            return this;
        }
        public Wrapper setFieldType(Map<String, LideeYunJiFieldTypeEnum> fieldTypeEnumMap){
            builderUpdate.setFieldType(fieldTypeEnumMap);
            return this;
        }

        public Wrapper setWhere(Consumer<SqlInfoWhereModel.Builder> whereBuilderConsumer) {
            whereBuilderConsumer.accept(builderWhere);
            initSet.add(BUILDER_WHERE);
            return this;
        }
        public Wrapper build() {
            return this;
        }

        public SqlFormatModel buildSql() {
            SQLInterpretContext context = new SQLInterpretContext(dbSchemaName);
            SqlInfoUpdateModel<BuildUpdate> buildUpdate = builderUpdate.build();
            SqlInfoWhereModel<BuildWhere> buildWhere = builderWhere.buildWhere();

            List<ISQLExpression> expressionList=new ArrayList<>();
            if(initSet.contains(BUILDER_UPDATE)){
                context.setUpdateModel(buildUpdate);
                expressionList.add(new BuildUpdate());
            }
            if(initSet.contains(BUILDER_WHERE)){
                context.setWhereModel(buildWhere);
                expressionList.add(new BuildWhere());
            }

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


}
