diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 99781466ce..614d97308c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -139,6 +139,10 @@ public class ApiDefinitionService { @Resource private ExtTestPlanApiCaseMapper extTestPlanApiCaseMapper; + @Lazy + @Resource + private ApiModuleService apiModuleService; + private ThreadLocal currentApiOrder = new ThreadLocal<>(); private ThreadLocal currentApiCaseOrder = new ThreadLocal<>(); @@ -1134,11 +1138,11 @@ public class ApiDefinitionService { private String getReportNameByTestId(String testId) { String testName = extApiDefinitionMapper.selectNameById(testId); - if(StringUtils.isEmpty(testName)){ + if (StringUtils.isEmpty(testName)) { testName = extApiTestCaseMapper.selectNameById(testId); - if(StringUtils.isEmpty(testName)){ + if (StringUtils.isEmpty(testName)) { String resourceID = extTestPlanApiCaseMapper.getApiTestCaseIdById(testId); - if(StringUtils.isNotEmpty(resourceID)){ + if (StringUtils.isNotEmpty(resourceID)) { testName = extApiTestCaseMapper.selectNameById(resourceID); } } @@ -1273,6 +1277,7 @@ public class ApiDefinitionService { currentApiCaseOrder.remove(); currentApiOrder.remove(); List data = apiImport.getData(); + data = this.initApiModuleId(data); ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class); ApiTestCaseMapper apiTestCaseMapper = sqlSession.getMapper(ApiTestCaseMapper.class); ExtApiDefinitionMapper extApiDefinitionMapper = sqlSession.getMapper(ExtApiDefinitionMapper.class); @@ -1342,6 +1347,36 @@ public class ApiDefinitionService { } } + /** + * 初始化apiModuleId,如果apiModuleId不存在则赋默认的moduleId + * + * @param apiDefinitionList + * @return + */ + private List initApiModuleId(List apiDefinitionList) { + Map> protocalModuleIdMap = new HashMap<>(); + apiDefinitionList.forEach(apiDefinition -> { + if (protocalModuleIdMap.containsKey(apiDefinition.getProtocol()) && !protocalModuleIdMap.get(apiDefinition.getProtocol()).contains(apiDefinition.getModuleId())) { + protocalModuleIdMap.get(apiDefinition.getProtocol()).add(apiDefinition.getModuleId()); + } else { + protocalModuleIdMap.put(apiDefinition.getProtocol(), new ArrayList<>() {{ + this.add(apiDefinition.getModuleId()); + }}); + } + }); + Map> rightfulModuleIdMap = apiModuleService.checkModuleIds(protocalModuleIdMap); + for (ApiDefinitionWithBLOBs apiBlobs : apiDefinitionList) { + if (!rightfulModuleIdMap.containsKey(apiBlobs.getProtocol()) || !rightfulModuleIdMap.get(apiBlobs.getProtocol()).contains(apiBlobs.getModuleId())) { + ApiModule defaultModule = apiModuleService.getDefaultNode(apiBlobs.getProjectId(), apiBlobs.getProtocol()); + if (defaultModule != null) { + apiBlobs.setModuleId(defaultModule.getId()); + apiBlobs.setModulePath(defaultModule.getName()); + } + } + } + return apiDefinitionList; + } + public ReferenceDTO getReference(ApiScenarioRequest request) { ReferenceDTO dto = new ReferenceDTO(); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java index dc572143bc..f216abef60 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -13,6 +13,7 @@ import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper; import io.metersphere.base.mapper.ext.ExtApiModuleMapper; import io.metersphere.commons.constants.TestCaseConstants; import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.i18n.Translator; import io.metersphere.log.utils.ReflexObjectUtil; @@ -24,6 +25,7 @@ import io.metersphere.service.ProjectService; import io.metersphere.track.service.TestPlanApiCaseService; import io.metersphere.track.service.TestPlanProjectService; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -126,16 +128,17 @@ public class ApiModuleService extends NodeTreeService { } private Map parseModuleCountList(List> moduleCountList) { - Map returnMap = new HashMap<>(); - for (Map map: moduleCountList){ + Map returnMap = new HashMap<>(); + for (Map map : moduleCountList) { Object moduleIdObj = map.get("moduleId"); Object countNumObj = map.get("countNum"); - if(moduleIdObj!= null && countNumObj != null){ + if (moduleIdObj != null && countNumObj != null) { String moduleId = String.valueOf(moduleIdObj); try { Integer countNumInteger = new Integer(String.valueOf(countNumObj)); - returnMap.put(moduleId,countNumInteger); - }catch (Exception e){ + returnMap.put(moduleId, countNumInteger); + } catch (Exception e) { + LogUtil.error("method parseModuleCountList has error:", e); } } } @@ -303,7 +306,7 @@ public class ApiModuleService extends NodeTreeService { if (StringUtils.isNotBlank(node.getId())) { criteria.andIdNotEqualTo(node.getId()); } - if(StringUtils.isNotEmpty(node.getProtocol())){ + if (StringUtils.isNotEmpty(node.getProtocol())) { criteria.andProtocolEqualTo(node.getProtocol()); } return apiModuleMapper.selectByExample(example); @@ -520,10 +523,10 @@ public class ApiModuleService extends NodeTreeService { public long countById(String nodeId) { ApiModuleExample example = new ApiModuleExample(); example.createCriteria().andIdEqualTo(nodeId); - return apiModuleMapper.countByExample(example); + return apiModuleMapper.countByExample(example); } - public ApiModule getDefaultNode(String projectId,String protocol) { + public ApiModule getDefaultNode(String projectId, String protocol) { ApiModuleExample example = new ApiModuleExample(); example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("未规划接口").andParentIdIsNull(); List list = apiModuleMapper.selectByExample(example); @@ -540,12 +543,12 @@ public class ApiModuleService extends NodeTreeService { record.setCreateUser(SessionUtils.getUserId()); apiModuleMapper.insert(record); return record; - }else { + } else { return list.get(0); } } - public ApiModule getDefaultNodeUnCreateNew(String projectId,String protocol) { + public ApiModule getDefaultNodeUnCreateNew(String projectId, String protocol) { ApiModuleExample example = new ApiModuleExample(); example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("未规划接口").andParentIdIsNull(); List list = apiModuleMapper.selectByExample(example); @@ -565,4 +568,35 @@ public class ApiModuleService extends NodeTreeService { public String getModuleNameById(String moduleId) { return extApiModuleMapper.getNameById(moduleId); } + + /** + * 返回数据库中存在的id + * + * @param protocalModuleIdMap > + * @return + */ + public Map> checkModuleIds(Map> protocalModuleIdMap) { + Map> returnMap = new HashMap<>(); + if (MapUtils.isNotEmpty(protocalModuleIdMap)) { + ApiModuleExample example = new ApiModuleExample(); + for (Map.Entry> entry : protocalModuleIdMap.entrySet()) { + String protocol = entry.getKey(); + List moduleIds = entry.getValue(); + if (CollectionUtils.isNotEmpty(moduleIds)) { + example.clear(); + example.createCriteria().andIdIn(moduleIds).andProtocolEqualTo(protocol); + List moduleList = apiModuleMapper.selectByExample(example); + if (CollectionUtils.isNotEmpty(moduleList)) { + List idLIst = new ArrayList<>(); + moduleList.forEach(module -> { + idLIst.add(module.getId()); + }); + returnMap.put(protocol, idLIst); + } + + } + } + } + return returnMap; + } }