Browse Source

优化创建或更新条件处理逻辑

woody 1 năm trước cách đây
mục cha
commit
e61cc50f6f

+ 110 - 6
framework-database/src/main/java/com/chelvc/framework/database/support/Updates.java

@@ -3,6 +3,7 @@ package com.chelvc.framework.database.support;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
@@ -11,10 +12,12 @@ import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
 import com.chelvc.framework.common.util.AssertUtils;
+import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.database.entity.Creatable;
 import com.chelvc.framework.database.entity.Deletable;
 import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 import lombok.NonNull;
 import org.apache.ibatis.reflection.property.PropertyNamer;
 
@@ -50,6 +53,32 @@ public final class Updates {
         return update.all(clazz);
     }
 
+    /**
+     * 更新除指定字段外的所有字段
+     *
+     * @param clazz   对象类型
+     * @param columns 排除字段数组
+     * @param <T>     数据类型
+     * @return 更新对象实例
+     */
+    public static <T> Update<T> not(@NonNull Class<T> clazz, @NonNull String... columns) {
+        Update<T> update = update();
+        return update.not(clazz, columns);
+    }
+
+    /**
+     * 更新除指定字段外的所有字段
+     *
+     * @param clazz  对象类型
+     * @param getter 排除属性Getter方法
+     * @param <T>    数据类型
+     * @return 更新对象实例
+     */
+    public static <T> Update<T> not(@NonNull Class<T> clazz, @NonNull SFunction<T, ?> getter) {
+        Update<T> update = update();
+        return update.not(clazz, getter);
+    }
+
     /**
      * 相等
      *
@@ -98,6 +127,30 @@ public final class Updates {
         return update.add(getter);
     }
 
+    /**
+     * 清空
+     *
+     * @param columns 列名数组
+     * @param <T>     数据类型
+     * @return 更新对象实例
+     */
+    public static <T> Update<T> clear(@NonNull String... columns) {
+        Update<T> update = update();
+        return update.clear(columns);
+    }
+
+    /**
+     * 清空
+     *
+     * @param getter 属性Getter方法
+     * @param <T>    数据类型
+     * @return 更新对象实例
+     */
+    public static <T> Update<T> clear(@NonNull SFunction<T, ?> getter) {
+        Update<T> update = update();
+        return update.clear(getter);
+    }
+
     /**
      * 相减
      *
@@ -225,21 +278,44 @@ public final class Updates {
          * @return 更新对象实例
          */
         public Update<T> all(@NonNull Class<T> clazz) {
+            return this.not(clazz);
+        }
+
+        /**
+         * 更新除指定字段外的所有字段
+         *
+         * @param clazz   对象类型
+         * @param columns 排除字段数组
+         * @return 更新对象实例
+         */
+        public Update<T> not(@NonNull Class<T> clazz, @NonNull String... columns) {
             this.initializeColumnCache(clazz);
             TableInfo table = TableInfoHelper.getTableInfo(clazz);
             boolean creatable = Creatable.class.isAssignableFrom(clazz);
             boolean deletable = Deletable.class.isAssignableFrom(clazz);
             String key = StringUtils.ifEmpty(table.getKeyProperty(), LambdaUtils::formatKey);
+            Set<String> excludes = ObjectUtils.isEmpty(columns) ? Collections.emptySet() : Sets.newHashSet(columns);
             this.columns.forEach((property, column) -> {
                 if (!Objects.equals(property, key)
                         && !(creatable && ("CREATOR".equals(property) || "CREATETIME".equals(property)))
-                        && !(deletable && "DELETED".equals(property))) {
+                        && !(deletable && "DELETED".equals(property)) && !excludes.contains(column.getColumn())) {
                     this.eq(column.getColumn());
                 }
             });
             return this;
         }
 
+        /**
+         * 更新除指定字段外的所有字段
+         *
+         * @param clazz  对象类型
+         * @param getter 排除属性Getter方法
+         * @return 更新对象实例
+         */
+        public Update<T> not(@NonNull Class<T> clazz, @NonNull SFunction<T, ?> getter) {
+            return this.not(clazz, this.getter2column(getter));
+        }
+
         /**
          * 相等
          *
@@ -286,6 +362,29 @@ public final class Updates {
             return this.add(this.getter2column(getter));
         }
 
+        /**
+         * 清空
+         *
+         * @param columns 列名数组
+         * @return 更新对象实例
+         */
+        public Update<T> clear(@NonNull String... columns) {
+            for (String column : columns) {
+                this.expression(column, " = null");
+            }
+            return this;
+        }
+
+        /**
+         * 清空
+         *
+         * @param getter 属性Getter方法
+         * @return 更新对象实例
+         */
+        public Update<T> clear(@NonNull SFunction<T, ?> getter) {
+            return this.clear(this.getter2column(getter));
+        }
+
         /**
          * 相减
          *
@@ -368,12 +467,12 @@ public final class Updates {
         }
 
         /**
-         * 除更新字段
+         * 除更新字段
          *
          * @param columns 列名数字
          * @return 更新对象实例
          */
-        public Update<T> remove(@NonNull String... columns) {
+        public Update<T> exclude(@NonNull String... columns) {
             for (String column : columns) {
                 this.expressions.remove(column);
             }
@@ -381,13 +480,13 @@ public final class Updates {
         }
 
         /**
-         * 除更新字段
+         * 除更新字段
          *
          * @param getter 属性Getter方法
          * @return 更新对象实例
          */
-        public Update<T> remove(@NonNull SFunction<T, ?> getter) {
-            return this.remove(this.getter2column(getter));
+        public Update<T> exclude(@NonNull SFunction<T, ?> getter) {
+            return this.exclude(this.getter2column(getter));
         }
 
         /**
@@ -398,5 +497,10 @@ public final class Updates {
         public Map<String, String> getExpressions() {
             return Collections.unmodifiableMap(this.expressions);
         }
+
+        @Override
+        public String toString() {
+            return this.expressions.toString();
+        }
     }
 }