
package com.jeelowcode.framework.plus.build.build.dql;

import com.jeelowcode.framework.plus.build.ISQLExpression;
import com.jeelowcode.framework.plus.build.buildmodel.SQLInterpretContext;
import com.jeelowcode.framework.plus.build.buildmodel.dql.SqlInfoDeleteModel;
import com.jeelowcode.framework.plus.core.dql.update.JeeLowCodeUpdateWrapper;
import com.jeelowcode.framework.plus.core.model.SqlFormatModel;
import com.jeelowcode.framework.utils.utils.FuncBase;

import java.util.Map;

/**
 * 构建 删除 语句
 */
public class BuildDelete implements ISQLExpression<SqlFormatModel> {


    public SqlFormatModel interpret(SQLInterpretContext context){
        SqlInfoDeleteModel sqlInfo = context.getDeleteModel();
        String tableName = sqlInfo.getTableName();
        Boolean isReal = sqlInfo.getReal();


        SqlFormatModel sqlFormatModel;
        if (isReal){//真实删除
            String sql = "Delete from " + tableName;
            sqlFormatModel = new SqlFormatModel(sql);
        }else {//逻辑删除
            JeeLowCodeUpdateWrapper updateWrapper = new JeeLowCodeUpdateWrapper<>();
            updateWrapper.set("is_deleted",-1);

            String sqlSet = updateWrapper.getSqlSet();
            Map<String,Object> paramNameValuePairs = updateWrapper.getParamNameValuePairs();

            //处理null值
            for (Map.Entry<String,Object> entry : paramNameValuePairs.entrySet()) {
                Object value = entry.getValue();
                if(FuncBase.isEmpty(value)){
                    entry.setValue(null);
                }
            }

            String sql = "update " + tableName + " set " + sqlSet ;
            sqlFormatModel = new SqlFormatModel(sql, paramNameValuePairs);
        }
        return  sqlFormatModel;

    }


}
