|
@@ -1,9 +1,24 @@
|
|
|
package com.chelvc.framework.database.support;
|
|
|
|
|
|
+import java.lang.reflect.Type;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
|
|
-import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
+import com.chelvc.framework.database.listener.AddListener;
|
|
|
+import com.chelvc.framework.database.listener.DeleteListener;
|
|
|
+import com.chelvc.framework.database.listener.ModifyListener;
|
|
|
+import com.chelvc.framework.database.listener.OperateListener;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import lombok.NonNull;
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
|
/**
|
|
|
* 业务增强操作默认实现
|
|
@@ -13,12 +28,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
* @author Woody
|
|
|
* @date 2024/4/29
|
|
|
*/
|
|
|
-public class DefaultEnhanceService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements EnhanceService<T> {
|
|
|
+public class DefaultEnhanceService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T>
|
|
|
+ implements EnhanceService<T>, ApplicationContextAware {
|
|
|
+ private final List<AddListener<T>> addListeners = Lists.newArrayListWithCapacity(0);
|
|
|
+ private final List<DeleteListener<T>> deleteListeners = Lists.newArrayListWithCapacity(0);
|
|
|
+ private final List<ModifyListener<T>> modifyListeners = Lists.newArrayListWithCapacity(0);
|
|
|
+
|
|
|
@Override
|
|
|
- public QueryChainWrapper<T> query() {
|
|
|
- QueryChainWrapper<T> query = super.query();
|
|
|
- query.setEntityClass(this.getEntityClass());
|
|
|
- return query;
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
+ applicationContext.getBeansOfType(OperateListener.class).values().forEach(this::register);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -27,4 +45,71 @@ public class DefaultEnhanceService<M extends BaseMapper<T>, T> extends ServiceIm
|
|
|
query.setEntityClass(this.getEntityClass());
|
|
|
return query;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized void register(@NonNull OperateListener<T> listener) {
|
|
|
+ // 校验监听数据类型与当前数据模型类型是否匹配
|
|
|
+ Class<?> type;
|
|
|
+ if (listener instanceof AddListener) {
|
|
|
+ type = AddListener.class;
|
|
|
+ } else if (listener instanceof DeleteListener) {
|
|
|
+ type = DeleteListener.class;
|
|
|
+ } else if (listener instanceof ModifyListener) {
|
|
|
+ type = ModifyListener.class;
|
|
|
+ } else {
|
|
|
+ type = OperateListener.class;
|
|
|
+ }
|
|
|
+ Type parameterized = ObjectUtils.lookupSuperclassParameterized(listener.getClass(), type);
|
|
|
+ if (ObjectUtils.type2class(parameterized) != this.getEntityClass()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注册监听器
|
|
|
+ if (listener instanceof AddListener) {
|
|
|
+ this.addListeners.add((AddListener<T>) listener);
|
|
|
+ } else if (listener instanceof DeleteListener) {
|
|
|
+ this.deleteListeners.add((DeleteListener<T>) listener);
|
|
|
+ } else if (listener instanceof ModifyListener) {
|
|
|
+ this.modifyListeners.add((ModifyListener<T>) listener);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void publishAddition(@NonNull T entity) {
|
|
|
+ this.addListeners.forEach(listener -> listener.add(entity));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void publishAddition(@NonNull Collection<T> entities) {
|
|
|
+ if (ObjectUtils.notEmpty(entities)) {
|
|
|
+ Collection<T> unmodifiable = Collections.unmodifiableCollection(entities);
|
|
|
+ this.addListeners.forEach(listener -> listener.add(unmodifiable));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void publishDeletion(@NonNull T entity) {
|
|
|
+ this.deleteListeners.forEach(listener -> listener.delete(entity));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void publishDeletion(@NonNull Collection<T> entities) {
|
|
|
+ if (ObjectUtils.notEmpty(entities)) {
|
|
|
+ Collection<T> unmodifiable = Collections.unmodifiableCollection(entities);
|
|
|
+ this.deleteListeners.forEach(listener -> listener.delete(unmodifiable));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void publishModification(@NonNull T before, @NonNull T after) {
|
|
|
+ this.modifyListeners.forEach(listener -> listener.modify(before, after));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void publishModification(@NonNull Collection<Pair<T, T>> pairs) {
|
|
|
+ if (ObjectUtils.notEmpty(pairs)) {
|
|
|
+ Collection<Pair<T, T>> unmodifiable = Collections.unmodifiableCollection(pairs);
|
|
|
+ this.modifyListeners.forEach(listener -> listener.modify(unmodifiable));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|