From 719a13786e81010161e4e09a467c2ed8a712f2eb Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Tue, 23 Jul 2024 15:14:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E7=94=A8=E4=BE=8B=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/FunctionalCaseService.java | 73 ++++++++++++------- .../FunctionalCaseControllerTests.java | 2 - 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseService.java index 5554d49924..1125159120 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseService.java @@ -32,10 +32,7 @@ import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.file.FileCenter; import io.metersphere.sdk.file.FileCopyRequest; import io.metersphere.sdk.file.FileRepository; -import io.metersphere.sdk.util.BeanUtils; -import io.metersphere.sdk.util.JSON; -import io.metersphere.sdk.util.LogUtils; -import io.metersphere.sdk.util.Translator; +import io.metersphere.sdk.util.*; import io.metersphere.system.domain.CustomFieldOption; import io.metersphere.system.domain.OperationHistoryExample; import io.metersphere.system.domain.User; @@ -63,7 +60,6 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -425,21 +421,21 @@ public class FunctionalCaseService { } //获取评论总数量数量 - ListcommentList = new ArrayList<>(); + List commentList = new ArrayList<>(); FunctionalCaseCommentExample functionalCaseCommentExample = new FunctionalCaseCommentExample(); functionalCaseCommentExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId()); long caseComment = functionalCaseCommentMapper.countByExample(functionalCaseCommentExample); OptionDTO caseOption = new OptionDTO(); caseOption.setId("caseComment"); caseOption.setName(String.valueOf(caseComment)); - commentList.add(0,caseOption); + commentList.add(0, caseOption); CaseReviewHistoryExample caseReviewHistoryExample = new CaseReviewHistoryExample(); caseReviewHistoryExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId()); long reviewComment = caseReviewHistoryMapper.countByExample(caseReviewHistoryExample); OptionDTO reviewOption = new OptionDTO(); reviewOption.setId("reviewComment"); reviewOption.setName(String.valueOf(reviewComment)); - commentList.add(1,reviewOption); + commentList.add(1, reviewOption); //获取关联测试计划的执行评论数量 TestPlanCaseExecuteHistoryExample testPlanCaseExecuteHistoryExample = new TestPlanCaseExecuteHistoryExample(); testPlanCaseExecuteHistoryExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId()); @@ -447,7 +443,7 @@ public class FunctionalCaseService { OptionDTO executeOption = new OptionDTO(); executeOption.setId("executiveComment"); executeOption.setName(String.valueOf(testPlanExecuteComment)); - commentList.add(2,executeOption); + commentList.add(2, executeOption); functionalCaseDetailDTO.setCommentList(commentList); long commentCount = caseComment + reviewComment + testPlanExecuteComment; functionalCaseDetailDTO.setCommentCount((int) commentCount); @@ -503,7 +499,7 @@ public class FunctionalCaseService { if (StringUtils.equalsAnyIgnoreCase(item.getType(), CustomFieldType.MEMBER.name(), CustomFieldType.MULTIPLE_MEMBER.name())) { item.setOptions(memberCustomOption); } - FunctionalCaseCustomField caseCustomField = customFieldMap.get(item.getFieldId()); + FunctionalCaseCustomField caseCustomField = customFieldMap.get(item.getFieldId()); Optional.ofNullable(caseCustomField).ifPresentOrElse(customField -> { item.setDefaultValue(customField.getValue()); if (Translator.get("custom_field.functional_priority").equals(item.getFieldName())) { @@ -607,23 +603,23 @@ public class FunctionalCaseService { functionalCaseBlob.setId(request.getId()); boolean hasUpdate = false; if (request.getSteps() != null) { - hasUpdate=true; + hasUpdate = true; functionalCaseBlob.setSteps(StringUtils.defaultIfEmpty(request.getSteps(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); } - if (request.getTextDescription()!=null) { - hasUpdate=true; + if (request.getTextDescription() != null) { + hasUpdate = true; functionalCaseBlob.setTextDescription(StringUtils.defaultIfEmpty(request.getTextDescription(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); } - if (request.getExpectedResult()!=null) { - hasUpdate=true; + if (request.getExpectedResult() != null) { + hasUpdate = true; functionalCaseBlob.setExpectedResult(StringUtils.defaultIfEmpty(request.getExpectedResult(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); } - if (request.getPrerequisite()!=null) { - hasUpdate=true; + if (request.getPrerequisite() != null) { + hasUpdate = true; functionalCaseBlob.setPrerequisite(StringUtils.defaultIfEmpty(request.getPrerequisite(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); } - if (request.getDescription()!=null) { - hasUpdate=true; + if (request.getDescription() != null) { + hasUpdate = true; functionalCaseBlob.setDescription(StringUtils.defaultIfEmpty(request.getDescription(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); } if (hasUpdate) { @@ -823,7 +819,6 @@ public class FunctionalCaseService { * @param request request * @param userId userId */ - @Async public void batchCopyFunctionalCase(FunctionalCaseBatchMoveRequest request, String userId, String organizationId) { List ids = doSelectIds(request, request.getProjectId()); if (CollectionUtils.isNotEmpty(ids)) { @@ -840,6 +835,13 @@ public class FunctionalCaseService { AtomicReference nextOrder = new AtomicReference<>(getNextOrder(request.getProjectId())); + List addList = new ArrayList<>(); + List addBlobList = new ArrayList<>(); + List addAttachMentList = new ArrayList<>(); + List addCustomFieldList = new ArrayList<>(); + Map> addFileAssociationMap = new HashMap<>(); + Map addLogMap = new HashMap<>(); + for (String s : ids) { String id = IDGenerator.nextStr(); FunctionalCase functionalCase = functionalCaseMap.get(s); @@ -862,10 +864,10 @@ public class FunctionalCaseService { functional.setUpdateUser(userId); functional.setCreateTime(System.currentTimeMillis()); functional.setUpdateTime(System.currentTimeMillis()); - functionalCaseMapper.insert(functional); + addList.add(functional); functionalCaseBlob.setId(id); - functionalCaseBlobMapper.insert(functionalCaseBlob); + addBlobList.add(functionalCaseBlob); nextOrder.updateAndGet(v -> v + ServiceUtils.POS_STEP); }); @@ -875,20 +877,20 @@ public class FunctionalCaseService { attachment.setCaseId(id); attachment.setCreateUser(userId); attachment.setCreateTime(System.currentTimeMillis()); + addAttachMentList.add(attachment); }); - functionalCaseAttachmentService.batchSaveAttachment(caseAttachments); } if (CollectionUtils.isNotEmpty(customFields)) { customFields.forEach(customField -> { customField.setCaseId(id); + addCustomFieldList.add(customField); }); - functionalCaseCustomFieldService.batchSaveCustomField(customFields); } if (CollectionUtils.isNotEmpty(fileAssociationList)) { List fileIds = fileAssociationList.stream().map(FileAssociation::getFileId).collect(Collectors.toList()); - functionalCaseAttachmentService.association(fileIds, id, userId, FUNCTIONAL_CASE_BATCH_COPY_FILE_LOG_URL, request.getProjectId()); + addFileAssociationMap.put(id, fileIds); } //日志 @@ -898,8 +900,27 @@ public class FunctionalCaseService { historyLogDTO.setCustomFields(customFields); historyLogDTO.setCaseAttachments(caseAttachments); historyLogDTO.setFileAssociationList(fileAssociationList); - saveAddDataLog(functionalCase, new FunctionalCaseHistoryLogDTO(), historyLogDTO, userId, organizationId, OperationLogType.ADD.name(), OperationLogModule.FUNCTIONAL_CASE); + addLogMap.put(functionalCase, historyLogDTO); } + SubListUtils.dealForSubList(addList, 500, subList -> { + functionalCaseMapper.batchInsert(subList); + }); + SubListUtils.dealForSubList(addBlobList, 500, subList -> { + functionalCaseBlobMapper.batchInsert(addBlobList); + }); + SubListUtils.dealForSubList(addAttachMentList, 500, subList -> { + functionalCaseAttachmentService.batchSaveAttachment(addAttachMentList); + }); + SubListUtils.dealForSubList(addCustomFieldList, 500, subList -> { + functionalCaseCustomFieldService.batchSaveCustomField(addCustomFieldList); + }); + addFileAssociationMap.entrySet().forEach(entry -> { + functionalCaseAttachmentService.association(entry.getValue(), entry.getKey(), userId, FUNCTIONAL_CASE_BATCH_COPY_FILE_LOG_URL, request.getProjectId()); + }); + addLogMap.entrySet().forEach(entry -> { + saveAddDataLog(entry.getKey(), new FunctionalCaseHistoryLogDTO(), entry.getValue(), userId, organizationId, OperationLogType.ADD.name(), OperationLogModule.FUNCTIONAL_CASE); + }); + User user = userMapper.selectByPrimaryKey(userId); functionalCaseNoticeService.batchSendNotice(request.getProjectId(), ids, user, NoticeConstants.Event.CREATE); diff --git a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java index ad7cff4b6b..f144c71eb6 100644 --- a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java +++ b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java @@ -615,8 +615,6 @@ public class FunctionalCaseControllerTests extends BaseTest { request.setModuleId("TEST_MOVE_MODULE_ID"); request.setSelectAll(false); this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_COPY_URL, request); - request.setSelectIds(Arrays.asList("TEST")); - this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_COPY_URL, request); request.setSelectIds(new ArrayList<>()); request.setSelectAll(true); this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_COPY_URL, request);