
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.BuildDelete;
import com.lideeyunji.tool.framework.sql.build.build.dql.BuildWhere;
import com.lideeyunji.tool.framework.sql.build.buildmodel.SQLInterpretContext;
import com.lideeyunji.tool.framework.sql.build.buildmodel.dql.SqlInfoDeleteModel;
import com.lideeyunji.tool.framework.sql.build.buildmodel.dql.SqlInfoWhereModel;
import com.lideeyunji.tool.framework.sql.core.model.SqlFormatModel;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * 删除-封装器
 */
public class SqlInfoDeleteWrapper {

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

    public static class Wrapper extends BaseWrapper {

        private String dbSchemaName;
        private SqlInfoDeleteModel.Builder builderDelete = SqlInfoDeleteModel.builder();
        private SqlInfoWhereModel.Builder builderWhere = SqlInfoWhereModel.builder();

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

        //逻辑删除
        public Wrapper setTableName(String tableName) {
            builderDelete.setTableName(tableName);
            initSet.add(BUILDER_DELETE);
            return this;
        }
        //true=真实删除，物理删除  false=逻辑删除
        public Wrapper setTableName(String tableName, boolean isReal) {
            builderDelete.setTableName(tableName,isReal);
            initSet.add(BUILDER_DELETE);
            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);
            SqlInfoDeleteModel<BuildDelete> buildDelete = builderDelete.build();
            SqlInfoWhereModel<BuildWhere> buildWhere = builderWhere.buildWhere();

            List<ISQLExpression> expressionList=new ArrayList<>();
            if(initSet.contains(BUILDER_DELETE)){
                context.setDeleteModel(buildDelete);
                expressionList.add(new BuildDelete());
            }
            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);
        }
    }


}
