!9 新增ListenerManager用以任务调度、三方接口回调等可自由控制审计字段

Merge pull request !9 from Ice/master
This commit is contained in:
数据小王子 2024-07-01 01:31:43 +00:00 committed by Gitee
commit 384a3f9b92
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 187 additions and 2 deletions

View File

@ -0,0 +1,183 @@
package com.ruoyi.common.orm.helper;
import lombok.extern.slf4j.Slf4j;
import java.util.function.Supplier;
/**
* 监听器管理
* <p>
* 考虑任务调度三方接口回调等可自由控制审计字段
*
* @author Ice
* @version 1.0
*/
@Slf4j
public class ListenerManager {
private ListenerManager() {}
private static ThreadLocal<Boolean> ignoreInsertListenerTl = ThreadLocal.withInitial(() -> Boolean.FALSE);
private static ThreadLocal<Boolean> ignoreUpdateListenerTl = ThreadLocal.withInitial(() -> Boolean.FALSE);
/**
* 设置 InsertListenerThreadLocal
* @param tl ThreadLocal
*/
public static synchronized void setInsertListenerTl(ThreadLocal<Boolean> tl) {
ignoreInsertListenerTl = tl;
}
/**
* 设置 UpdateListenerThreadLocal
* @param tl ThreadLocal
*/
public static synchronized void setUpdateListenerTl(ThreadLocal<Boolean> 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> T withoutListener(Supplier<T> 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> T withoutInsertListener(Supplier<T> 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> T withoutUpdateListener(Supplier<T> 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();
}
}

View File

@ -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();

View File

@ -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());