From e79f47611e1abf3f28813b6e9d75ec48c5d4d502 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Mon, 4 Dec 2023 10:58:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=8A=9F=E8=83=BD=E7=94=A8=E4=BE=8B):?= =?UTF-8?q?=20=E7=94=A8=E4=BE=8B=E5=8F=98=E6=9B=B4=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/FunctionalCaseHistoryLogDTO.java | 36 +++++++++++ .../service/FunctionalCaseLogService.java | 60 ++++++++++++++----- .../io/metersphere/system/log/dto/LogDTO.java | 3 + .../log/service/OperationLogService.java | 8 ++- .../OperationLogControllerTests.java | 20 +++++++ 5 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalCaseHistoryLogDTO.java diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalCaseHistoryLogDTO.java b/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalCaseHistoryLogDTO.java new file mode 100644 index 0000000000..65442225d1 --- /dev/null +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalCaseHistoryLogDTO.java @@ -0,0 +1,36 @@ +package io.metersphere.functional.dto; + +import io.metersphere.functional.domain.FunctionalCase; +import io.metersphere.functional.domain.FunctionalCaseAttachment; +import io.metersphere.functional.domain.FunctionalCaseBlob; +import io.metersphere.functional.domain.FunctionalCaseCustomField; +import io.metersphere.project.domain.FileAssociation; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; + +@Data +public class FunctionalCaseHistoryLogDTO implements Serializable { + @Serial + private static final long serialVersionUID = 1L; + + private FunctionalCase functionalCase; + + private FunctionalCaseBlob functionalCaseBlob; + + private List customFields; + + private List caseAttachments; + + private List fileAssociationList; + + public FunctionalCaseHistoryLogDTO(FunctionalCase functionalCase, FunctionalCaseBlob functionalCaseBlob, List customFields, List caseAttachments, List fileAssociationList) { + this.functionalCase = functionalCase; + this.functionalCaseBlob = functionalCaseBlob; + this.customFields = customFields; + this.caseAttachments = caseAttachments; + this.fileAssociationList = fileAssociationList; + } +} diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseLogService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseLogService.java index 40eb9b1304..13ac287238 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseLogService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseLogService.java @@ -1,18 +1,18 @@ package io.metersphere.functional.service; -import io.metersphere.functional.domain.FunctionalCase; -import io.metersphere.functional.domain.FunctionalCaseDemand; +import io.metersphere.functional.domain.*; import io.metersphere.functional.dto.BaseFunctionalCaseBatchDTO; -import io.metersphere.functional.mapper.ExtFunctionalCaseMapper; -import io.metersphere.functional.mapper.FunctionalCaseDemandMapper; -import io.metersphere.functional.mapper.FunctionalCaseMapper; +import io.metersphere.functional.dto.FunctionalCaseHistoryLogDTO; +import io.metersphere.functional.mapper.*; import io.metersphere.functional.request.*; +import io.metersphere.project.domain.FileAssociation; +import io.metersphere.project.domain.FileAssociationExample; +import io.metersphere.project.mapper.FileAssociationMapper; import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.util.JSON; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.dto.LogDTO; -import io.metersphere.system.log.service.OperationLogService; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; @@ -31,15 +31,20 @@ public class FunctionalCaseLogService { @Resource private FunctionalCaseMapper functionalCaseMapper; - - @Resource - private OperationLogService operationLogService; @Resource private FunctionalCaseService functionalCaseService; @Resource private ExtFunctionalCaseMapper extFunctionalCaseMapper; @Resource private FunctionalCaseDemandMapper functionalCaseDemandMapper; + @Resource + private FunctionalCaseBlobMapper functionalCaseBlobMapper; + @Resource + private FunctionalCaseCustomFieldMapper functionalCaseCustomFieldMapper; + @Resource + private FunctionalCaseAttachmentMapper functionalCaseAttachmentMapper; + @Resource + private FileAssociationMapper fileAssociationMapper; //TODO 日志(需要修改) @@ -60,10 +65,10 @@ public class FunctionalCaseLogService { OperationLogType.ADD.name(), OperationLogModule.FUNCTIONAL_CASE, requests.getName()); - + dto.setHistory(true); dto.setPath("/functional/case/add"); dto.setMethod(HttpMethodConstants.POST.name()); - dto.setOriginalValue(JSON.toJSONBytes(requests)); + dto.setModifiedValue(JSON.toJSONBytes(requests)); return dto; } @@ -76,7 +81,7 @@ public class FunctionalCaseLogService { * @return */ public LogDTO updateFunctionalCaseLog(FunctionalCaseEditRequest requests, List files) { - //TODO 获取原值 + FunctionalCaseHistoryLogDTO historyLogDTO = getOriginalValue(requests.getId()); LogDTO dto = new LogDTO( requests.getProjectId(), null, @@ -85,13 +90,37 @@ public class FunctionalCaseLogService { OperationLogType.UPDATE.name(), OperationLogModule.FUNCTIONAL_CASE, requests.getName()); - + dto.setHistory(true); dto.setPath("/functional/case/update"); dto.setMethod(HttpMethodConstants.POST.name()); dto.setModifiedValue(JSON.toJSONBytes(requests)); + dto.setOriginalValue(JSON.toJSONBytes(historyLogDTO)); return dto; } + private FunctionalCaseHistoryLogDTO getOriginalValue(String id) { + FunctionalCase functionalCase = functionalCaseMapper.selectByPrimaryKey(id); + FunctionalCaseBlob functionalCaseBlob = functionalCaseBlobMapper.selectByPrimaryKey(id); + + //自定义字段 + FunctionalCaseCustomFieldExample fieldExample = new FunctionalCaseCustomFieldExample(); + fieldExample.createCriteria().andCaseIdEqualTo(id); + List customFields = functionalCaseCustomFieldMapper.selectByExample(fieldExample); + + //附件 本地 + 文件库 + FunctionalCaseAttachmentExample attachmentExample = new FunctionalCaseAttachmentExample(); + attachmentExample.createCriteria().andCaseIdEqualTo(id); + List caseAttachments = functionalCaseAttachmentMapper.selectByExample(attachmentExample); + + FileAssociationExample example = new FileAssociationExample(); + example.createCriteria().andSourceIdEqualTo(id); + List fileAssociationList = fileAssociationMapper.selectByExample(example); + + + FunctionalCaseHistoryLogDTO historyLogDTO = new FunctionalCaseHistoryLogDTO(functionalCase, functionalCaseBlob, customFields, caseAttachments, fileAssociationList); + return historyLogDTO; + } + /** * 删除用例 日志 @@ -260,6 +289,7 @@ public class FunctionalCaseLogService { if (CollectionUtils.isNotEmpty(ids)) { List functionalCases = extFunctionalCaseMapper.getLogInfo(ids, false); functionalCases.forEach(functionalCase -> { + FunctionalCaseHistoryLogDTO historyLogDTO = getOriginalValue(functionalCase.getId()); LogDTO dto = new LogDTO( functionalCase.getProjectId(), null, @@ -268,10 +298,10 @@ public class FunctionalCaseLogService { OperationLogType.DELETE.name(), OperationLogModule.FUNCTIONAL_CASE, functionalCase.getName()); - + dto.setHistory(true); dto.setPath("/functional/case/batch/edit"); dto.setMethod(HttpMethodConstants.POST.name()); - dto.setOriginalValue(JSON.toJSONBytes(functionalCase)); + dto.setOriginalValue(JSON.toJSONBytes(historyLogDTO)); dtoList.add(dto); }); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/log/dto/LogDTO.java b/backend/services/system-setting/src/main/java/io/metersphere/system/log/dto/LogDTO.java index bbe16d87a6..ac56d8cd9f 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/log/dto/LogDTO.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/log/dto/LogDTO.java @@ -14,6 +14,9 @@ public class LogDTO extends OperationLog { @Schema(description = "变更后内容") private byte[] modifiedValue; + @Schema(description = "是否需要历史记录") + private Boolean history = false; + public LogDTO() { } public LogDTO(String projectId, String organizationId, String sourceId, String createUser, String type, String module, String content) { diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/log/service/OperationLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/log/service/OperationLogService.java index 38871da1f2..3a4b9fd7c4 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/log/service/OperationLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/log/service/OperationLogService.java @@ -82,7 +82,9 @@ public class OperationLogService { } log.setContent(subStrContent(log.getContent())); operationLogMapper.insert(log); - operationHistoryMapper.insert(getHistory(log)); + if (log.getHistory()) { + operationHistoryMapper.insert(getHistory(log)); + } operationLogBlobMapper.insert(getBlob(log)); } @@ -127,7 +129,9 @@ public class OperationLogService { item.setCreateTime(currentTimeMillis); // 限制长度 operationLogMapper.insert(item); - operationHistoryMapper.insert(getHistory(item)); + if (item.getHistory()) { + operationHistoryMapper.insert(getHistory(item)); + } logBlobMapper.insert(getBlob(item)); }); } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OperationLogControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OperationLogControllerTests.java index 719505a32f..b79cdc80fd 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OperationLogControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OperationLogControllerTests.java @@ -2,8 +2,11 @@ package io.metersphere.system.controller; import io.metersphere.system.base.BaseTest; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.system.log.dto.LogDTO; +import io.metersphere.system.log.service.OperationLogService; import io.metersphere.system.log.vo.OperationLogRequest; import io.metersphere.system.controller.param.OperationLogRequestDefinition; +import jakarta.annotation.Resource; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -14,7 +17,9 @@ import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.ResultActions; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -34,6 +39,8 @@ public class OperationLogControllerTests extends BaseTest { public static final String ORGANIZATION = "ORGANIZATION"; public static final String PROJECT = "PROJECT"; + @Resource + private OperationLogService opreationLogService; /** * 系统级别 查询 用例 @@ -167,4 +174,17 @@ public class OperationLogControllerTests extends BaseTest { } + @Test + @Order(4) + public void testLog() throws Exception { + //增加覆蓋率 + LogDTO logDTO = new LogDTO(DEFAULT_PROJECT_ID,DEFAULT_ORGANIZATION_ID,"test_source_id","admin",DEFAULT_ADD,"SYSTEM","測試"); + logDTO.setHistory(true); + logDTO.setMethod("test"); + opreationLogService.add(logDTO); + + List logDTOList = new ArrayList<>(); + logDTOList.add(logDTO); + opreationLogService.batchAdd(logDTOList); + } }