Browse Source

优化Creatable、Updatable、Environmental、Deletable对象实例存储默认字段初始化逻辑

Woody 4 tuần trước cách đây
mục cha
commit
c81122773c

+ 89 - 4
framework-database/src/main/java/com/chelvc/framework/database/interceptor/DynamicInvokeInterceptor.java

@@ -660,6 +660,63 @@ public class DynamicInvokeInterceptor implements Interceptor {
         }
     }
 
+    /**
+     * 初始化模型实例默认值
+     *
+     * @param creatable 模型实例
+     * @param operator  操作人ID
+     * @param datetime  操作时间
+     */
+    private void initializeDefaultValue(Creatable<?> creatable, Long operator, Date datetime) {
+        if (creatable.getCreator() == null) {
+            creatable.setCreator(operator);
+        }
+        if (creatable.getCreateTime() == null) {
+            creatable.setCreateTime(datetime);
+        }
+    }
+
+    /**
+     * 初始化模型实例默认值
+     *
+     * @param updatable 模型实例
+     * @param operator  操作人ID
+     * @param datetime  操作时间
+     * @param cover     是否覆盖
+     */
+    private void initializeDefaultValue(Updatable<?> updatable, Long operator, Date datetime, boolean cover) {
+        if (cover || updatable.getUpdater() == null) {
+            updatable.setUpdater(operator);
+        }
+        if (cover || updatable.getUpdateTime() == null) {
+            updatable.setUpdateTime(datetime);
+        }
+    }
+
+    /**
+     * 初始化模型实例默认值
+     *
+     * @param environmental 模型实例
+     * @param env           环境标识
+     */
+    private void initializeDefaultValue(Environmental<?> environmental, String env) {
+        if (environmental.getEnv() == null) {
+            environmental.setEnv(env);
+        }
+    }
+
+    /**
+     * 初始化模型实例默认值
+     *
+     * @param deletable 模型实例
+     * @param deleted   是否删除
+     */
+    private void initializeDefaultValue(Deletable<?> deletable, boolean deleted) {
+        if (deletable.getDeleted() == null) {
+            deletable.setDeleted(deleted);
+        }
+    }
+
     /**
      * 初始化字段默认值,并返回是否需要重构SQL语句
      *
@@ -790,13 +847,14 @@ public class DynamicInvokeInterceptor implements Interceptor {
      * @param datetime 更新时间信息
      * @return true/false
      */
