Parcourir la source

优化增强业务实现逻辑

woody il y a 1 an
Parent
commit
771c574413

+ 9 - 11
framework-database/src/main/java/com/chelvc/framework/database/context/DatabaseContextHolder.java

@@ -621,26 +621,24 @@ public final class DatabaseContextHolder {
      * @param entity 数据实体
      */
     public static void initializeEntityDefaultValue(@NonNull Entity<?> entity) {
-        // 设置新增属性默认值
-        Long operator = SessionContextHolder.getId();
-        if (operator != null && entity instanceof Creatable) {
+        if (entity instanceof Creatable) {
             Creatable<?> creatable = (Creatable<?>) entity;
             if (creatable.getCreator() == null) {
-                creatable.setCreator(operator);
+                creatable.setCreator(SessionContextHolder.getId());
             }
             if (creatable.getCreateTime() == null) {
                 creatable.setCreateTime(new Date());
             }
         }
-
-        // 设置修改属性默认值
-        if (operator != null && entity instanceof Updatable) {
+        if (entity instanceof Updatable) {
             Updatable<?> updatable = (Updatable<?>) entity;
-            updatable.setUpdater(operator);
-            updatable.setUpdateTime(new Date());
+            if (updatable.getUpdater() == null) {
+                updatable.setUpdater(SessionContextHolder.getId());
+            }
+            if (updatable.getUpdateTime() == null) {
+                updatable.setUpdateTime(new Date());
+            }
         }
-
-        // 设置删除属性默认值
         if (entity instanceof Deletable) {
             Deletable<?> deletable = (Deletable<?>) entity;
             if (deletable.getDeleted() == null) {

+ 13 - 9
framework-database/src/main/java/com/chelvc/framework/database/interceptor/PropertyUpdateInterceptor.java

@@ -71,10 +71,11 @@ public class PropertyUpdateInterceptor implements Interceptor {
      * 数据更新
      *
      * @param invocation 方法调用信息
+     * @param operation  数据更新类型
      * @return 更新结果
      * @throws Throwable 操作异常
      */
-    private Object modify(Invocation invocation) throws Throwable {
+    private Object modify(Invocation invocation, SqlCommandType operation) throws Throwable {
         // 获取更新目标对象实例
         Object object = invocation.getArgs()[1];
         if (object instanceof MapperMethod.ParamMap) {
@@ -93,11 +94,11 @@ public class PropertyUpdateInterceptor implements Interceptor {
         if (object instanceof Collection) {
             ((Collection<?>) object).forEach(entity -> {
                 if (entity instanceof Entity) {
-                    this.modify((Entity<?>) entity);
+                    this.modify((Entity<?>) entity, operation);
                 }
             });
         } else if (object instanceof Entity) {
-            this.modify((Entity<?>) object);
+            this.modify((Entity<?>) object, operation);
         }
         return invocation.proceed();
     }
@@ -105,13 +106,16 @@ public class PropertyUpdateInterceptor implements Interceptor {
     /**
      * 实体信息更新
      *
-     * @param entity 实体对象实例
-     * @param <T>    实体类型
+     * @param entity    实体对象实例
+     * @param operation 数据更新类型
+     * @param <T>       实体类型
      */
     @SuppressWarnings("unchecked")
-    private <T extends Entity<?>> void modify(T entity) {
-        // 初始化默认属性值
-        DatabaseContextHolder.initializeEntityDefaultValue(entity);
+    private <T extends Entity<?>> void modify(T entity, SqlCommandType operation) {
+        // 初始化实体新增默认属性值
+        if (operation == SqlCommandType.INSERT) {
+            DatabaseContextHolder.initializeEntityDefaultValue(entity);
+        }
 
         // 设置加密属性值
         Class<T> clazz = (Class<T>) entity.getClass();
@@ -165,7 +169,7 @@ public class PropertyUpdateInterceptor implements Interceptor {
         if (operation == SqlCommandType.SELECT) {
             return this.select(invocation);
         } else if (operation == SqlCommandType.INSERT || operation == SqlCommandType.UPDATE) {
-            return this.modify(invocation);
+            return this.modify(invocation, operation);
         }
         return invocation.proceed();
     }

+ 0 - 45
framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceLambdaQueryWrapper.java

@@ -1,45 +0,0 @@
-package com.chelvc.framework.database.support;
-
-import java.util.List;
-import java.util.function.Function;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
-import com.chelvc.framework.common.model.Pagination;
-import com.chelvc.framework.common.model.Paging;
-import com.chelvc.framework.database.context.DatabaseContextHolder;
-import lombok.NonNull;
-
-/**
- * Lambda查询包装器增强实现
- *
- * @author Woody
- * @date 2024/3/18
- */
-public class EnhanceLambdaQueryWrapper<T> extends LambdaQueryChainWrapper<T> {
-    public EnhanceLambdaQueryWrapper(BaseMapper<T> mapper) {
-        super(mapper);
-    }
-
-    /**
-     * 分页查询
-     *
-     * @param paging 查询分页信息
-     * @return 数据分页信息
-     */
-    public Pagination<T> paging(@NonNull Paging paging) {
-        return DatabaseContextHolder.paging(paging, this::page);
-    }
-
-    /**
-     * 分页查询
-     *
-     * @param paging  查询分页信息
-     * @param adapter 数据转换适配器
-     * @param <R>     目标数据类型
-     * @return 数据分页信息
-     */
-    public <R> Pagination<R> paging(@NonNull Paging paging, @NonNull Function<List<T>, List<R>> adapter) {
-        return DatabaseContextHolder.paging(paging, this::page, adapter);
-    }
-}

+ 0 - 46
framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceQueryWrapper.java

@@ -1,46 +0,0 @@
-package com.chelvc.framework.database.support;
-
-import java.util.List;
-import java.util.function.Function;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
-import com.chelvc.framework.common.model.Pagination;
-import com.chelvc.framework.common.model.Paging;
-import com.chelvc.framework.database.context.DatabaseContextHolder;
-import lombok.NonNull;
-
-/**
- * 查询包装器增强实现
- *
- * @param <T> 数据类型
- * @author Woody
- * @date 2024/3/18
- */
-public class EnhanceQueryWrapper<T> extends QueryChainWrapper<T> {
-    public EnhanceQueryWrapper(BaseMapper<T> mapper) {
-        super(mapper);
-    }
-
-    /**
-     * 分页查询
-     *
-     * @param paging 查询分页信息
-     * @return 数据分页信息
-     */
-    public Pagination<T> paging(@NonNull Paging paging) {
-        return DatabaseContextHolder.paging(paging, this::page);
-    }
-
-    /**
-     * 分页查询
-     *
-     * @param paging  查询分页信息
-     * @param adapter 数据转换适配器
-     * @param <R>     目标数据类型
-     * @return 数据分页信息
-     */
-    public <R> Pagination<R> paging(@NonNull Paging paging, @NonNull Function<List<T>, List<R>> adapter) {
-        return DatabaseContextHolder.paging(paging, this::page, adapter);
-    }
-}

+ 41 - 10
framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceService.java

@@ -1,15 +1,19 @@
 package com.chelvc.framework.database.support;
 
 import java.util.Collection;
+import java.util.Date;
 import java.util.List;
 import java.util.function.Function;
 
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.common.model.Pagination;
 import com.chelvc.framework.common.model.Paging;
+import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.database.context.DatabaseContextHolder;
+import com.chelvc.framework.database.entity.Updatable;
 import lombok.NonNull;
 
 /**
@@ -20,6 +24,43 @@ import lombok.NonNull;
  * @date 2024/3/10
  */
 public interface EnhanceService<T> extends IService<T> {
+    /**
+     * 更新实体(自动绑定更新人及更新时间)
+     *
+     * @param entity 实体对象实例
+     * @return true/false
+     */
+    default boolean modify(@NonNull T entity) {
+        if (entity instanceof Updatable) {
+            Updatable<?> updatable = (Updatable<?>) entity;
+            updatable.setUpdater(SessionContextHolder.getId());
+            updatable.setUpdateTime(new Date());
+        }
+        return this.updateById(entity);
+    }
+
+    /**
+     * 批量更新实体(自动绑定更新人及更新时间)
+     *
+     * @param entities 实体对象实例集合
+     * @return true/false
+     */
+    default boolean batchModify(@NonNull Collection<T> entities) {
+        if (ObjectUtils.isEmpty(entities)) {
+            return false;
+        }
+        if (Updatable.class.isAssignableFrom(this.getEntityClass())) {
+            Date now = new Date();
+            Long operator = SessionContextHolder.getId();
+            entities.forEach(entity -> {
+                Updatable<?> updatable = (Updatable<?>) entity;
+                updatable.setUpdater(operator);
+                updatable.setUpdateTime(now);
+            });
+        }
+        return this.updateBatchById(entities);
+    }
+
     /**
      * 创建或忽略实体
      *
@@ -60,16 +101,6 @@ public interface EnhanceService<T> extends IService<T> {
         return !entities.isEmpty() && ((EnhanceMapper<T>) this.getBaseMapper()).batchInsertUpdate(entities) > 0;
     }
 
-    @Override
-    default EnhanceQueryWrapper<T> query() {
-        return new EnhanceQueryWrapper<>(this.getBaseMapper());
-    }
-
-    @Override
-    default EnhanceLambdaQueryWrapper<T> lambdaQuery() {
-        return new EnhanceLambdaQueryWrapper<>(this.getBaseMapper());
-    }
-
     /**
      * 分页查询
      *