From 1b67bc455ba0695713f977f46ccc67a444e9b871 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Wed, 22 Feb 2023 17:59:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E5=8A=9F=E8=83=BD=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E9=99=84=E4=BB=B6=E6=B2=A1=E6=9C=89=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E5=88=B0=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1023259 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001023259 --- .../metersphere/log/aspect/MsLogAspect.java | 4 +- .../controller/AttachmentController.java | 5 ++ .../service/AttachmentService.java | 80 +++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/log/aspect/MsLogAspect.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/log/aspect/MsLogAspect.java index 1b152fe30e..9f43ccf9d5 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/log/aspect/MsLogAspect.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/log/aspect/MsLogAspect.java @@ -21,7 +21,7 @@ import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.core.LocalVariableTableParameterNameDiscoverer; +import org.springframework.core.StandardReflectionParameterNameDiscoverer; import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; @@ -50,7 +50,7 @@ public class MsLogAspect { /** * 将方法参数纳入Spring管理 */ - LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); + StandardReflectionParameterNameDiscoverer discoverer = new StandardReflectionParameterNameDiscoverer(); @Autowired private ApplicationContext applicationContext; diff --git a/test-track/backend/src/main/java/io/metersphere/controller/AttachmentController.java b/test-track/backend/src/main/java/io/metersphere/controller/AttachmentController.java index d53d981193..8a94333c4c 100644 --- a/test-track/backend/src/main/java/io/metersphere/controller/AttachmentController.java +++ b/test-track/backend/src/main/java/io/metersphere/controller/AttachmentController.java @@ -1,6 +1,9 @@ package io.metersphere.controller; import io.metersphere.base.domain.FileAttachmentMetadata; +import io.metersphere.commons.constants.OperLogConstants; +import io.metersphere.commons.constants.OperLogModule; +import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.request.attachment.AttachmentDumpRequest; import io.metersphere.xpack.track.dto.AttachmentRequest; @@ -27,6 +30,7 @@ public class AttachmentController { @Resource private FileMetadataService fileMetadataService; + @MsAuditLog(module = OperLogModule.TRACK_TEST_CASE, type = OperLogConstants.UPDATE, title = "#request.belongType", content = "#msClass.getLogDetails(#request.belongId, #request.belongType, #file.getOriginalFilename(), false)", msClass = AttachmentService.class) @PostMapping(value = "/upload", consumes = {"multipart/form-data"}) public void uploadAttachment(@RequestPart("request") AttachmentRequest request, @RequestPart(value = "file", required = false) MultipartFile file) { attachmentService.uploadAttachment(request, file); @@ -57,6 +61,7 @@ public class AttachmentController { } } + @MsAuditLog(module = OperLogModule.TRACK_TEST_CASE, type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#attachmentId, #attachmentType)", title = "#request.belongType", msClass = AttachmentService.class) @GetMapping("/delete/{attachmentType}/{attachmentId}") public void deleteAttachment(@PathVariable String attachmentId, @PathVariable String attachmentType) { attachmentService.deleteAttachment(attachmentId, attachmentType); diff --git a/test-track/backend/src/main/java/io/metersphere/service/AttachmentService.java b/test-track/backend/src/main/java/io/metersphere/service/AttachmentService.java index 6ec3b86bb9..97d74511be 100644 --- a/test-track/backend/src/main/java/io/metersphere/service/AttachmentService.java +++ b/test-track/backend/src/main/java/io/metersphere/service/AttachmentService.java @@ -7,9 +7,12 @@ import io.metersphere.commons.constants.FileAssociationType; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.FileUtils; +import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.constants.AttachmentType; import io.metersphere.i18n.Translator; +import io.metersphere.log.vo.DetailColumn; +import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.utils.MetadataUtils; import io.metersphere.platform.domain.SyncIssuesAttachmentRequest; @@ -578,4 +581,81 @@ public class AttachmentService { return type.toUpperCase(); } + /** + * 上传文件的操作记录 + * + * @param sourceId + * @param type + * @return + */ + public String getLogDetails(String sourceId, String type, String fileName, Boolean isDelete) { + String projectId = null; + String createUser = null; + if (StringUtils.isBlank(sourceId) || StringUtils.isBlank(type) || StringUtils.isBlank(fileName)) { + return null; + } + if (AttachmentType.ISSUE.type().equals(type)) { + IssuesWithBLOBs issues = issuesMapper.selectByPrimaryKey(sourceId); + if (issues == null) { + return null; + } + projectId = issues.getProjectId(); + createUser = issues.getCreator(); + } else if (AttachmentType.TEST_CASE.type().equals(type)) { + TestCaseWithBLOBs testCase = testCaseMapper.selectByPrimaryKey(sourceId); + if (testCase == null) { + return null; + } + projectId = testCase.getProjectId(); + createUser = testCase.getCreateUser(); + } + AttachmentRequest attachmentRequest = new AttachmentRequest(); + attachmentRequest.setBelongId(sourceId); + attachmentRequest.setBelongType(type); + List originFiles = listMetadata(attachmentRequest); + List fileNames = new LinkedList<>(); + if (CollectionUtils.isNotEmpty(originFiles)) { + fileNames = originFiles.stream().map(FileAttachmentMetadata::getName).collect(Collectors.toList()); + } + String after; + String before; + + if (fileNames.contains(fileName)) { + after = String.join(",", fileNames); + fileNames.remove(fileName); + before = String.join(",", fileNames); + } else { + after = String.join(",", fileNames); + fileNames.add(fileName); + before = String.join(",", fileNames); + } + + List columns = new ArrayList<>(); + if (isDelete) { + DetailColumn column = new DetailColumn("附件", "files", after, before); + columns.add(column); + } else { + DetailColumn column = new DetailColumn("附件", "files", before, after); + columns.add(column); + } + OperatingLogDetails details = new OperatingLogDetails(JSON.toJSONString(sourceId), projectId, createUser, columns); + return JSON.toJSONString(details); + } + + public String getLogDetails(String attachmentId, String attachmentType) { + FileAttachmentMetadata fileAttachmentMetadata = fileAttachmentMetadataMapper.selectByPrimaryKey(attachmentId); + if (fileAttachmentMetadata == null) { + return null; + } + String fileName = fileAttachmentMetadata.getName(); + AttachmentModuleRelationExample example = new AttachmentModuleRelationExample(); + example.createCriteria().andAttachmentIdEqualTo(attachmentId).andRelationTypeEqualTo(attachmentType); + List attachmentModuleRelations = attachmentModuleRelationMapper.selectByExample(example); + if (CollectionUtils.isEmpty(attachmentModuleRelations)) { + return null; + } + String relationId = attachmentModuleRelations.get(0).getRelationId(); + return this.getLogDetails(relationId, attachmentType, fileName, true); + } + }