diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/quota/service/BaseQuotaService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/quota/service/BaseQuotaService.java index ab03fa7859..7dde186864 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/quota/service/BaseQuotaService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/quota/service/BaseQuotaService.java @@ -10,11 +10,13 @@ import io.metersphere.commons.constants.ReportKeys; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.LogUtil; -import io.metersphere.commons.utils.SessionUtils; - import io.metersphere.i18n.Translator; -import io.metersphere.quota.dto.*; +import io.metersphere.quota.dto.CountDto; +import io.metersphere.quota.dto.QuotaConstants; +import io.metersphere.quota.dto.ReportTimeInfo; +import io.metersphere.quota.dto.TestOverview; import io.metersphere.request.TestPlanRequest; +import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; @@ -22,7 +24,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; -import jakarta.annotation.Resource; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.SimpleDateFormat; @@ -220,9 +221,8 @@ public class BaseQuotaService { * 获取可用资源池集合 * @return 资源池名称Set集合 */ - public Set getQuotaResourcePools() { + public Set getQuotaResourcePools(String projectId) { Set pools = new HashSet<>(); - String projectId = SessionUtils.getCurrentProjectId(); Quota pjQuota = quotaManagementService.getProjectQuota(projectId); if (pjQuota != null) { if (isValid(pjQuota, pjQuota.getResourcePool())) { diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/SystemParameterService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/SystemParameterService.java index 639d76db97..7da1af15f0 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/SystemParameterService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/SystemParameterService.java @@ -1,7 +1,10 @@ package io.metersphere.service; import io.metersphere.base.domain.*; -import io.metersphere.base.mapper.*; +import io.metersphere.base.mapper.ProjectApplicationMapper; +import io.metersphere.base.mapper.SystemHeaderMapper; +import io.metersphere.base.mapper.SystemParameterMapper; +import io.metersphere.base.mapper.UserHeaderMapper; import io.metersphere.base.mapper.ext.BaseSystemParameterMapper; import io.metersphere.commons.constants.MicroServiceName; import io.metersphere.commons.constants.ParamConstants; @@ -24,6 +27,7 @@ import io.metersphere.notice.domain.MailInfo; import io.metersphere.notice.domain.Receiver; import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.sender.impl.MailNoticeSender; +import io.metersphere.quota.service.BaseQuotaService; import io.metersphere.request.HeaderRequest; import io.metersphere.request.resourcepool.QueryResourcePoolRequest; import jakarta.annotation.Resource; @@ -38,10 +42,7 @@ import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; @@ -63,7 +64,10 @@ public class SystemParameterService { @Resource private MicroService microService; @Resource + private BaseQuotaService baseQuotaService; + @Resource private BaseTestResourcePoolService baseTestResourcePoolService; + @Resource private BaseProjectService baseProjectService; @Resource @@ -262,44 +266,51 @@ public class SystemParameterService { return baseSystemConfigDTO; } - public TestResourcePoolDTO getTestResourcePool() { + public List getTestResourcePool() { QueryResourcePoolRequest resourcePoolRequest = new QueryResourcePoolRequest(); resourcePoolRequest.setStatus(ResourceStatusEnum.VALID.name()); - List poolDTOS = baseTestResourcePoolService.listResourcePools(resourcePoolRequest); - if (CollectionUtils.isNotEmpty(poolDTOS)) { - List localPools = poolDTOS.stream().filter(item -> - StringUtils.equals(item.getName(), "LOCAL")).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(localPools)) { - return localPools.get(0); - } else { - return poolDTOS.get(0); - } + return baseTestResourcePoolService.listResourcePools(resourcePoolRequest); + } + + private String filterQuota(List list, String projectId) { + Set pools = baseQuotaService.getQuotaResourcePools(projectId); + List poolList = new ArrayList<>(); + if (!pools.isEmpty()) { + poolList = list.stream().filter(pool -> pools.contains(pool.getId())).collect(Collectors.toList()); + } + if (CollectionUtils.isNotEmpty(poolList)) { + return poolList.get(0).getId(); + } else if (CollectionUtils.isNotEmpty(list)) { + return list.get(0).getId(); } return null; } - public void batchSaveApp(List projectIds, String poolId, ProjectApplicationMapper batchMapper) { + public void batchSaveApp(List projectIds, List poolList, ProjectApplicationMapper batchMapper) { if (CollectionUtils.isNotEmpty(projectIds)) { List appProjectIds = baseProjectApplicationService.getProjectIds(projectIds); projectIds.removeAll(appProjectIds); // 添加 projectIds.forEach(item -> { - ProjectApplication application = new ProjectApplication(); - application.setProjectId(item); - application.setType(ProjectApplicationType.POOL_ENABLE.name()); - application.setTypeValue(String.valueOf(true)); - batchMapper.insert(application); + String id = filterQuota(poolList, item); + if (StringUtils.isNotBlank(id)) { + ProjectApplication application = new ProjectApplication(); + application.setProjectId(item); + application.setType(ProjectApplicationType.POOL_ENABLE.name()); + application.setTypeValue(String.valueOf(true)); + batchMapper.insert(application); - ProjectApplication applicationValue = new ProjectApplication(); - applicationValue.setProjectId(item); - applicationValue.setType(ProjectApplicationType.RESOURCE_POOL_ID.name()); - applicationValue.setTypeValue(poolId); - batchMapper.insert(applicationValue); + ProjectApplication applicationValue = new ProjectApplication(); + applicationValue.setProjectId(item); + applicationValue.setType(ProjectApplicationType.RESOURCE_POOL_ID.name()); + applicationValue.setTypeValue(id); + batchMapper.insert(applicationValue); + } }); } } - public void batchSaveProjectApp(String poolId, long size) { + public void batchSaveProjectApp(List poolList, long size) { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); ProjectApplicationMapper batchMapper = sqlSession.getMapper(ProjectApplicationMapper.class); final long pageSize = 500; @@ -308,7 +319,7 @@ public class SystemParameterService { long pageNum = i * pageSize; List projectIds = baseProjectService.getPage(pageNum, pageSize); if (CollectionUtils.isNotEmpty(projectIds)) { - batchSaveApp(projectIds, poolId, batchMapper); + batchSaveApp(projectIds, poolList, batchMapper); } } sqlSession.flushStatements(); @@ -320,12 +331,13 @@ public class SystemParameterService { public void saveProjectApplication(String projectId) { BaseSystemConfigDTO config = getBaseInfo(); if (config != null && StringUtils.equals(config.getRunMode(), "POOL")) { - TestResourcePoolDTO poolDTO = getTestResourcePool(); - if (poolDTO != null) { + List poolList = getTestResourcePool(); + String id = filterQuota(poolList, projectId); + if (StringUtils.isNotBlank(id)) { ProjectApplication applicationValue = new ProjectApplication(); applicationValue.setProjectId(projectId); applicationValue.setType(ProjectApplicationType.RESOURCE_POOL_ID.name()); - applicationValue.setTypeValue(poolDTO.getId()); + applicationValue.setTypeValue(id); projectApplicationMapper.insert(applicationValue); ProjectApplication application = new ProjectApplication(); @@ -356,10 +368,10 @@ public class SystemParameterService { } // 启用资源池 if (param.getParamKey().equals("base.run.mode") && param.getParamValue().equals("POOL")) { - TestResourcePoolDTO poolDTO = getTestResourcePool(); long size = baseProjectService.count(); - if (poolDTO != null && size > 0) { - batchSaveProjectApp(poolDTO.getId(), size); + List poolList = getTestResourcePool(); + if (CollectionUtils.isNotEmpty(poolList) && size > 0) { + batchSaveProjectApp(poolList, size); } } // 去掉路径最后的 / diff --git a/framework/sdk-parent/xpack-interface/src/main/java/io/metersphere/xpack/resourcepool/service/ValidQuotaResourcePoolService.java b/framework/sdk-parent/xpack-interface/src/main/java/io/metersphere/xpack/resourcepool/service/ValidQuotaResourcePoolService.java index 86d7ff0077..2810b07bac 100644 --- a/framework/sdk-parent/xpack-interface/src/main/java/io/metersphere/xpack/resourcepool/service/ValidQuotaResourcePoolService.java +++ b/framework/sdk-parent/xpack-interface/src/main/java/io/metersphere/xpack/resourcepool/service/ValidQuotaResourcePoolService.java @@ -1,5 +1,6 @@ package io.metersphere.xpack.resourcepool.service; +import io.metersphere.commons.utils.SessionUtils; import io.metersphere.dto.TestResourcePoolDTO; import io.metersphere.quota.service.BaseQuotaService; import io.metersphere.request.resourcepool.QueryResourcePoolRequest; @@ -23,9 +24,9 @@ public class ValidQuotaResourcePoolService { public List listValidQuotaResourcePools() { return filterQuota(listValidResourcePools()); } - private List filterQuota(List list) { - Set pools = baseQuotaService.getQuotaResourcePools(); + String projectId = SessionUtils.getCurrentProjectId(); + Set pools = baseQuotaService.getQuotaResourcePools(projectId); if (!pools.isEmpty()) { return list.stream().filter(pool -> pools.contains(pool.getId())).collect(Collectors.toList()); }