瀏覽代碼

恢复逻辑删除逻辑

hubin 7 年之前
父節點
當前提交
1b403f4894
共有 1 個文件被更改,包括 169 次插入0 次删除
  1. 169 0
      src/main/java/com/baomidou/mybatisplus/mapper/LogicSqlInjector.java

+ 169 - 0
src/main/java/com/baomidou/mybatisplus/mapper/LogicSqlInjector.java

@@ -102,6 +102,91 @@ public class LogicSqlInjector extends AutoSqlInjector {
         }
     }
 
+    /**
+     * <p>
+     * 注入查询 SQL 语句
+     * </p>
+     *
+     * @param batch       是否为批量插入
+     * @param mapperClass
+     * @param modelClass
+     * @param table
+     */
+    protected void injectSelectByIdSql(boolean batch, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
+        if (table.isLogicDelete()) {
+            SqlMethod sqlMethod = SqlMethod.LOGIC_SELECT_BY_ID;
+            SqlSource sqlSource;
+            if (batch) {
+                sqlMethod = SqlMethod.LOGIC_SELECT_BATCH_BY_IDS;
+                StringBuilder ids = new StringBuilder();
+                ids.append("\n<foreach item=\"item\" index=\"index\" collection=\"list\" separator=\",\">");
+                ids.append("#{item}");
+                ids.append("\n</foreach>");
+                sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(), sqlSelectColumns(table, false),
+                        table.getTableName(), table.getKeyColumn(), ids.toString(), getLogicDeleteSql(table)), modelClass);
+            } else {
+                sqlSource = new RawSqlSource(configuration, String.format(sqlMethod.getSql(), sqlSelectColumns(table, false), table.getTableName(),
+                        table.getKeyColumn(), table.getKeyProperty(), getLogicDeleteSql(table)), Object.class);
+            }
+            this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
+        } else {
+            // 正常查询
+            super.injectSelectByIdSql(batch, mapperClass, modelClass, table);
+        }
+    }
+
+    /**
+     * <p>
+     * 注入更新 SQL 语句
+     * </p>
+     *
+     * @param mapperClass
+     * @param modelClass
+     * @param table
+     */
+    protected void injectUpdateByIdSql(boolean selective, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
+        if (table.isLogicDelete()) {
+            SqlMethod sqlMethod = selective ? SqlMethod.LOGIC_UPDATE_BY_ID : SqlMethod.LOGIC_UPDATE_ALL_COLUMN_BY_ID;
+            String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlSet(selective, table, "et."),
+                    table.getKeyColumn(),
+                    "et." + table.getKeyProperty(),
+                    "<if test=\"et instanceof java.util.Map\">" +
+                            "<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">"
+                            + "and ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}"
+                            + "</if>"
+                            + "</if>" +
+                            getLogicDeleteSql(table)
+            );
+            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+            this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
+        } else {
+            super.injectUpdateByIdSql(selective, mapperClass, modelClass, table);
+        }
+    }
+
+    /**
+     * <p>
+     * SQL 更新 set 语句
+     * </p>
+     *
+     * @param table 表信息
+     * @return sql and 片段
+     */
+    public String getLogicDeleteSql(TableInfo table) {
+        StringBuilder sql = new StringBuilder();
+        List<TableFieldInfo> fieldList = table.getFieldList();
+        for (TableFieldInfo fieldInfo : fieldList) {
+            if (fieldInfo.isLogicDelete()) {
+                sql.append(" AND ").append(fieldInfo.getColumn());
+                if (StringUtils.isCharSequence(fieldInfo.getPropertyType())) {
+                    sql.append("='").append(fieldInfo.getLogicNotDeleteValue()).append("'");
+                } else {
+                    sql.append("=").append(fieldInfo.getLogicNotDeleteValue());
+                }
+            }
+        }
+        return sql.toString();
+    }
 
     /**
      * <p>
@@ -131,4 +216,88 @@ public class LogicSqlInjector extends AutoSqlInjector {
         return set.toString();
     }
 
+    // ------------ 处理逻辑删除条件过滤 ------------
+
+    @Override
+    protected String sqlWhere(TableInfo table) {
+        if (table.isLogicDelete()) {
+            StringBuilder where = new StringBuilder("\n<where>");
+            // 过滤逻辑
+            List<TableFieldInfo> fieldList = table.getFieldList();
+            // EW 逻辑
+            if (StringUtils.isNotEmpty(table.getKeyProperty())) {
+                where.append("\n<if test=\"ew.").append(table.getKeyProperty()).append("!=null\">");
+                where.append(" AND ").append(table.getKeyColumn()).append("=#{ew.");
+                where.append(table.getKeyProperty()).append("}");
+                where.append("</if>");
+            }
+            for (TableFieldInfo fieldInfo : fieldList) {
+                where.append(convertIfTag(fieldInfo, "ew.", false));
+                where.append(" AND ").append(fieldInfo.getColumn()).append("=#{ew.");
+                where.append(fieldInfo.getEl()).append("}");
+                where.append(convertIfTag(fieldInfo, true));
+            }
+            // 过滤逻辑
+            where.append("\n").append(getLogicDeleteSql(table));
+            where.append("\n</where>");
+            return where.toString();
+        }
+        // 正常逻辑
+        return super.sqlWhere(table);
+    }
+
+    @Override
+    protected String sqlWhereEntityWrapper(TableInfo table) {
+        if (table.isLogicDelete()) {
+            StringBuilder where = new StringBuilder(128);
+            where.append("\n<where>");
+            where.append("\n<if test=\"ew!=null\">");
+            where.append("\n<if test=\"ew.entity!=null\">");
+            if (StringUtils.isNotEmpty(table.getKeyProperty())) {
+                where.append("\n<if test=\"ew.entity.").append(table.getKeyProperty()).append("!=null\">");
+                where.append(" AND ").append(table.getKeyColumn()).append("=#{ew.entity.");
+                where.append(table.getKeyProperty()).append("}");
+                where.append("</if>");
+            }
+            List<TableFieldInfo> fieldList = table.getFieldList();
+            for (TableFieldInfo fieldInfo : fieldList) {
+                where.append(convertIfTag(fieldInfo, "ew.entity.", false));
+                where.append(" AND ").append(fieldInfo.getColumn()).append("=#{ew.entity.");
+                where.append(fieldInfo.getEl()).append("}");
+                where.append(convertIfTag(fieldInfo, true));
+            }
+            where.append("\n</if>");
+            where.append("\n").append(getLogicDeleteSql(table));
+            where.append("\n<if test=\"ew.sqlSegment!=null\">${ew.sqlSegment}\n</if>");
+            where.append("\n</if>");
+            where.append("\n</where>");
+            return where.toString();
+        }
+        // 正常逻辑
+        return super.sqlWhereEntityWrapper(table);
+    }
+
+
+    @Override
+    protected String sqlWhereByMap(TableInfo table) {
+        if (table.isLogicDelete()) {
+            StringBuilder where = new StringBuilder();
+            where.append("\n<where>");
+            // MAP 逻辑
+            where.append("\n<if test=\"cm!=null and !cm.isEmpty\">");
+            where.append("\n<foreach collection=\"cm.keys\" item=\"k\" separator=\"AND\">");
+            where.append("\n<if test=\"cm[k] != null\">");
+            where.append(SqlReservedWords.convert(getGlobalConfig(), "\n${k}")).append(" = #{cm[${k}]}");
+            where.append("</if>");
+            where.append("\n</foreach>");
+            where.append("\n</if>");
+            // 过滤逻辑
+            where.append("\n").append(getLogicDeleteSql(table));
+            where.append("\n</where>");
+            return where.toString();
+        }
+        // 正常逻辑
+        return super.sqlWhereByMap(table);
+    }
+
 }