From 1e9abcb3f5359b8326e57125f53d0484ded8086e Mon Sep 17 00:00:00 2001 From: Ice Date: Thu, 27 Jun 2024 18:24:05 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9AListenerManager=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BB=A5=E4=BB=BB=E5=8A=A1=E8=B0=83=E5=BA=A6=E5=8F=8A=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E6=8E=A5=E5=8F=A3=E5=9B=9E=E8=B0=83=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E8=87=AA=E7=94=B1=E6=8E=A7=E5=88=B6=E5=AE=A1=E8=AE=A1?= =?UTF-8?q?=E5=AD=97=E6=AE=B5(=E7=9B=AE=E5=89=8D=E4=BC=9A=E5=9B=A0?= =?UTF-8?q?=E6=8B=BF=E4=B8=8D=E5=88=B0=E4=B8=8A=E4=B8=8B=E6=96=87=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BF=A1=E6=81=AF=E5=B0=86=E8=87=AA=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E7=9A=84=E5=AE=A1=E8=AE=A1=E5=AD=97=E6=AE=B5=E6=94=B9=E4=B8=BA?= =?UTF-8?q?null=E5=80=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/orm/helper/ListenerManager.java | 183 ++++++++++++++++++ .../orm/listener/EntityInsertListener.java | 3 +- .../orm/listener/EntityUpdateListener.java | 3 +- 3 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/helper/ListenerManager.java diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/helper/ListenerManager.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/helper/ListenerManager.java new file mode 100644 index 0000000..708826f --- /dev/null +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/helper/ListenerManager.java @@ -0,0 +1,183 @@ +package com.ruoyi.common.orm.helper; + +import lombok.extern.slf4j.Slf4j; + +import java.util.function.Supplier; + +/** + * 监听器管理 + *

+ * 考虑任务调度、三方接口回调等可自由控制审计字段 + * + * @author Ice + * @version 1.0 + */ +@Slf4j +public class ListenerManager { + + private ListenerManager() {} + + private static ThreadLocal ignoreInsertListenerTl = ThreadLocal.withInitial(() -> Boolean.FALSE); + + private static ThreadLocal ignoreUpdateListenerTl = ThreadLocal.withInitial(() -> Boolean.FALSE); + + + /** + * 设置 InsertListenerThreadLocal + * @param tl ThreadLocal + */ + public static synchronized void setInsertListenerTl(ThreadLocal tl) { + ignoreInsertListenerTl = tl; + } + + /** + * 设置 UpdateListenerThreadLocal + * @param tl ThreadLocal + */ + public static synchronized void setUpdateListenerTl(ThreadLocal tl) { + ignoreUpdateListenerTl = tl; + } + + /** + * 是否执行 InsertListener + * @return 是否执行 + */ + public static boolean isDoInsertListener() { + return !ignoreInsertListenerTl.get(); + } + + /** + * 是否执行 UpdateListener + * @return 是否执行 + */ + public static boolean isDoUpdateListener() { + return !ignoreUpdateListenerTl.get(); + } + + /** + * 忽略 Listener + */ + public static T withoutListener(Supplier supplier) { + try { + ignoreListener(); + return supplier.get(); + } finally { + restoreListener(); + } + } + + /** + * 忽略 Listener + */ + public static void withoutListener(Runnable runnable) { + try { + ignoreListener(); + runnable.run(); + } finally { + restoreListener(); + } + } + + + /** + * 忽略 Listener + */ + public static void ignoreListener() { + ignoreInsertListenerTl.set(Boolean.TRUE); + ignoreUpdateListenerTl.set(Boolean.TRUE); + } + + + /** + * 恢复 Listener + */ + public static void restoreListener() { + ignoreInsertListenerTl.remove(); + ignoreUpdateListenerTl.remove(); + } + + + + /** + * 忽略 InsertListener + */ + public static T withoutInsertListener(Supplier supplier) { + try { + ignoreInsertListener(); + return supplier.get(); + } finally { + restoreInsertListener(); + } + } + + /** + * 忽略 InsertListener + */ + public static void withoutInsertListener(Runnable runnable) { + try { + ignoreInsertListener(); + runnable.run(); + } finally { + restoreInsertListener(); + } + } + + + /** + * 忽略 InsertListener + */ + public static void ignoreInsertListener() { + ignoreInsertListenerTl.set(Boolean.TRUE); + } + + + /** + * 恢复 InsertListener + */ + public static void restoreInsertListener() { + ignoreInsertListenerTl.remove(); + } + + + + + /** + * 忽略 UpdateListener + */ + public static T withoutUpdateListener(Supplier supplier) { + try { + ignoreUpdateListener(); + return supplier.get(); + } finally { + restoreUpdateListener(); + } + } + + /** + * 忽略 UpdateListener + */ + public static void withoutUpdateListener(Runnable runnable) { + try { + ignoreUpdateListener(); + runnable.run(); + } finally { + restoreUpdateListener(); + } + } + + + /** + * 忽略 UpdateListener + */ + public static void ignoreUpdateListener() { + ignoreUpdateListenerTl.set(Boolean.TRUE); + } + + + /** + * 恢复 UpdateListener + */ + public static void restoreUpdateListener() { + ignoreUpdateListenerTl.remove(); + } +} diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java index 16cd82b..2379dee 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java @@ -5,6 +5,7 @@ import cn.hutool.http.HttpStatus; import com.mybatisflex.annotation.InsertListener; import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.orm.core.domain.BaseEntity; +import com.ruoyi.common.orm.helper.ListenerManager; import com.ruoyi.common.security.utils.LoginHelper; import java.util.Date; @@ -19,7 +20,7 @@ public class EntityInsertListener implements InsertListener { @Override public void onInsert(Object entity) { try { - if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) { + if (ListenerManager.isDoInsertListener() && ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) { BaseEntity baseEntity = (BaseEntity) entity; Long loginUserId = LoginHelper.getUserId(); diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java index 3b42383..ba72e05 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java @@ -5,6 +5,7 @@ import cn.hutool.http.HttpStatus; import com.mybatisflex.annotation.UpdateListener; import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.orm.core.domain.BaseEntity; +import com.ruoyi.common.orm.helper.ListenerManager; import com.ruoyi.common.security.utils.LoginHelper; import java.util.Date; @@ -18,7 +19,7 @@ public class EntityUpdateListener implements UpdateListener { @Override public void onUpdate(Object entity) { try { - if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) { + if (ListenerManager.isDoUpdateListener() && ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) { BaseEntity baseEntity = (BaseEntity) entity; baseEntity.setUpdateBy(LoginHelper.getUserId()); baseEntity.setUpdateTime(new Date());