|
@@ -2,17 +2,22 @@ package com.chelvc.framework.database.interceptor;
|
|
|
|
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
import com.chelvc.framework.common.exception.ParameterInvalidException;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.database.context.DatabaseContextHolder;
|
|
|
import com.chelvc.framework.database.context.UniqueContext;
|
|
|
+import com.chelvc.framework.database.entity.Creatable;
|
|
|
+import com.chelvc.framework.database.entity.Deletable;
|
|
|
import com.chelvc.framework.database.entity.Entity;
|
|
|
+import com.chelvc.framework.database.entity.Updatable;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.ibatis.binding.MapperMethod;
|
|
|
import org.apache.ibatis.cache.CacheKey;
|
|
@@ -45,6 +50,36 @@ import org.springframework.stereotype.Component;
|
|
|
RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})
|
|
|
})
|
|
|
public class PropertyUpdateInterceptor implements Interceptor {
|
|
|
+ /**
|
|
|
+ * 初始化实体默认属性值
|
|
|
+ *
|
|
|
+ * @param entity 数据实体
|
|
|
+ */
|
|
|
+ private void initializeDefaultValue(Entity<?> entity) {
|
|
|
+ Date now = new Date();
|
|
|
+ Long operator = SessionContextHolder.getId();
|
|
|
+ if (entity instanceof Creatable) {
|
|
|
+ Creatable<?> creatable = (Creatable<?>) entity;
|
|
|
+ if (creatable.getCreator() == null) {
|
|
|
+ creatable.setCreator(operator);
|
|
|
+ }
|
|
|
+ if (creatable.getCreateTime() == null) {
|
|
|
+ creatable.setCreateTime(now);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (entity instanceof Updatable) {
|
|
|
+ Updatable<?> updatable = (Updatable<?>) entity;
|
|
|
+ updatable.setUpdater(operator);
|
|
|
+ updatable.setUpdateTime(now);
|
|
|
+ }
|
|
|
+ if (entity instanceof Deletable) {
|
|
|
+ Deletable<?> deletable = (Deletable<?>) entity;
|
|
|
+ if (deletable.getDeleted() == null) {
|
|
|
+ deletable.setDeleted(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 数据查询
|
|
|
*
|
|
@@ -71,11 +106,10 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
|
* 数据更新
|
|
|
*
|
|
|
* @param invocation 方法调用信息
|
|
|
- * @param operation 数据更新类型
|
|
|
* @return 更新结果
|
|
|
* @throws Throwable 操作异常
|
|
|
*/
|
|
|
- private Object modify(Invocation invocation, SqlCommandType operation) throws Throwable {
|
|
|
+ private Object modify(Invocation invocation) throws Throwable {
|
|
|
// 获取更新目标对象实例
|
|
|
Object object = invocation.getArgs()[1];
|
|
|
if (object instanceof MapperMethod.ParamMap) {
|
|
@@ -94,11 +128,11 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
|
if (object instanceof Collection) {
|
|
|
((Collection<?>) object).forEach(entity -> {
|
|
|
if (entity instanceof Entity) {
|
|
|
- this.modify((Entity<?>) entity, operation);
|
|
|
+ this.modify((Entity<?>) entity);
|
|
|
}
|
|
|
});
|
|
|
} else if (object instanceof Entity) {
|
|
|
- this.modify((Entity<?>) object, operation);
|
|
|
+ this.modify((Entity<?>) object);
|
|
|
}
|
|
|
return invocation.proceed();
|
|
|
}
|
|
@@ -106,16 +140,13 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
|
/**
|
|
|
* 实体信息更新
|
|
|
*
|
|
|
- * @param entity 实体对象实例
|
|
|
- * @param operation 数据更新类型
|
|
|
- * @param <T> 实体类型
|
|
|
+ * @param entity 实体对象实例
|
|
|
+ * @param <T> 实体类型
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- private <T extends Entity<?>> void modify(T entity, SqlCommandType operation) {
|
|
|
- // 初始化实体新增默认属性值
|
|
|
- if (operation == SqlCommandType.INSERT) {
|
|
|
- DatabaseContextHolder.initializeEntityDefaultValue(entity);
|
|
|
- }
|
|
|
+ private <T extends Entity<?>> void modify(T entity) {
|
|
|
+ // 初始化实体默认属性值
|
|
|
+ this.initializeDefaultValue(entity);
|
|
|
|
|
|
// 设置加密属性值
|
|
|
Class<T> clazz = (Class<T>) entity.getClass();
|
|
@@ -169,7 +200,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, operation);
|
|
|
+ return this.modify(invocation);
|
|
|
}
|
|
|
return invocation.proceed();
|
|
|
}
|