add:ListenerManager,用以任务调度及三方接口回调情况下自由控制审计字段(目前会因拿不到上下文用户信息将自填充的审计字段改为null值)

This commit is contained in:
Ice 2024-06-27 18:24:05 +08:00
parent ee3948d8e3
commit 1e9abcb3f5
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.mybatisflex.annotation.InsertListener;
import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.orm.core.domain.BaseEntity; import com.ruoyi.common.orm.core.domain.BaseEntity;
import com.ruoyi.common.orm.helper.ListenerManager;
import com.ruoyi.common.security.utils.LoginHelper; import com.ruoyi.common.security.utils.LoginHelper;
import java.util.Date; import java.util.Date;
@ -19,7 +20,7 @@ public class EntityInsertListener implements InsertListener {
@Override @Override
public void onInsert(Object entity) { public void onInsert(Object entity) {
try { try {
if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) { if (ListenerManager.isDoInsertListener() && ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) {
BaseEntity baseEntity = (BaseEntity) entity; BaseEntity baseEntity = (BaseEntity) entity;
Long loginUserId = LoginHelper.getUserId(); Long loginUserId = LoginHelper.getUserId();

View File

@ -5,6 +5,7 @@ import cn.hutool.http.HttpStatus;
import com.mybatisflex.annotation.UpdateListener; import com.mybatisflex.annotation.UpdateListener;
import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.orm.core.domain.BaseEntity; import com.ruoyi.common.orm.core.domain.BaseEntity;
import com.ruoyi.common.orm.helper.ListenerManager;
import com.ruoyi.common.security.utils.LoginHelper; import com.ruoyi.common.security.utils.LoginHelper;
import java.util.Date; import java.util.Date;
@ -18,7 +19,7 @@ public class EntityUpdateListener implements UpdateListener {
@Override @Override
public void onUpdate(Object entity) { public void onUpdate(Object entity) {
try { try {
if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) { if (ListenerManager.isDoUpdateListener() && ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) {
BaseEntity baseEntity = (BaseEntity) entity; BaseEntity baseEntity = (BaseEntity) entity;
baseEntity.setUpdateBy(LoginHelper.getUserId()); baseEntity.setUpdateBy(LoginHelper.getUserId());
baseEntity.setUpdateTime(new Date()); baseEntity.setUpdateTime(new Date());