|
@@ -0,0 +1,126 @@
|
|
|
+package com.hys.app.framework.util;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class RemoveOperationAspect {
|
|
|
+
|
|
|
+ private final ObjectMapper objectMapper;
|
|
|
+
|
|
|
+ public RemoveOperationAspect() {
|
|
|
+ this.objectMapper = new ObjectMapper();
|
|
|
+ // 设置美化输出
|
|
|
+ this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
+ // 空值也输出
|
|
|
+ this.objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String toJsonString(Object obj) {
|
|
|
+ try {
|
|
|
+ return obj != null ? objectMapper.writeValueAsString(obj) : "null";
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("JSON序列化失败: {}", e.getMessage());
|
|
|
+ return obj != null ? obj.toString() : "null";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Around("execution(* com.baomidou.mybatisplus.extension.service.IService+.remove*(..)) || " +
|
|
|
+ "execution(* com.hys.app..*.remove*(..)) || " +
|
|
|
+ "execution(* com.hys.app..*.delete*(..))")
|
|
|
+ public Object logRemoveOperation(ProceedingJoinPoint point) throws Throwable {
|
|
|
+ String className = point.getTarget().getClass().getSimpleName();
|
|
|
+ String methodName = point.getSignature().getName();
|
|
|
+ Object[] args = point.getArgs();
|
|
|
+
|
|
|
+ // 获取调用者信息
|
|
|
+ StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
|
|
+ String caller = "未知";
|
|
|
+ for (StackTraceElement element : stackTrace) {
|
|
|
+ if (!element.getClassName().contains("$Proxy")
|
|
|
+ && !element.getClassName().contains("RemoveOperationAspect")
|
|
|
+ && element.getClassName().startsWith("com.hys.app")) {
|
|
|
+ caller = element.getClassName() + "." + element.getMethodName() + ":" + element.getLineNumber();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (point.getTarget() instanceof IService) {
|
|
|
+ IService<?> service = (IService<?>) point.getTarget();
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ BaseMapper<Object> baseMapper = (BaseMapper<Object>) service.getBaseMapper();
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (args.length > 0 && args[0] != null) {
|
|
|
+ if (args[0] instanceof LambdaQueryWrapper) {
|
|
|
+ // 如果是条件删除
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ LambdaQueryWrapper<Object> wrapper = (LambdaQueryWrapper<Object>) args[0];
|
|
|
+ List<Object> dataList = baseMapper.selectList(wrapper);
|
|
|
+
|
|
|
+ // 获取SQL和参数
|
|
|
+ String sqlSegment = wrapper.getCustomSqlSegment();
|
|
|
+ Map<String, Object> paramNameValuePairs = wrapper.getParamNameValuePairs();
|
|
|
+
|
|
|
+ log.info("\n删除前的数据 - \n调用者: {}\n类: {}\n方法: {}\nSQL条件: {}\n参数: {}\n数据: {}",
|
|
|
+ caller,
|
|
|
+ className,
|
|
|
+ methodName,
|
|
|
+ sqlSegment,
|
|
|
+ toJsonString(paramNameValuePairs),
|
|
|
+ toJsonString(dataList));
|
|
|
+
|
|
|
+ } else if (args[0] instanceof Collection) {
|
|
|
+ // 如果是批量删除
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Collection<? extends Serializable> ids = (Collection<? extends Serializable>) args[0];
|
|
|
+ List<Object> dataList = baseMapper.selectBatchIds(ids);
|
|
|
+ log.info("\n删除前的数据 - \n调用者: {}\n类: {}\n方法: {}\nID列表: {}\n数据: {}",
|
|
|
+ caller,
|
|
|
+ className,
|
|
|
+ methodName,
|
|
|
+ toJsonString(ids),
|
|
|
+ toJsonString(dataList));
|
|
|
+
|
|
|
+ } else if (args[0] instanceof Serializable) {
|
|
|
+ // 如果是单个ID删除
|
|
|
+ Serializable id = (Serializable) args[0];
|
|
|
+ Object data = baseMapper.selectById(id);
|
|
|
+ if (data != null) {
|
|
|
+ log.info("\n删除前的数据 - \n调用者: {}\n类: {}\n方法: {}\nID: {}\n数据: {}",
|
|
|
+ caller,
|
|
|
+ className,
|
|
|
+ methodName,
|
|
|
+ id,
|
|
|
+ toJsonString(data));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("\n记录删除数据失败 - \n调用者: {}\n类: {}\n方法: {}\n异常: {}",
|
|
|
+ caller,
|
|
|
+ className,
|
|
|
+ methodName,
|
|
|
+ e.getMessage(),
|
|
|
+ e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return point.proceed();
|
|
|
+ }
|
|
|
+}
|