From 41e0b1702d5065f617065c766b4a7be6b38d4fe3 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 18 Jul 2023 18:02:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B7=BB=E5=8A=A0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=88=9B=E5=BB=BA=E7=94=A8=E6=88=B7=E5=92=8C=E8=B5=84?= =?UTF-8?q?=E6=BA=90id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/log/aspect/OperationLogAspect.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/aspect/OperationLogAspect.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/aspect/OperationLogAspect.java index 2c66325c74..36fb3d716f 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/aspect/OperationLogAspect.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/aspect/OperationLogAspect.java @@ -57,6 +57,10 @@ public class OperationLogAspect { private ThreadLocal localUser = new ThreadLocal<>(); + private ThreadLocal localOrganizationId = new ThreadLocal<>(); + + private ThreadLocal localProjectId = new ThreadLocal<>(); + // 此方法随时补充类型,需要在内容变更前执行的类型都可以加入 private final OperationLogType[] beforeMethodNames = new OperationLogType[]{OperationLogType.UPDATE, OperationLogType.DELETE}; // 需要后置执行合并内容的 @@ -73,6 +77,8 @@ public class OperationLogAspect { public void handleException(Exception ex) { localUser.remove(); beforeValues.remove(); + localOrganizationId.remove(); + localProjectId.remove(); LogUtils.error(ex); } @@ -80,6 +86,9 @@ public class OperationLogAspect { public void before(JoinPoint joinPoint) { try { localUser.set(SessionUtils.getUserId()); + localOrganizationId.set(SessionUtils.getCurrentOrganizationId()); + localProjectId.set(SessionUtils.getCurrentProjectId()); + //从切面织入点处通过反射机制获取织入点处的方法 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //获取切入点所在的方法 @@ -209,16 +218,16 @@ public class OperationLogAspect { if (CollectionUtils.isEmpty(logDTOList)) { return; } + logDTOList.forEach(logDTO -> { + logDTO.setSourceId(StringUtils.defaultIfBlank(logDTO.getSourceId(), getId(result))); + logDTO.setCreateUser(StringUtils.defaultIfBlank(logDTO.getCreateUser(), localUser.get())); + logDTO.setOrganizationId(StringUtils.defaultIfBlank(logDTO.getOrganizationId(), localOrganizationId.get())); + logDTO.setProjectId(StringUtils.defaultIfBlank(logDTO.getProjectId(), localProjectId.get())); + }); + // 单条存储 if (logDTOList.size() == 1) { - LogDTO logDTO = logDTOList.get(0); - if (StringUtils.isBlank(logDTO.getSourceId())) { - logDTO.setSourceId(getId(result)); - } - if (StringUtils.isBlank(logDTO.getCreateUser())) { - logDTO.setCreateUser(localUser.get()); - } - operationLogService.add(logDTO); + operationLogService.add(logDTOList.get(0)); } else { operationLogService.batchAdd(logDTOList); }