-    private boolean initializeDefaultValue(BoundSql bound, Update update, Table table, Pair<?, Expression> operator,
-                                           Pair<?, Expression> datetime) {
+    private boolean initializeDefaultValue(BoundSql bound, Update update, Table table, Pair<Long, Expression> operator,
+                                           Pair<Date, Expression> datetime) {
         Class<?> model = Tables.getTableClass(table.getName());
         if (model == null) {
             return false;
         }
         boolean rebuild = false;
+        Object entity = bound.getParameterObject();
         if (Creatable.class.isAssignableFrom(model)) {
             int index = this.lookupColumnIndex(table, Expressions.CREATOR_COLUMN, update.getColumns());
             if (index >= 0) {
@@ -805,22 +863,34 @@ public class DynamicInvokeInterceptor implements Interceptor {
             if ((index = this.lookupColumnIndex(table, Expressions.CREATE_TIME_COLUMN, update.getColumns())) >= 0) {
                 rebuild |= this.initializeDefaultValue(bound, update.getExpressions(), index, datetime, false);
             }
+            if (entity instanceof Creatable) {
+                this.initializeDefaultValue((Creatable<?>) entity, operator.getLeft(), datetime.getLeft());
+            }
         }
         if (Updatable.class.isAssignableFrom(model)) {
             rebuild |= this.initializeDefaultValue(bound, update, table, Expressions.UPDATER_COLUMN, operator);
             rebuild |= this.initializeDefaultValue(bound, update, table, Expressions.UPDATE_TIME_COLUMN, datetime);
+            if (entity instanceof Updatable) {
+                this.initializeDefaultValue((Updatable<?>) entity, operator.getLeft(), datetime.getLeft(), true);
+            }
         }
         if (Environmental.class.isAssignableFrom(model) && Expressions.env().getLeft() != null) {
             int index = this.lookupColumnIndex(table, Expressions.ENV_COLUMN, update.getColumns());
             if (index >= 0) {
                 rebuild |= this.initializeDefaultValue(bound, update.getExpressions(), index, Expressions.env(), false);
             }
+            if (entity instanceof Environmental) {
+                this.initializeDefaultValue((Environmental<?>) entity, Expressions.env().getLeft());
+            }
         }
         if (Deletable.class.isAssignableFrom(model)) {
             int index = this.lookupColumnIndex(table, Expressions.DELETED_COLUMN, update.getColumns());
             if (index >= 0) {
                 rebuild |= this.initializeDefaultValue(bound, update.getExpressions(), index, Expressions.FALSE, false);
             }
+            if (entity instanceof Deletable) {
+                this.initializeDefaultValue((Deletable<?>) entity, Expressions.FALSE.getLeft());
+            }
         }
         return rebuild;
     }
@@ -939,24 +1009,38 @@ public class DynamicInvokeInterceptor implements Interceptor {
         if (model == null) {
             return rebuild;
         }
-        Pair<?, Expression> operator = null, datetime = null;
+        Pair<Long, Expression> operator = null;
+        Pair<Date, Expression> datetime = null;
+        Object entity = bound.getParameterObject();
         if (Creatable.class.isAssignableFrom(model)) {
             operator = Expressions.operator();
             datetime = Expressions.datetime();
             rebuild = this.initializeDefaultValue(bound, insert, Expressions.CREATOR_COLUMN, operator);
             rebuild |= this.initializeDefaultValue(bound, insert, Expressions.CREATE_TIME_COLUMN, datetime);
+            if (entity instanceof Creatable) {
+                this.initializeDefaultValue((Creatable<?>) entity, operator.getLeft(), datetime.getLeft());
+            }
         }
         if (Updatable.class.isAssignableFrom(model)) {
             operator = ObjectUtils.ifNull(operator, Expressions::operator);
             datetime = ObjectUtils.ifNull(datetime, Expressions::datetime);
             rebuild |= this.initializeDefaultValue(bound, insert, Expressions.UPDATER_COLUMN, operator);
             rebuild |= this.initializeDefaultValue(bound, insert, Expressions.UPDATE_TIME_COLUMN, datetime);
+            if (entity instanceof Updatable) {
+                this.initializeDefaultValue((Updatable<?>) entity, operator.getLeft(), datetime.getLeft(), false);
+            }
         }
         if (Environmental.class.isAssignableFrom(model) && Expressions.env().getLeft() != null) {
             rebuild |= this.initializeDefaultValue(bound, insert, Expressions.ENV_COLUMN, Expressions.env());
+            if (entity instanceof Environmental) {
+                this.initializeDefaultValue((Environmental<?>) entity, Expressions.env().getLeft());
+            }
         }
         if (Deletable.class.isAssignableFrom(model)) {
             rebuild |= this.initializeDefaultValue(bound, insert, Expressions.DELETED_COLUMN, Expressions.FALSE);
+            if (entity instanceof Deletable) {
+                this.initializeDefaultValue((Deletable<?>) entity, Expressions.FALSE.getLeft());
+            }
         }
         return rebuild;
     }
@@ -1020,7 +1104,8 @@ public class DynamicInvokeInterceptor implements Interceptor {
         }
 
         // 初始化主表字段默认值
-        Pair<?, Expression> operator = Expressions.operator(), datetime = Expressions.datetime();
+        Pair<Long, Expression> operator = Expressions.operator();
+        Pair<Date, Expression> datetime = Expressions.datetime();
         rebuild |= this.initializeDefaultValue(bound, update, update.getTable(), operator, datetime);
 
         // 初始化关联表字段默认值