diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalMinderUpdateDTO.java b/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalMinderUpdateDTO.java new file mode 100644 index 0000000000..03f28317bb --- /dev/null +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/dto/FunctionalMinderUpdateDTO.java @@ -0,0 +1,39 @@ +package io.metersphere.functional.dto; + +import io.metersphere.system.log.dto.LogDTO; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +@Data +public class FunctionalMinderUpdateDTO implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + @Schema(description = "新增日志") + private List addLogDTOS; + + @Schema(description = "更新日志") + private List updateLogDTOS; + + @Schema(description = "新增通知") + private List noticeList; + + @Schema(description = "更新通知") + private List updateNoticeList; + + @Schema(description = "资源Id与相邻点的Map") + private Map sourceIdAndTargetIdsMap; + + public FunctionalMinderUpdateDTO(List addLogDTOS, List updateLogDTOS, List noticeList, List updateNoticeList) { + this.addLogDTOS = addLogDTOS; + this.updateLogDTOS = updateLogDTOS; + this.noticeList = noticeList; + this.updateNoticeList = updateNoticeList; + } +} diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseMinderService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseMinderService.java index ac9823c36e..8d23c266b2 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseMinderService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseMinderService.java @@ -50,6 +50,7 @@ import org.springframework.transaction.annotation.Transactional; import java.nio.charset.StandardCharsets; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -185,7 +186,7 @@ public class FunctionalCaseMinderService { rootData.setId(functionalCaseMindDTO.getId()); rootData.setPos(functionalCaseMindDTO.getPos()); rootData.setText(functionalCaseMindDTO.getName()); - rootData.setPriority(StringUtils.isNotBlank(priorityMap.get(functionalCaseMindDTO.getId())) ? Integer.parseInt(priorityMap.get(functionalCaseMindDTO.getId()).substring(1))+1 : 1); + rootData.setPriority(StringUtils.isNotBlank(priorityMap.get(functionalCaseMindDTO.getId())) ? Integer.parseInt(priorityMap.get(functionalCaseMindDTO.getId()).substring(1)) + 1 : 1); rootData.setStatus(functionalCaseMindDTO.getReviewStatus()); rootData.setResource(List.of(Translator.get("minder_extra_node.case"))); List children = buildChildren(functionalCaseMindDTO); @@ -279,64 +280,339 @@ public class FunctionalCaseMinderService { } public void editFunctionalCaseBatch(FunctionalCaseMinderEditRequest request, String userId) { - //处理删除的模块和用例 + //处理删除的模块和用例和空白节点 deleteResource(request, userId); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - FunctionalCaseMapper caseMapper = sqlSession.getMapper(FunctionalCaseMapper.class); - FunctionalCaseBlobMapper caseBlobMapper = sqlSession.getMapper(FunctionalCaseBlobMapper.class); - FunctionalCaseCustomFieldMapper caseCustomFieldMapper = sqlSession.getMapper(FunctionalCaseCustomFieldMapper.class); FunctionalCaseModuleMapper moduleMapper = sqlSession.getMapper(FunctionalCaseModuleMapper.class); MindAdditionalNodeMapper additionalNodeMapper = sqlSession.getMapper(MindAdditionalNodeMapper.class); - - List addLogDTOS = new ArrayList<>(); - List noticeList = new ArrayList<>(); - List updateNoticeList = new ArrayList<>(); - List updateLogDTOS = new ArrayList<>(); - Map newModuleMap = new HashMap<>(); + FunctionalMinderUpdateDTO functionalMinderUpdateDTO = new FunctionalMinderUpdateDTO(new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); //处理模块 - List moduleNodeIds = dealModule(request, userId, moduleMapper, newModuleMap); - + Map sourceIdAndInsertModuleIdMap = dealModule(request, userId, moduleMapper, functionalMinderUpdateDTO); + List needToTurnModuleIds = sourceIdAndInsertModuleIdMap.keySet().stream().distinct().toList(); //处理用例 - List caseNodeIds = new ArrayList<>(); + Map sourceIdAndInsertCaseIdMap = dealCase(request, userId, sqlSession, sourceIdAndInsertModuleIdMap, functionalMinderUpdateDTO); + List needToTurnCaseIds = new ArrayList<>(sourceIdAndInsertCaseIdMap.keySet().stream().distinct().toList()); + + //删除已转为模块或用例的空白节点 + needToTurnCaseIds.addAll(needToTurnModuleIds); + if (CollectionUtils.isNotEmpty(needToTurnCaseIds)) { + dealMindAdditionalMode(needToTurnCaseIds, additionalNodeMapper); + } + //处理空白节点 + Map sourceIdAndInsertTextIdMap = dealAdditionalNode(request, userId, additionalNodeMapper, sourceIdAndInsertModuleIdMap, functionalMinderUpdateDTO); + + //替换targetId + Map sourceIdAndTargetIdMap = functionalMinderUpdateDTO.getSourceIdAndTargetIdsMap(); + //新的target + Map sourceIdAndInsertTargetIdMap = new HashMap<>(); + sourceIdAndTargetIdMap.forEach((sourceId, targetId) -> { + if (StringUtils.isNotBlank(sourceIdAndInsertModuleIdMap.get(targetId))) { + sourceIdAndInsertTargetIdMap.put(sourceId, sourceIdAndInsertModuleIdMap.get(targetId)); + } else if (StringUtils.isNotBlank(sourceIdAndInsertCaseIdMap.get(targetId))) { + sourceIdAndInsertTargetIdMap.put(sourceId, sourceIdAndInsertCaseIdMap.get(targetId)); + + } else if (StringUtils.isNotBlank(sourceIdAndInsertTextIdMap.get(targetId))) { + sourceIdAndInsertTargetIdMap.put(sourceId, sourceIdAndInsertTextIdMap.get(targetId)); + } else { + sourceIdAndInsertTargetIdMap.put(sourceId, targetId); + } + }); + //排序 + List targetIds = sourceIdAndInsertTargetIdMap.values().stream().distinct().toList(); + if (CollectionUtils.isNotEmpty(targetIds)) { + FunctionalCaseModuleExample functionalCaseModuleExample = new FunctionalCaseModuleExample(); + functionalCaseModuleExample.createCriteria().andIdIn(targetIds); + List targetModuleIds = functionalCaseModuleMapper.selectByExample(functionalCaseModuleExample); + Map targetModuleMap = new HashMap<>(); + List targetModuleParentIds = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(targetModuleIds)) { + targetModuleMap = targetModuleIds.stream().collect(Collectors.toMap(FunctionalCaseModule::getId, FunctionalCaseModule::getParentId)); + targetModuleParentIds = targetModuleIds.stream().map(FunctionalCaseModule::getParentId).distinct().toList(); + } + List allChildrenInDB = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(targetModuleParentIds)) { + functionalCaseModuleExample = new FunctionalCaseModuleExample(); + functionalCaseModuleExample.createCriteria().andParentIdIn(targetModuleParentIds); + allChildrenInDB = functionalCaseModuleMapper.selectByExample(functionalCaseModuleExample); + } + + FunctionalCaseExample functionalCaseExample = new FunctionalCaseExample(); + functionalCaseExample.createCriteria().andIdIn(targetIds); + List functionalCases = functionalCaseMapper.selectByExample(functionalCaseExample); + + Map targetCaseMap = new HashMap<>(); + List targetCaseParentIds = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(functionalCases)) { + targetCaseMap = functionalCases.stream().collect(Collectors.toMap(FunctionalCase::getId, FunctionalCase::getModuleId)); + targetCaseParentIds = new ArrayList<>(functionalCases.stream().map(FunctionalCase::getModuleId).toList()); + } + + targetCaseParentIds.addAll(targetModuleParentIds); + List targetCaseParentIdsNoRepeat = targetCaseParentIds.stream().distinct().toList(); + List allChildrenCaseInDB = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(targetCaseParentIdsNoRepeat)) { + functionalCaseExample = new FunctionalCaseExample(); + functionalCaseExample.createCriteria().andModuleIdIn(targetCaseParentIdsNoRepeat); + allChildrenCaseInDB = functionalCaseMapper.selectByExample(functionalCaseExample); + } + + List finalTargetModuleParentIds = targetModuleParentIds; + List caseModuleIds = targetCaseParentIds.stream().filter(t -> !finalTargetModuleParentIds.contains(t)).distinct().toList(); + List allChildrenByCaseInDB = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(caseModuleIds)) { + functionalCaseModuleExample = new FunctionalCaseModuleExample(); + functionalCaseModuleExample.createCriteria().andParentIdIn(caseModuleIds); + allChildrenByCaseInDB = functionalCaseModuleMapper.selectByExample(functionalCaseModuleExample); + } + allChildrenInDB.addAll(allChildrenByCaseInDB); + + + MindAdditionalNodeExample mindAdditionalNodeExample = new MindAdditionalNodeExample(); + mindAdditionalNodeExample.createCriteria().andIdIn(targetIds); + List mindAdditionalNodes = mindAdditionalNodeMapper.selectByExample(mindAdditionalNodeExample); + Map targetTextMap = new HashMap<>(); + List targetTextParentIds = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(mindAdditionalNodes)) { + targetTextMap = mindAdditionalNodes.stream().collect(Collectors.toMap(MindAdditionalNode::getId, MindAdditionalNode::getParentId)); + //空白节点的父亲可能是空白节点,也可能是模块 + targetTextParentIds = new ArrayList<>(mindAdditionalNodes.stream().map(MindAdditionalNode::getParentId).toList()); + } + + targetTextParentIds.addAll(targetModuleParentIds); + targetTextParentIds.addAll(targetCaseParentIds); + List targetTextParentNoRepeatIds = targetTextParentIds.stream().distinct().toList(); + List allChildrenTextInDB = new ArrayList<>(); + Map> parentChildrenTextMap = new HashMap<>(); + if (CollectionUtils.isNotEmpty(targetTextParentNoRepeatIds)) { + mindAdditionalNodeExample = new MindAdditionalNodeExample(); + mindAdditionalNodeExample.createCriteria().andParentIdIn(targetTextParentNoRepeatIds); + allChildrenTextInDB = mindAdditionalNodeMapper.selectByExample(mindAdditionalNodeExample); + parentChildrenTextMap = allChildrenTextInDB.stream().collect(Collectors.groupingBy(MindAdditionalNode::getParentId)); + } + + List finalTargetModuleParentIds1 = targetModuleParentIds; + List textModuleIds = targetTextParentIds.stream().filter(t -> !finalTargetModuleParentIds1.contains(t)).distinct().toList(); + + if (CollectionUtils.isNotEmpty(textModuleIds)) { + functionalCaseModuleExample = new FunctionalCaseModuleExample(); + functionalCaseModuleExample.createCriteria().andParentIdIn(textModuleIds); + List allChildrenByTextInDB = functionalCaseModuleMapper.selectByExample(functionalCaseModuleExample); + allChildrenInDB.addAll(allChildrenByTextInDB); + } + List textModuleFilterCaseIds = targetTextParentIds.stream().filter(t -> !targetCaseParentIdsNoRepeat.contains(t)).distinct().toList(); + + if (CollectionUtils.isNotEmpty(textModuleFilterCaseIds)) { + functionalCaseExample = new FunctionalCaseExample(); + functionalCaseExample.createCriteria().andModuleIdIn(textModuleFilterCaseIds); + List allChildrenCaseByTextInDB = functionalCaseMapper.selectByExample(functionalCaseExample); + allChildrenCaseInDB.addAll(allChildrenCaseByTextInDB); + } + Map> parentChildrenMap = new HashMap<>(); + if (CollectionUtils.isNotEmpty(allChildrenInDB)) { + parentChildrenMap = allChildrenInDB.stream().collect(Collectors.groupingBy(FunctionalCaseModule::getParentId)); + + } + Map> parentChildrenCaseMap = new HashMap<>(); + if (CollectionUtils.isNotEmpty(allChildrenCaseInDB)) { + parentChildrenCaseMap = allChildrenCaseInDB.stream().collect(Collectors.groupingBy(FunctionalCase::getModuleId)); + } + + //更新模块顺序 + if (CollectionUtils.isNotEmpty(request.getUpdateModuleList())) { + for (FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest : request.getUpdateModuleList()) { + //找出替换后的target + String targetId = sourceIdAndInsertTargetIdMap.get(functionalCaseModuleEditRequest.getId()); + //先看看相邻点是否是模块,不是模块可能是用例,否则是空白节点 + String moduleMapKey = getModuleMapKey(targetModuleMap, targetId, targetCaseMap, targetTextMap); + String moduleId = sourceIdAndInsertModuleIdMap.get(functionalCaseModuleEditRequest.getId()); + if (StringUtils.isBlank(moduleId)) { + moduleId = functionalCaseModuleEditRequest.getId(); + } + List functionalCaseModules = parentChildrenMap.get(moduleMapKey); + if (CollectionUtils.isNotEmpty(functionalCaseModules)) { + List finallyModules = sortMindCases(functionalCaseModuleEditRequest.getMoveMode(), targetId, moduleId, functionalCaseModules, FunctionalCaseModule::getId, FunctionalCaseModule::getPos); + for (int i = 0; i < finallyModules.size(); i++) { + finallyModules.get(i).setPos(LIMIT_POS * i); + moduleMapper.updateByPrimaryKey(finallyModules.get(i)); + } + } + } + } + + //更新用例顺序 + if (CollectionUtils.isNotEmpty(request.getUpdateCaseList())) { + for (FunctionalCaseChangeRequest caseChangeRequest : request.getUpdateCaseList()) { + String caseId = sourceIdAndInsertCaseIdMap.get(caseChangeRequest.getId()); + if (StringUtils.isBlank(caseId)) { + caseId = caseChangeRequest.getId(); + } + //找出替换后的target + String targetId = sourceIdAndInsertTargetIdMap.get(caseChangeRequest.getId()); + //先看看相邻点是否是模块,不是模块可能是用例,否则是空白节点 + String moduleMapKey = getModuleMapKey(targetModuleMap, targetId, targetCaseMap, targetTextMap); + List functionalCasesList = parentChildrenCaseMap.get(moduleMapKey); + if (CollectionUtils.isNotEmpty(functionalCasesList)) { + List finallyCases = sortMindCases(caseChangeRequest.getMoveMode(), targetId, caseId, functionalCasesList, FunctionalCase::getId, FunctionalCase::getPos); + FunctionalCaseMapper caseMapper = sqlSession.getMapper(FunctionalCaseMapper.class); + for (int i = 0; i < finallyCases.size(); i++) { + finallyCases.get(i).setPos(LIMIT_POS * i); + caseMapper.updateByPrimaryKey(finallyCases.get(i)); + } + } + } + } + + //更新空白节点顺序 + if (CollectionUtils.isNotEmpty(request.getAdditionalNodeList())) { + for (MindAdditionalNodeRequest mindAdditionalNodeRequest : request.getAdditionalNodeList()) { + + String textId = sourceIdAndInsertTextIdMap.get(mindAdditionalNodeRequest.getId()); + if (StringUtils.isBlank(textId)) { + textId = mindAdditionalNodeRequest.getId(); + } + //找出替换后的target + String targetId = sourceIdAndInsertTargetIdMap.get(mindAdditionalNodeRequest.getId()); + //先看看相邻点是否是模块,不是模块可能是用例,否则是空白节点 + String moduleMapKey = getModuleMapKey(targetModuleMap, targetId, targetCaseMap, targetTextMap); + List mindAdditionalNodeList = parentChildrenTextMap.get(moduleMapKey); + if (CollectionUtils.isNotEmpty(mindAdditionalNodeList)) { + List finallyNode = sortMindCases(mindAdditionalNodeRequest.getMoveMode(), targetId, textId, mindAdditionalNodeList, MindAdditionalNode::getId, MindAdditionalNode::getPos); + for (int i = 0; i < finallyNode.size(); i++) { + finallyNode.get(i).setPos(LIMIT_POS * i); + additionalNodeMapper.updateByPrimaryKey(finallyNode.get(i)); + } + } + } + } + } + sqlSession.flushStatements(); + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + + dealLogAndNotice(request, userId, functionalMinderUpdateDTO); + } + + /** + * @param moveMode 移动方式 前后 + * @param targetId 要移动到的目标元素标识 + * @param moveId 要移动的元素标识 + * @param sources sources + * @return List + */ + private List sortMindCases(String moveMode, String targetId, String moveId, List sources, Function idExtractor, Function posExtractor) { + int targetIndex = 0; + int nodeIndex = 0; + boolean findTarget = false; + boolean findNode = false; + T currentNode = null; + sources = sources.stream().sorted(Comparator.comparing(posExtractor)).collect(Collectors.toList()); + for (int i = 0; i < sources.size(); i++) { + if (StringUtils.equalsIgnoreCase(targetId, idExtractor.apply(sources.get(i)))) { + targetIndex = i; + findTarget = true; + } + if (StringUtils.equalsIgnoreCase(moveId, idExtractor.apply(sources.get(i)))) { + nodeIndex = i; + findNode = true; + currentNode = sources.get(i); + } + if (findTarget && findNode) { + break; + } + } + sources.remove(nodeIndex); + List beforeNode; + List afterNode; + if (StringUtils.equals(moveMode, MoveTypeEnum.AFTER.name())) { + beforeNode = sources.subList(0, targetIndex + 1); + afterNode = sources.subList(targetIndex + 1, sources.size()); + } else { + beforeNode = sources.subList(0, targetIndex); + afterNode = sources.subList(targetIndex, sources.size()); + } + List finallyNode = new ArrayList<>(beforeNode); + finallyNode.add(currentNode); + finallyNode.addAll(afterNode); + return finallyNode; + } + + private static String getModuleMapKey(Map targetModuleMap, String targetId, Map targetCaseMap, Map targetTextMap) { + String moduleMapKey; + String parentId = targetModuleMap.get(targetId); + String caseModuleId = targetCaseMap.get(targetId); + String textParentId = targetTextMap.get(targetId); + if (StringUtils.isNotBlank(parentId)) { + moduleMapKey = parentId; + } else if (StringUtils.isNotBlank(parentId)) { + moduleMapKey = caseModuleId; + } else { + moduleMapKey = textParentId; + } + return moduleMapKey; + } + + private void dealLogAndNotice(FunctionalCaseMinderEditRequest request, String userId, FunctionalMinderUpdateDTO functionalMinderUpdateDTO) { + Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); + for (LogDTO addLogDTO : functionalMinderUpdateDTO.getAddLogDTOS()) { + addLogDTO.setOrganizationId(project.getOrganizationId()); + } + for (LogDTO updateLogDTO : functionalMinderUpdateDTO.getUpdateLogDTOS()) { + updateLogDTO.setOrganizationId(project.getOrganizationId()); + } + operationLogService.batchAdd(functionalMinderUpdateDTO.getAddLogDTOS()); + operationLogService.batchAdd(functionalMinderUpdateDTO.getUpdateLogDTOS()); + User user = userMapper.selectByPrimaryKey(userId); + List resources = new ArrayList<>(); + resources.addAll(JSON.parseArray(JSON.toJSONString(functionalMinderUpdateDTO.getNoticeList()), Map.class)); + commonNoticeSendService.sendNotice(NoticeConstants.TaskType.FUNCTIONAL_CASE_TASK, NoticeConstants.Event.CREATE, resources, user, request.getProjectId()); + resources = new ArrayList<>(); + resources.addAll(JSON.parseArray(JSON.toJSONString(functionalMinderUpdateDTO.getUpdateNoticeList()), Map.class)); + commonNoticeSendService.sendNotice(NoticeConstants.TaskType.FUNCTIONAL_CASE_TASK, NoticeConstants.Event.UPDATE, resources, user, request.getProjectId()); + } + + private Map dealCase(FunctionalCaseMinderEditRequest request, String userId, SqlSession sqlSession, Map sourceIdAndInsertModuleIdMap, FunctionalMinderUpdateDTO functionalMinderUpdateDTO) { + Map sourceIdAndInsertCaseIdMap = new HashMap<>(); + Map sourceIdAndTargetIdsMap = new HashMap<>(); if (CollectionUtils.isNotEmpty(request.getUpdateCaseList())) { + FunctionalCaseMapper caseMapper = sqlSession.getMapper(FunctionalCaseMapper.class); + FunctionalCaseBlobMapper caseBlobMapper = sqlSession.getMapper(FunctionalCaseBlobMapper.class); + FunctionalCaseCustomFieldMapper caseCustomFieldMapper = sqlSession.getMapper(FunctionalCaseCustomFieldMapper.class); + // 获取页面改动的自定义字段值 + Map customFieldNameMap = getCustomFieldNameMap(request); + //获取自定义字段的默认值 + Map defaultCustomFieldValueMap = getDefaultCustomFieldValueMap(request); Map> resourceMap = request.getUpdateCaseList().stream().collect(Collectors.groupingBy(FunctionalCaseChangeRequest::getType)); //处理新增 - Map customFieldNameMap = getCustomFieldNameMap(request); List addList = resourceMap.get(OperationLogType.ADD.toString()); - List updatePosList = new ArrayList<>(); - Map defaultValueMap = getDefaultValueMap(request); if (CollectionUtils.isNotEmpty(addList)) { - Map> moduleCaseMap = getModuleCaseMap(addList); for (FunctionalCaseChangeRequest functionalCaseChangeRequest : addList) { - FunctionalCase functionalCase = addCase(request, userId, functionalCaseChangeRequest, caseMapper, newModuleMap); + //基本信息 + FunctionalCase functionalCase = addCase(request, userId, functionalCaseChangeRequest, caseMapper, sourceIdAndInsertModuleIdMap); String caseId = functionalCase.getId(); + sourceIdAndInsertCaseIdMap.put(functionalCaseChangeRequest.getId(), caseId); + //附属表 - caseNodeIds.add(caseId); FunctionalCaseBlob functionalCaseBlob = addCaseBlob(functionalCaseChangeRequest, caseId, caseBlobMapper); //保存自定义字段 - List functionalCaseCustomFields = addCustomFields(functionalCaseChangeRequest, caseId, caseCustomFieldMapper, defaultValueMap); - //排序 - reSetMap(functionalCaseChangeRequest, moduleCaseMap, functionalCase); + List functionalCaseCustomFields = addCustomFields(functionalCaseChangeRequest, caseId, caseCustomFieldMapper, defaultCustomFieldValueMap); + //日志 FunctionalCaseHistoryLogDTO historyLogDTO = new FunctionalCaseHistoryLogDTO(functionalCase, functionalCaseBlob, functionalCaseCustomFields, new ArrayList<>(), new ArrayList<>()); - addLog(request, userId, caseId, historyLogDTO, addLogDTOS, null); + LogDTO logDTO = addLog(request, userId, caseId, historyLogDTO, null); + functionalMinderUpdateDTO.getAddLogDTOS().add(logDTO); + //消息通知 FunctionalCaseDTO functionalCaseDTO = getFunctionalCaseDTO(functionalCase, functionalCaseCustomFields, customFieldNameMap); - noticeList.add(functionalCaseDTO); + functionalMinderUpdateDTO.getNoticeList().add(functionalCaseDTO); + updateTargetIdsMap(functionalCaseChangeRequest.getId(), functionalCaseChangeRequest.getTargetId(), sourceIdAndTargetIdsMap); } - moduleCaseMap.forEach((k, v) -> { - updatePosList.addAll(v); - }); } //处理更新 List updateList = resourceMap.get(OperationLogType.UPDATE.toString()); if (CollectionUtils.isNotEmpty(updateList)) { List caseIds = updateList.stream().map(FunctionalCaseChangeRequest::getId).toList(); - FunctionalCaseCustomFieldExample example = new FunctionalCaseCustomFieldExample(); - example.createCriteria().andCaseIdIn(caseIds); - List allFields = functionalCaseCustomFieldMapper.selectByExample(example); - Map> caseCustomFieldMap = allFields.stream().collect(Collectors.groupingBy(FunctionalCaseCustomField::getCaseId)); - Map> moduleCaseMap = getModuleCaseMap(updateList); + //获取已存在的自定义字段值 + Map> oldCaseCustomFieldMap = getOldCaseCustomFieldMap(caseIds); + FunctionalCaseExample functionalCaseExample = new FunctionalCaseExample(); functionalCaseExample.createCriteria().andIdIn(caseIds); List oldCase = functionalCaseMapper.selectByExample(functionalCaseExample); @@ -352,56 +628,38 @@ public class FunctionalCaseMinderService { //更新附属表信息 FunctionalCaseBlob functionalCaseBlob = updateBlob(functionalCaseChangeRequest, caseId, caseBlobMapper); //更新自定义字段 - List functionalCaseCustomFields = updateCustomFields(functionalCaseChangeRequest, caseCustomFieldMap, caseId, caseCustomFieldMapper); - //排序 - if (StringUtils.isNotBlank(functionalCaseChangeRequest.getMoveMode())) { - reSetMap(functionalCaseChangeRequest, moduleCaseMap, functionalCase); - } - FunctionalCaseHistoryLogDTO historyLogDTO = new FunctionalCaseHistoryLogDTO(functionalCase, functionalCaseBlob, caseCustomFieldMap.get(caseId), new ArrayList<>(), new ArrayList<>()); + List functionalCaseCustomFields = updateCustomFields(functionalCaseChangeRequest, oldCaseCustomFieldMap, caseId, caseCustomFieldMapper); + //日志 + FunctionalCaseHistoryLogDTO historyLogDTO = new FunctionalCaseHistoryLogDTO(functionalCase, functionalCaseBlob, oldCaseCustomFieldMap.get(caseId), new ArrayList<>(), new ArrayList<>()); FunctionalCaseHistoryLogDTO old = new FunctionalCaseHistoryLogDTO(oldCaseMap.get(caseId), oldBlobMap.get(caseId), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); - addLog(request, userId, caseId, historyLogDTO, updateLogDTOS, old); + LogDTO logDTO = addLog(request, userId, caseId, historyLogDTO, old); + functionalMinderUpdateDTO.getUpdateLogDTOS().add(logDTO); + //通知 FunctionalCaseDTO functionalCaseDTO = getFunctionalCaseDTO(functionalCase, functionalCaseCustomFields, customFieldNameMap); - updateNoticeList.add(functionalCaseDTO); - + functionalMinderUpdateDTO.getUpdateNoticeList().add(functionalCaseDTO); + updateTargetIdsMap(functionalCaseChangeRequest.getId(), functionalCaseChangeRequest.getTargetId(), sourceIdAndTargetIdsMap); } - moduleCaseMap.forEach((k, v) -> { - updatePosList.addAll(v); - }); } - //批量排序 - batchSort(updatePosList, caseMapper); } - //处理空白节点 - dealAdditionalNode(request, userId, additionalNodeMapper, newModuleMap); + functionalMinderUpdateDTO.setSourceIdAndTargetIdsMap(sourceIdAndTargetIdsMap); + return sourceIdAndInsertCaseIdMap; + } - moduleNodeIds.addAll(caseNodeIds); - if (CollectionUtils.isNotEmpty(moduleNodeIds)) { - dealMindAdditionalMode(moduleNodeIds); - } - - sqlSession.flushStatements(); - SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); - - Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); - for (LogDTO addLogDTO : addLogDTOS) { - addLogDTO.setOrganizationId(project.getOrganizationId()); - } - for (LogDTO updateLogDTO : updateLogDTOS) { - updateLogDTO.setOrganizationId(project.getOrganizationId()); - } - operationLogService.batchAdd(addLogDTOS); - operationLogService.batchAdd(updateLogDTOS); - User user = userMapper.selectByPrimaryKey(userId); - List resources = new ArrayList<>(); - resources.addAll(JSON.parseArray(JSON.toJSONString(noticeList), Map.class)); - commonNoticeSendService.sendNotice(NoticeConstants.TaskType.FUNCTIONAL_CASE_TASK, NoticeConstants.Event.CREATE, resources, user, request.getProjectId()); - resources = new ArrayList<>(); - resources.addAll(JSON.parseArray(JSON.toJSONString(updateNoticeList), Map.class)); - commonNoticeSendService.sendNotice(NoticeConstants.TaskType.FUNCTIONAL_CASE_TASK, NoticeConstants.Event.UPDATE, resources, user, request.getProjectId()); + /** + * 根据Ids获取已存在的自定义字段值 + * + * @param caseIds caseIds + * @return Map> + */ + private Map> getOldCaseCustomFieldMap(List caseIds) { + FunctionalCaseCustomFieldExample example = new FunctionalCaseCustomFieldExample(); + example.createCriteria().andCaseIdIn(caseIds); + List allFields = functionalCaseCustomFieldMapper.selectByExample(example); + return allFields.stream().collect(Collectors.groupingBy(FunctionalCaseCustomField::getCaseId)); } @NotNull - private Map getDefaultValueMap(FunctionalCaseMinderEditRequest request) { + private Map getDefaultCustomFieldValueMap(FunctionalCaseMinderEditRequest request) { TemplateDTO defaultTemplateDTO = projectTemplateService.getDefaultTemplateDTO(request.getProjectId(), TemplateScene.FUNCTIONAL.toString()); List customFields = defaultTemplateDTO.getCustomFields(); Map defaultValueMap = new HashMap<>(); @@ -418,71 +676,53 @@ public class FunctionalCaseMinderService { return defaultValueMap; } - private void dealAdditionalNode(FunctionalCaseMinderEditRequest request, String userId, MindAdditionalNodeMapper additionalNodeMapper, Map newModuleMap) { + private Map dealAdditionalNode(FunctionalCaseMinderEditRequest request, String userId, MindAdditionalNodeMapper additionalNodeMapper, Map sourceIdAndInsertModuleIdMap, FunctionalMinderUpdateDTO functionalMinderUpdateDTO) { + Map sourceIdAndInsertTextIdMap = new HashMap<>(); + Map sourceIdAndTargetIdsMap = new HashMap<>(); if (CollectionUtils.isNotEmpty(request.getAdditionalNodeList())) { - List updatePosList = new ArrayList<>(); Map> resourceMap = request.getAdditionalNodeList().stream().collect(Collectors.groupingBy(MindAdditionalNodeRequest::getType)); List addList = resourceMap.get(OperationLogType.ADD.toString()); - Map newNodeMap = new HashMap<>(); //空白节点的父节点不一定是空白节点,有可能是模块 if (CollectionUtils.isNotEmpty(addList)) { List nodes = new ArrayList<>(); - Map> parentModuleMap = getParentNodeMap(addList); for (MindAdditionalNodeRequest mindAdditionalNodeRequest : addList) { MindAdditionalNode mindAdditionalNode = buildNode(request, userId, mindAdditionalNodeRequest, additionalNodeMapper); nodes.add(mindAdditionalNode); - newNodeMap.put(mindAdditionalNodeRequest.getId(), mindAdditionalNode.getId()); - reSetNodeMap(mindAdditionalNodeRequest, parentModuleMap, mindAdditionalNode); + sourceIdAndInsertTextIdMap.put(mindAdditionalNodeRequest.getId(), mindAdditionalNode.getId()); + updateTargetIdsMap(mindAdditionalNodeRequest.getId(), mindAdditionalNodeRequest.getTargetId(), sourceIdAndTargetIdsMap); } for (MindAdditionalNode node : nodes) { - if (StringUtils.isNotBlank(newNodeMap.get(node.getParentId()))) { - node.setParentId(newNodeMap.get(node.getParentId())); + if (StringUtils.isNotBlank(sourceIdAndInsertTextIdMap.get(node.getParentId()))) { + node.setParentId(sourceIdAndInsertTextIdMap.get(node.getParentId())); } - if (StringUtils.isNotBlank(newModuleMap.get(node.getParentId()))) { - node.setParentId(newModuleMap.get(node.getParentId())); + if (StringUtils.isNotBlank(sourceIdAndInsertModuleIdMap.get(node.getParentId()))) { + node.setParentId(sourceIdAndInsertModuleIdMap.get(node.getParentId())); } additionalNodeMapper.insert(node); } - parentModuleMap.forEach((k, v) -> { - updatePosList.addAll(v); - }); - } //处理更新 List updateList = resourceMap.get(OperationLogType.UPDATE.toString()); if (CollectionUtils.isNotEmpty(updateList)) { List nodes = new ArrayList<>(); - Map> parentModuleMap = getParentNodeMap(updateList); for (MindAdditionalNodeRequest mindAdditionalNodeRequest : updateList) { MindAdditionalNode updateModule = updateNode(userId, mindAdditionalNodeRequest, additionalNodeMapper); nodes.add(updateModule); - reSetNodeMap(mindAdditionalNodeRequest, parentModuleMap, updateModule); + updateTargetIdsMap(mindAdditionalNodeRequest.getId(), mindAdditionalNodeRequest.getTargetId(), sourceIdAndTargetIdsMap); } for (MindAdditionalNode node : nodes) { - if (StringUtils.isNotBlank(newNodeMap.get(node.getParentId()))) { - node.setParentId(newNodeMap.get(node.getParentId())); + if (StringUtils.isNotBlank(sourceIdAndInsertTextIdMap.get(node.getParentId()))) { + node.setParentId(sourceIdAndInsertTextIdMap.get(node.getParentId())); } - if (StringUtils.isNotBlank(newModuleMap.get(node.getParentId()))) { - node.setParentId(newModuleMap.get(node.getParentId())); + if (StringUtils.isNotBlank(sourceIdAndInsertModuleIdMap.get(node.getParentId()))) { + node.setParentId(sourceIdAndInsertModuleIdMap.get(node.getParentId())); } additionalNodeMapper.updateByPrimaryKeySelective(node); } - parentModuleMap.forEach((k, v) -> { - updatePosList.addAll(v); - }); } - //批量排序 - batchSortNode(updatePosList, additionalNodeMapper); - } - } - - private void batchSortNode(List updatePosList, MindAdditionalNodeMapper mindAdditionalNodeMapper) { - for (MindAdditionalNode mindAdditionalNode : updatePosList) { - MindAdditionalNode additionalNode = new MindAdditionalNode(); - additionalNode.setId(mindAdditionalNode.getId()); - additionalNode.setPos(mindAdditionalNode.getPos()); - mindAdditionalNodeMapper.updateByPrimaryKeySelective(additionalNode); } + functionalMinderUpdateDTO.setSourceIdAndTargetIdsMap(sourceIdAndTargetIdsMap); + return sourceIdAndInsertTextIdMap; } private MindAdditionalNode updateNode(String userId, MindAdditionalNodeRequest mindAdditionalNodeRequest, MindAdditionalNodeMapper mindAdditionalNodeMapper) { @@ -497,31 +737,6 @@ public class FunctionalCaseMinderService { return mindAdditionalNode; } - private void reSetNodeMap(MindAdditionalNodeRequest mindAdditionalNodeRequest, Map> parentModuleMap, MindAdditionalNode mindAdditionalNode) { - List mindAdditionalNodes = parentModuleMap.get(mindAdditionalNode.getParentId()); - if (CollectionUtils.isEmpty(mindAdditionalNodes)) { - return; - } - List sortList = mindAdditionalNodes.stream().sorted(Comparator.comparing(MindAdditionalNode::getPos)).toList(); - int j = getNodeIndex(mindAdditionalNodeRequest, sortList); - List additionalNodeList = getAdditionalNodes(mindAdditionalNodeRequest, mindAdditionalNode, sortList, j); - for (int i = 0; i < additionalNodeList.size(); i++) { - additionalNodeList.get(i).setPos(5000L * i); - } - parentModuleMap.put(mindAdditionalNode.getParentId(), additionalNodeList); - } - - private int getNodeIndex(MindAdditionalNodeRequest mindAdditionalNodeRequest, List sortList) { - int j = 0; - for (int i = 0; i < sortList.size(); i++) { - if (StringUtils.equalsIgnoreCase(sortList.get(i).getId(), mindAdditionalNodeRequest.getTargetId())) { - j = i; - break; - } - } - return j; - } - private MindAdditionalNode buildNode(FunctionalCaseMinderEditRequest request, String userId, MindAdditionalNodeRequest mindAdditionalNodeRequest, MindAdditionalNodeMapper additionalNodeMapper) { MindAdditionalNode mindAdditionalNode = new MindAdditionalNode(); mindAdditionalNode.setId(IDGenerator.nextStr()); @@ -530,122 +745,104 @@ public class FunctionalCaseMinderService { mindAdditionalNode.setProjectId(request.getProjectId()); mindAdditionalNode.setCreateTime(System.currentTimeMillis()); mindAdditionalNode.setUpdateTime(mindAdditionalNode.getCreateTime()); - mindAdditionalNode.setPos(this.countPos(mindAdditionalNode.getParentId())); + mindAdditionalNode.setPos(LIMIT_POS); mindAdditionalNode.setCreateUser(userId); mindAdditionalNode.setUpdateUser(userId); return mindAdditionalNode; } - private Map> getParentNodeMap(List addList) { - List targetIds = addList.stream().filter(t -> !StringUtils.equalsIgnoreCase(t.getMoveMode(), MoveTypeEnum.APPEND.name())).map(MindAdditionalNodeRequest::getTargetId).distinct().toList(); - List parentIds = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(targetIds)) { - MindAdditionalNodeExample mindAdditionalNodeExample = new MindAdditionalNodeExample(); - mindAdditionalNodeExample.createCriteria().andIdIn(targetIds); - List mindAdditionalNodes = mindAdditionalNodeMapper.selectByExample(mindAdditionalNodeExample); - parentIds = mindAdditionalNodes.stream().map(MindAdditionalNode::getParentId).distinct().toList(); - } - if (CollectionUtils.isEmpty(parentIds)) { - parentIds = addList.stream().map(MindAdditionalNodeRequest::getParentId).distinct().toList(); - } - MindAdditionalNodeExample mindAdditionalNodeExample = new MindAdditionalNodeExample(); - mindAdditionalNodeExample.createCriteria().andParentIdIn(parentIds); - List mindAdditionalNodes = mindAdditionalNodeMapper.selectByExample(mindAdditionalNodeExample); - return mindAdditionalNodes.stream().collect(Collectors.groupingBy(MindAdditionalNode::getParentId)); - - } - - private List dealModule(FunctionalCaseMinderEditRequest request, String userId, FunctionalCaseModuleMapper moduleMapper, Map newModuleMap) { - List nodeIds = new ArrayList<>(); + private Map dealModule(FunctionalCaseMinderEditRequest request, String userId, FunctionalCaseModuleMapper moduleMapper, FunctionalMinderUpdateDTO functionalMinderUpdateDTO) { + //页面传来的模块ids 与系统内新增的ID的map + Map sourceIdAndInsertIdMap = new HashMap<>(); + Map sourceIdAndTargetIdsMap = new HashMap<>(); if (CollectionUtils.isNotEmpty(request.getUpdateModuleList())) { - List updatePosList = new ArrayList<>(); //处理新增 Map> resourceMap = request.getUpdateModuleList().stream().collect(Collectors.groupingBy(FunctionalCaseModuleEditRequest::getType)); List addList = resourceMap.get(OperationLogType.ADD.toString()); if (CollectionUtils.isNotEmpty(addList)) { List modules = new ArrayList<>(); - Map> parentModuleMap = getParentModuleMap(addList); + //查出已存在同层级的节点 + Map> parentIdInDBMap = getParentIdInDBMap(addList); for (FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest : addList) { - checkModules(functionalCaseModuleEditRequest, parentModuleMap, OperationLogType.ADD.toString()); - FunctionalCaseModule functionalCaseModule = buildModule(request, userId, functionalCaseModuleEditRequest, moduleMapper); + FunctionalCaseModule functionalCaseModule = buildModule(request, userId, functionalCaseModuleEditRequest); modules.add(functionalCaseModule); - newModuleMap.put(functionalCaseModuleEditRequest.getId(), functionalCaseModule.getId()); - reSetModuleMap(functionalCaseModuleEditRequest, parentModuleMap, functionalCaseModule); + sourceIdAndInsertIdMap.put(functionalCaseModuleEditRequest.getId(), functionalCaseModule.getId()); + updateTargetIdsMap(functionalCaseModuleEditRequest.getId(), functionalCaseModuleEditRequest.getTargetId(), sourceIdAndTargetIdsMap); } for (FunctionalCaseModule module : modules) { - if (StringUtils.isNotBlank(newModuleMap.get(module.getParentId()))) { - module.setParentId(newModuleMap.get(module.getParentId())); + if (StringUtils.isNotBlank(sourceIdAndInsertIdMap.get(module.getParentId()))) { + module.setParentId(sourceIdAndInsertIdMap.get(module.getParentId())); } - nodeIds.add(module.getId()); + checkModules(module, parentIdInDBMap, OperationLogType.ADD.toString()); moduleMapper.insert(module); } - parentModuleMap.forEach((k, v) -> { - updatePosList.addAll(v); - }); - } - //处理更新 + //处理更新(更新的情况是可能换数据本身,可能换父节点,可能换顺序) List updateList = resourceMap.get(OperationLogType.UPDATE.toString()); if (CollectionUtils.isNotEmpty(updateList)) { List modules = new ArrayList<>(); - Map> parentModuleMap = getParentModuleMap(updateList); + Map> parentIdInDBMap = getParentIdInDBMap(updateList); for (FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest : updateList) { - checkModules(functionalCaseModuleEditRequest, parentModuleMap, OperationLogType.UPDATE.toString()); - FunctionalCaseModule updateModule = updateModule(userId, functionalCaseModuleEditRequest, moduleMapper); + FunctionalCaseModule updateModule = updateModule(userId, functionalCaseModuleEditRequest); modules.add(updateModule); - reSetModuleMap(functionalCaseModuleEditRequest, parentModuleMap, updateModule); + updateTargetIdsMap(functionalCaseModuleEditRequest.getId(), functionalCaseModuleEditRequest.getTargetId(), sourceIdAndTargetIdsMap); } for (FunctionalCaseModule module : modules) { - if (StringUtils.isNotBlank(newModuleMap.get(module.getParentId()))) { - module.setParentId(newModuleMap.get(module.getParentId())); + if (StringUtils.isNotBlank(sourceIdAndInsertIdMap.get(module.getParentId()))) { + module.setParentId(sourceIdAndInsertIdMap.get(module.getParentId())); } + checkModules(module, parentIdInDBMap, OperationLogType.UPDATE.toString()); moduleMapper.updateByPrimaryKeySelective(module); } - parentModuleMap.forEach((k, v) -> { - updatePosList.addAll(v); - }); } - //批量排序 - batchSortModule(updatePosList, moduleMapper); } - return nodeIds; + functionalMinderUpdateDTO.setSourceIdAndTargetIdsMap(sourceIdAndTargetIdsMap); + return sourceIdAndInsertIdMap; } - private static void batchSortModule(List updatePosList, FunctionalCaseModuleMapper moduleMapper) { - for (FunctionalCaseModule functionalCaseModule : updatePosList) { - FunctionalCaseModule functionalCaseModuleUpdatePos = new FunctionalCaseModule(); - functionalCaseModuleUpdatePos.setId(functionalCaseModule.getId()); - functionalCaseModuleUpdatePos.setPos(functionalCaseModule.getPos()); - moduleMapper.updateByPrimaryKeySelective(functionalCaseModuleUpdatePos); - } + private static void updateTargetIdsMap(String sourceId, String targetId, Map sourceIdAndTargetIdsMap) { + sourceIdAndTargetIdsMap.put(sourceId, targetId); } - private static void batchSort(List updatePosList, FunctionalCaseMapper caseMapper) { - if (CollectionUtils.isEmpty(updatePosList)) { - return; - } - for (FunctionalCase functionalCase : updatePosList) { - FunctionalCase functionalCaseUpdatePos = new FunctionalCase(); - functionalCaseUpdatePos.setId(functionalCase.getId()); - functionalCaseUpdatePos.setPos(functionalCase.getPos()); - caseMapper.updateByPrimaryKeySelective(functionalCaseUpdatePos); - } + @NotNull + private Map> getParentIdInDBMap(List functionalCaseModuleEditRequests) { + List parentIds = functionalCaseModuleEditRequests.stream().map(FunctionalCaseModuleEditRequest::getParentId).toList(); + FunctionalCaseModuleExample functionalCaseModuleExample = new FunctionalCaseModuleExample(); + functionalCaseModuleExample.createCriteria().andParentIdIn(parentIds); + List sameParentListInDB = functionalCaseModuleMapper.selectByExample(functionalCaseModuleExample); + return sameParentListInDB.stream().collect(Collectors.groupingBy(FunctionalCaseModule::getParentId)); } - private static void checkModules(FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest, Map> parentModuleMap, String type) { - List functionalCaseModules = parentModuleMap.get(functionalCaseModuleEditRequest.getParentId()); + /** + * 检查同一个父模块下不能有同名子集 + * + * @param parentIdMap 同一父模块的其他子集 + * @param type 保存类型 + */ + private static void checkModules(FunctionalCaseModule module, Map> parentIdMap, String type) { + List functionalCaseModules = parentIdMap.get(module.getParentId()); if (CollectionUtils.isEmpty(functionalCaseModules)) { + //新增的算上 + List modules = new ArrayList<>(); + modules.add(module); + parentIdMap.put(module.getParentId(), modules); return; } if (StringUtils.equalsIgnoreCase(type, OperationLogType.ADD.toString())) { - List sameNameList = functionalCaseModules.stream().filter(t -> StringUtils.equalsIgnoreCase(t.getName(), functionalCaseModuleEditRequest.getName())).toList(); + List sameNameList = functionalCaseModules.stream().filter(t -> StringUtils.equalsIgnoreCase(t.getName(), module.getName())).toList(); if (CollectionUtils.isNotEmpty(sameNameList)) { throw new MSException(Translator.get("node.name.repeat")); + } else { + functionalCaseModules.add(module); + parentIdMap.put(module.getParentId(), functionalCaseModules); } } else { - List sameNameList = functionalCaseModules.stream().filter(t -> StringUtils.equalsIgnoreCase(t.getName(), functionalCaseModuleEditRequest.getName()) && !StringUtils.equalsIgnoreCase(t.getId(), functionalCaseModuleEditRequest.getId())).toList(); + List sameNameList = functionalCaseModules.stream().filter(t -> StringUtils.equalsIgnoreCase(t.getName(), module.getName()) && !StringUtils.equalsIgnoreCase(t.getId(), module.getId())).toList(); if (CollectionUtils.isNotEmpty(sameNameList)) { throw new MSException(Translator.get("node.name.repeat")); + } else { + functionalCaseModules.add(module); + parentIdMap.put(module.getParentId(), functionalCaseModules); } } } @@ -694,7 +891,7 @@ public class FunctionalCaseMinderService { return functionalCaseDTO; } - private static void addLog(FunctionalCaseMinderEditRequest request, String userId, String caseId, FunctionalCaseHistoryLogDTO historyLogDTO, List addLogDTOS, FunctionalCaseHistoryLogDTO old) { + private LogDTO addLog(FunctionalCaseMinderEditRequest request, String userId, String caseId, FunctionalCaseHistoryLogDTO historyLogDTO, FunctionalCaseHistoryLogDTO old) { LogDTO dto = new LogDTO( request.getProjectId(), null, @@ -708,11 +905,11 @@ public class FunctionalCaseMinderService { dto.setMethod(HttpMethodConstants.POST.name()); dto.setModifiedValue(JSON.toJSONBytes(historyLogDTO)); dto.setOriginalValue(JSON.toJSONBytes(old)); - addLogDTOS.add(dto); + return dto; } @NotNull - private FunctionalCaseModule buildModule(FunctionalCaseMinderEditRequest request, String userId, FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest, FunctionalCaseModuleMapper moduleMapper) { + private FunctionalCaseModule buildModule(FunctionalCaseMinderEditRequest request, String userId, FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest) { FunctionalCaseModule functionalCaseModule = new FunctionalCaseModule(); functionalCaseModule.setId(IDGenerator.nextStr()); functionalCaseModule.setName(functionalCaseModuleEditRequest.getName()); @@ -720,14 +917,14 @@ public class FunctionalCaseMinderService { functionalCaseModule.setProjectId(request.getProjectId()); functionalCaseModule.setCreateTime(System.currentTimeMillis()); functionalCaseModule.setUpdateTime(functionalCaseModule.getCreateTime()); - functionalCaseModule.setPos(this.countPos(functionalCaseModule.getParentId())); + functionalCaseModule.setPos(LIMIT_POS); functionalCaseModule.setCreateUser(userId); functionalCaseModule.setUpdateUser(userId); return functionalCaseModule; } @NotNull - private FunctionalCaseModule updateModule(String userId, FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest, FunctionalCaseModuleMapper moduleMapper) { + private FunctionalCaseModule updateModule(String userId, FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest) { FunctionalCaseModule updateModule = new FunctionalCaseModule(); updateModule.setId(functionalCaseModuleEditRequest.getId()); updateModule.setName(functionalCaseModuleEditRequest.getName()); @@ -739,134 +936,13 @@ public class FunctionalCaseMinderService { return updateModule; } - @NotNull - private Map> getParentModuleMap(List addList) { - List targetIds = addList.stream().filter(t -> !StringUtils.equalsIgnoreCase(t.getMoveMode(), MoveTypeEnum.APPEND.name())).map(FunctionalCaseModuleEditRequest::getTargetId).distinct().toList(); - List parentIds = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(targetIds)) { - FunctionalCaseModuleExample functionalCaseModuleExample = new FunctionalCaseModuleExample(); - functionalCaseModuleExample.createCriteria().andIdIn(targetIds); - List functionalCaseModules = functionalCaseModuleMapper.selectByExample(functionalCaseModuleExample); - parentIds = functionalCaseModules.stream().map(FunctionalCaseModule::getParentId).distinct().toList(); - } - if (CollectionUtils.isEmpty(parentIds)) { - parentIds = addList.stream().map(FunctionalCaseModuleEditRequest::getParentId).distinct().toList(); - } - FunctionalCaseModuleExample functionalCaseModuleExample = new FunctionalCaseModuleExample(); - functionalCaseModuleExample.createCriteria().andParentIdIn(parentIds); - List functionalCaseModules = functionalCaseModuleMapper.selectByExample(functionalCaseModuleExample); - return functionalCaseModules.stream().collect(Collectors.groupingBy(FunctionalCaseModule::getParentId)); - } - - private Long countPos(String parentId) { - Long maxPos = extFunctionalCaseModuleMapper.getMaxPosByParentId(parentId); - if (maxPos == null) { - return LIMIT_POS; - } else { - return maxPos + LIMIT_POS; - } - } - - private void reSetModuleMap(FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest, Map> parentModuleMap, FunctionalCaseModule functionalCaseModule) { - List functionalCaseModuleInDbList = parentModuleMap.get(functionalCaseModule.getParentId()); - if (CollectionUtils.isEmpty(functionalCaseModuleInDbList)) { - return; - } - List sortList = functionalCaseModuleInDbList.stream().sorted(Comparator.comparing(FunctionalCaseModule::getPos)).toList(); - int j = getModuleIndex(functionalCaseModuleEditRequest, sortList); - List functionalCaseModules = getFunctionalCaseModules(functionalCaseModuleEditRequest, functionalCaseModule, sortList, j); - for (int i = 0; i < functionalCaseModules.size(); i++) { - functionalCaseModules.get(i).setPos(5000L * i); - } - parentModuleMap.put(functionalCaseModule.getParentId(), functionalCaseModules); - } - - @NotNull - private static List getFunctionalCaseModules(FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest, FunctionalCaseModule functionalCaseModule, List sortList, int j) { - List finallyModules = new ArrayList<>(); - List beforeModules; - List afterModules; - if (StringUtils.equals(functionalCaseModuleEditRequest.getMoveMode(), MoveTypeEnum.AFTER.name())) { - beforeModules = sortList.subList(0, j + 1); - afterModules = sortList.subList(j + 1, sortList.size()); - } else { - beforeModules = sortList.subList(0, j); - afterModules = sortList.subList(j, sortList.size()); - } - finallyModules.addAll(beforeModules); - finallyModules.add(functionalCaseModule); - finallyModules.addAll(afterModules); - return finallyModules; - } - - private static List getAdditionalNodes(MindAdditionalNodeRequest mindAdditionalNodeRequest, MindAdditionalNode mindAdditionalNode, List sortList, int j) { - List finallyModules = new ArrayList<>(); - List beforeModules; - List afterModules; - if (StringUtils.equals(mindAdditionalNodeRequest.getMoveMode(), MoveTypeEnum.AFTER.name())) { - beforeModules = sortList.subList(0, j + 1); - afterModules = sortList.subList(j + 1, sortList.size()); - } else { - beforeModules = sortList.subList(0, j); - afterModules = sortList.subList(j, sortList.size()); - } - finallyModules.addAll(beforeModules); - finallyModules.add(mindAdditionalNode); - finallyModules.addAll(afterModules); - return finallyModules; - } - - private static int getModuleIndex(FunctionalCaseModuleEditRequest functionalCaseModuleEditRequest, List sortList) { - int j = 0; - for (int i = 0; i < sortList.size(); i++) { - if (StringUtils.equalsIgnoreCase(sortList.get(i).getId(), functionalCaseModuleEditRequest.getTargetId())) { - j = i; - break; - } - } - return j; - } - - - private void reSetMap(FunctionalCaseChangeRequest functionalCaseChangeRequest, Map> moduleCaseMap, FunctionalCase functionalCase) { - List functionalCaseInDbList = moduleCaseMap.get(functionalCase.getModuleId()); - if (CollectionUtils.isEmpty(functionalCaseInDbList)) { - return; - } - List sortList = functionalCaseInDbList.stream().sorted(Comparator.comparing(FunctionalCase::getPos)).toList(); - int j = 0; - j = getIndex(functionalCaseChangeRequest, sortList, j); - List functionalCases = getFunctionalCases(functionalCaseChangeRequest, sortList, j, functionalCase); - for (int i = 0; i < functionalCases.size(); i++) { - functionalCases.get(i).setPos(5000L * i); - } - moduleCaseMap.put(functionalCase.getModuleId(), functionalCases); - } - - @NotNull - private Map> getModuleCaseMap(List addList) { - List list = addList.stream().map(FunctionalCaseChangeRequest::getTargetId).distinct().toList(); - if (CollectionUtils.isEmpty(list)) { - return new HashMap<>(); - } - FunctionalCaseExample functionalCaseExample = new FunctionalCaseExample(); - functionalCaseExample.createCriteria().andIdIn(list); - List functionalCases = functionalCaseMapper.selectByExample(functionalCaseExample); - List targetModuleIds = functionalCases.stream().map(FunctionalCase::getModuleId).distinct().toList(); - if (CollectionUtils.isEmpty(targetModuleIds)) { - return new HashMap<>(); - } - functionalCaseExample = new FunctionalCaseExample(); - functionalCaseExample.createCriteria().andModuleIdIn(targetModuleIds); - List functionalCasesByModule = functionalCaseMapper.selectByExample(functionalCaseExample); - return functionalCasesByModule.stream().collect(Collectors.groupingBy(FunctionalCase::getModuleId)); - } - private FunctionalCase updateCase(FunctionalCaseChangeRequest request, String userId, FunctionalCaseMapper caseMapper) { FunctionalCase functionalCase = new FunctionalCase(); BeanUtils.copyBean(functionalCase, request); functionalCase.setUpdateUser(userId); functionalCase.setUpdateTime(System.currentTimeMillis()); + functionalCase.setCreateUser(null); + functionalCase.setCreateTime(null); //更新用例 caseMapper.updateByPrimaryKeySelective(functionalCase); return functionalCase; @@ -905,45 +981,17 @@ public class FunctionalCaseMinderService { } }); if (CollectionUtils.isNotEmpty(addFields)) { - List functionalCaseCustomFields1 = saveCustomField(caseId, caseCustomFieldMapper, addFields); - total.addAll(functionalCaseCustomFields1); + List caseCustomFields = saveCustomField(caseId, caseCustomFieldMapper, addFields); + total.addAll(caseCustomFields); } if (CollectionUtils.isNotEmpty(updateFields)) { - List functionalCaseCustomFields1 = updateField(updateFields, caseId, caseCustomFieldMapper); - total.addAll(functionalCaseCustomFields1); + List caseCustomFields = updateField(updateFields, caseId, caseCustomFieldMapper); + total.addAll(caseCustomFields); } } return total; } - @NotNull - private static List getFunctionalCases(FunctionalCaseChangeRequest functionalCaseChangeRequest, List sortList, int j, FunctionalCase functionalCase) { - List finallyCases = new ArrayList<>(); - List beforeCases; - List afterCases; - if (StringUtils.equals(functionalCaseChangeRequest.getMoveMode(), MoveTypeEnum.AFTER.name())) { - beforeCases = sortList.subList(0, j + 1); - afterCases = sortList.subList(j + 1, sortList.size()); - } else { - beforeCases = sortList.subList(0, j); - afterCases = sortList.subList(j, sortList.size()); - } - finallyCases.addAll(beforeCases); - finallyCases.add(functionalCase); - finallyCases.addAll(afterCases); - return finallyCases; - } - - private static int getIndex(FunctionalCaseChangeRequest functionalCaseChangeRequest, List sortList, int j) { - for (int i = 0; i < sortList.size(); i++) { - if (StringUtils.equalsIgnoreCase(sortList.get(i).getId(), functionalCaseChangeRequest.getTargetId())) { - j = i; - break; - } - } - return j; - } - private List updateField(List updateFields, String caseId, FunctionalCaseCustomFieldMapper caseCustomFieldMapper) { List caseCustomFields = new ArrayList<>(); updateFields.forEach(custom -> { @@ -1000,19 +1048,19 @@ public class FunctionalCaseMinderService { } @NotNull - private FunctionalCase addCase(FunctionalCaseMinderEditRequest request, String userId, FunctionalCaseChangeRequest functionalCaseChangeRequest, FunctionalCaseMapper caseMapper, Map newModuleMap) { + private FunctionalCase addCase(FunctionalCaseMinderEditRequest request, String userId, FunctionalCaseChangeRequest functionalCaseChangeRequest, FunctionalCaseMapper caseMapper, Map sourceIdAndInsertModuleIdMap) { FunctionalCase functionalCase = new FunctionalCase(); BeanUtils.copyBean(functionalCase, functionalCaseChangeRequest); String caseId = IDGenerator.nextStr(); functionalCase.setId(caseId); - if (StringUtils.isNotBlank(newModuleMap.get(functionalCaseChangeRequest.getModuleId()))) { - functionalCase.setModuleId(newModuleMap.get(functionalCaseChangeRequest.getModuleId())); + if (StringUtils.isNotBlank(sourceIdAndInsertModuleIdMap.get(functionalCaseChangeRequest.getModuleId()))) { + functionalCase.setModuleId(sourceIdAndInsertModuleIdMap.get(functionalCaseChangeRequest.getModuleId())); } functionalCase.setProjectId(request.getProjectId()); functionalCase.setVersionId(request.getVersionId()); functionalCase.setNum(functionalCaseService.getNextNum(request.getProjectId())); functionalCase.setReviewStatus(FunctionalCaseReviewStatus.UN_REVIEWED.name()); - functionalCase.setPos(functionalCaseService.getNextOrder(functionalCase.getProjectId())); + functionalCase.setPos(LIMIT_POS); functionalCase.setRefId(caseId); functionalCase.setLastExecuteResult(ExecStatus.PENDING.name()); functionalCase.setLatest(true); @@ -1050,18 +1098,17 @@ public class FunctionalCaseMinderService { List additionalOptionDTOS = resourceMap.get(ModuleConstants.ROOT_NODE_PARENT_ID); if (CollectionUtils.isNotEmpty(additionalOptionDTOS)) { List mindAdditionalNodeIds = caseModuleOptionDTOS.stream().map(MinderOptionDTO::getId).toList(); - dealMindAdditionalMode(mindAdditionalNodeIds); + dealMindAdditionalMode(mindAdditionalNodeIds, mindAdditionalNodeMapper); } } } - private void dealMindAdditionalMode(List mindAdditionalNodeIds) { + private void dealMindAdditionalMode(List mindAdditionalNodeIds, MindAdditionalNodeMapper additionalNodeMapper) { MindAdditionalNodeExample mindAdditionalNodeExample = new MindAdditionalNodeExample(); mindAdditionalNodeExample.createCriteria().andIdIn(mindAdditionalNodeIds); - mindAdditionalNodeMapper.deleteByExample(mindAdditionalNodeExample); + additionalNodeMapper.deleteByExample(mindAdditionalNodeExample); } - public List getReviewMindFunctionalCase(FunctionalCaseReviewMindRequest request, boolean deleted, String userId, String viewStatusUserId) { List list = new ArrayList<>(); //查出当前模块下的所有用例 @@ -1073,7 +1120,6 @@ public class FunctionalCaseMinderService { return list; } - public List getPlanMindFunctionalCase(FunctionalCasePlanMindRequest request, boolean deleted) { List list = new ArrayList<>(); //查出当前模块下的所有用例 @@ -1148,4 +1194,5 @@ public class FunctionalCaseMinderService { //默认模块下不允许创建子模块。 它本身也就是叶子节点。 return new BaseTreeNode(ModuleConstants.DEFAULT_NODE_ID, name, ModuleConstants.NODE_TYPE_DEFAULT, ModuleConstants.ROOT_NODE_PARENT_ID); } + } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/TestPlanCollectionAssociateDTO.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/TestPlanCollectionAssociateDTO.java index dc9ad97c18..3ca6ae61dd 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/TestPlanCollectionAssociateDTO.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/dto/TestPlanCollectionAssociateDTO.java @@ -19,7 +19,7 @@ public class TestPlanCollectionAssociateDTO implements Serializable { @Schema(description = "关联关系的ids", requiredMode = Schema.RequiredMode.REQUIRED) private List ids; - @Schema(description = "关联关系的type(功能:FUNCTIONAL_CASE/接口定义:API/接口用例:API_CASE/场景:SCENARIO_CASE)", requiredMode = Schema.RequiredMode.REQUIRED) + @Schema(description = "关联关系的type(功能:FUNCTIONAL/接口定义:API/接口用例:API_CASE/场景:API_SCENARIO)", requiredMode = Schema.RequiredMode.REQUIRED) private String associateType;