diff --git a/project-management/backend/src/main/java/io/metersphere/service/EnvironmentGroupProjectService.java b/project-management/backend/src/main/java/io/metersphere/service/EnvironmentGroupProjectService.java deleted file mode 100644 index 43bfb39bf0..0000000000 --- a/project-management/backend/src/main/java/io/metersphere/service/EnvironmentGroupProjectService.java +++ /dev/null @@ -1,84 +0,0 @@ -package io.metersphere.service; - - -import io.metersphere.base.domain.ApiTestEnvironmentExample; -import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; -import io.metersphere.base.domain.EnvironmentGroupProject; -import io.metersphere.base.domain.EnvironmentGroupProjectExample; -import io.metersphere.base.mapper.ApiTestEnvironmentMapper; -import io.metersphere.base.mapper.EnvironmentGroupProjectMapper; -import io.metersphere.base.mapper.ext.ExtEnvGroupProjectMapper; -import io.metersphere.commons.utils.JSON; -import io.metersphere.dto.EnvironmentGroupProjectDTO; -import org.jsoup.internal.StringUtil; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.CollectionUtils; - -import jakarta.annotation.Resource; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * @author lyh - */ - -@Service -@Transactional(rollbackFor = Exception.class) -public class EnvironmentGroupProjectService { - - @Resource - private ExtEnvGroupProjectMapper extEnvGroupProjectMapper; - @Resource - private ApiTestEnvironmentMapper apiTestEnvironmentMapper; - @Resource - private EnvironmentGroupProjectMapper environmentGroupProjectMapper; - - public List getList(String groupId) { - List list = extEnvGroupProjectMapper.getList(groupId); - if (!CollectionUtils.isEmpty(list)) { - list.forEach(e -> { - String projectId = e.getProjectId(); - ApiTestEnvironmentExample example = new ApiTestEnvironmentExample(); - example.createCriteria().andProjectIdEqualTo(projectId); - List environments = apiTestEnvironmentMapper.selectByExampleWithBLOBs(example); - e.setEnvironments(environments); - }); - } - return list; - } - - public String getEnvJson(String groupId) { - List list = extEnvGroupProjectMapper.getList(groupId); - Map map = list.stream() - .collect(Collectors.toMap(EnvironmentGroupProject::getProjectId, EnvironmentGroupProject::getEnvironmentId)); - return JSON.toJSONString(map); - } - - public Map getEnvMap(String groupId) { - List list = extEnvGroupProjectMapper.getList(groupId); - Map map = list.stream() - .collect(Collectors.toMap(EnvironmentGroupProject::getProjectId, EnvironmentGroupProject::getEnvironmentId)); - return map; - } - - public void deleteRelateEnv(String environmentId) { - if (StringUtil.isBlank(environmentId)) { - return; - } - EnvironmentGroupProjectExample example = new EnvironmentGroupProjectExample(); - example.createCriteria().andEnvironmentGroupIdEqualTo(environmentId); - environmentGroupProjectMapper.deleteByExample(example); - } - - public void deleteRelateProject(String projectId) { - if (StringUtil.isBlank(projectId)) { - return; - } - EnvironmentGroupProjectExample example = new EnvironmentGroupProjectExample(); - example.createCriteria().andProjectIdEqualTo(projectId); - environmentGroupProjectMapper.deleteByExample(example); - } - -} diff --git a/project-management/backend/src/main/java/io/metersphere/service/EnvironmentGroupService.java b/project-management/backend/src/main/java/io/metersphere/service/EnvironmentGroupService.java deleted file mode 100644 index 3f210bf9c8..0000000000 --- a/project-management/backend/src/main/java/io/metersphere/service/EnvironmentGroupService.java +++ /dev/null @@ -1,306 +0,0 @@ -package io.metersphere.service; - -import io.metersphere.base.domain.*; -import io.metersphere.base.mapper.ApiTestEnvironmentMapper; -import io.metersphere.base.mapper.EnvironmentGroupMapper; -import io.metersphere.base.mapper.EnvironmentGroupProjectMapper; -import io.metersphere.base.mapper.ProjectMapper; -import io.metersphere.base.mapper.ext.ExtEnvironmentGroupMapper; -import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.SessionUtils; -import io.metersphere.dto.EnvironmentGroupDTO; -import io.metersphere.i18n.Translator; -import io.metersphere.request.EnvironmentGroupRequest; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.StringUtils; -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.beans.BeanUtils; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import jakarta.annotation.Resource; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.stream.Collectors; - -@Service -@Transactional(rollbackFor = Exception.class) -public class EnvironmentGroupService { - - @Resource - private EnvironmentGroupMapper environmentGroupMapper; - @Resource - private ExtEnvironmentGroupMapper extEnvironmentGroupMapper; - @Resource - private SqlSessionFactory sqlSessionFactory; - @Resource - private EnvironmentGroupProjectMapper environmentGroupProjectMapper; - @Resource - private ProjectMapper projectMapper; - @Resource - private ApiTestEnvironmentMapper apiTestEnvironmentMapper; - @Resource - private EnvironmentGroupProjectService environmentGroupProjectService; - - public EnvironmentGroup add(EnvironmentGroupRequest request) { - request.setWorkspaceId(SessionUtils.getCurrentWorkspaceId()); - this.checkEnvironmentGroup(request); - - request.setId(UUID.randomUUID().toString()); - request.setCreateTime(System.currentTimeMillis()); - request.setUpdateTime(System.currentTimeMillis()); - environmentGroupMapper.insertSelective(request); - this.insertGroupProject(request); - - return request; - } - - private void insertGroupProject(EnvironmentGroupRequest request) { - SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - EnvironmentGroupProjectMapper mapper = sqlSession.getMapper(EnvironmentGroupProjectMapper.class); - - List envGroupProject = request.getEnvGroupProject(); - if (CollectionUtils.isEmpty(envGroupProject)) { - return; - } - List distinctProjects = envGroupProject.stream().map(EnvironmentGroupProject::getProjectId).distinct().collect(Collectors.toList()); - if (envGroupProject.size() != distinctProjects.size()) { - MSException.throwException(Translator.get("environment_group_has_duplicate_project")); - } - List projects = projectMapper.selectByExample(new ProjectExample()); - List projectIds = projects.stream().map(Project::getId).collect(Collectors.toList()); - List environments = apiTestEnvironmentMapper.selectByExample(new ApiTestEnvironmentExample()); - List environmentIds = environments.stream().map(ApiTestEnvironment::getId).collect(Collectors.toList()); - - for (EnvironmentGroupProject egp : envGroupProject) { - String projectId = egp.getProjectId(); - String environmentId = egp.getEnvironmentId(); - if (StringUtils.isBlank(projectId) || StringUtils.isBlank(environmentId)) { - continue; - } - - if (!projectIds.contains(projectId) || !environmentIds.contains(environmentId)) { - continue; - } - - EnvironmentGroupProjectExample example = new EnvironmentGroupProjectExample(); - example.createCriteria() - .andEnvironmentGroupIdEqualTo(request.getId()) - .andProjectIdEqualTo(projectId); - List environmentGroupProjects = environmentGroupProjectMapper.selectByExample(example); - if (CollectionUtils.isNotEmpty(environmentGroupProjects)) { - continue; - } - - EnvironmentGroupProject e = new EnvironmentGroupProject(); - e.setId(UUID.randomUUID().toString()); - e.setEnvironmentGroupId(request.getId()); - e.setProjectId(projectId); - e.setEnvironmentId(environmentId); - mapper.insert(e); - } - sqlSession.flushStatements(); - if (sqlSession != null && sqlSessionFactory != null) { - SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); - } - } - - public List getList(EnvironmentGroupRequest request) { - return extEnvironmentGroupMapper.getList(request); - } - - public void delete(String id) { - if (StringUtils.isBlank(id)) { - return; - } - environmentGroupMapper.deleteByPrimaryKey(id); - EnvironmentGroupProjectExample example = new EnvironmentGroupProjectExample(); - example.createCriteria().andEnvironmentGroupIdEqualTo(id); - environmentGroupProjectMapper.deleteByExample(example); - } - - public void deleteByWorkspaceId(String workspaceId) { - if (StringUtils.isBlank(workspaceId)) { - return; - } - EnvironmentGroupExample example = new EnvironmentGroupExample(); - example.createCriteria().andWorkspaceIdEqualTo(workspaceId); - List environmentGroups = environmentGroupMapper.selectByExample(example); - if (CollectionUtils.isNotEmpty(environmentGroups)) { - environmentGroups.forEach(environmentGroup -> delete(environmentGroup.getId())); - } - } - - public EnvironmentGroup update(EnvironmentGroupRequest request) { - request.setWorkspaceId(SessionUtils.getCurrentWorkspaceId()); - if (StringUtils.isNotBlank(request.getName())) { - this.checkEnvironmentGroup(request); - } - request.setUpdateTime(System.currentTimeMillis()); - environmentGroupMapper.updateByPrimaryKeySelective(request); - - String envGroupId = request.getId(); - EnvironmentGroupProjectExample example = new EnvironmentGroupProjectExample(); - example.createCriteria().andEnvironmentGroupIdEqualTo(envGroupId); - environmentGroupProjectMapper.deleteByExample(example); - - this.insertGroupProject(request); - - return request; - } - - private void checkEnvironmentGroup(EnvironmentGroupRequest request) { - String name = request.getName(); - if (StringUtils.isBlank(name)) { - MSException.throwException(Translator.get("null_environment_group_name")); - } - - EnvironmentGroupExample environmentGroupExample = new EnvironmentGroupExample(); - EnvironmentGroupExample.Criteria criteria = environmentGroupExample.createCriteria(); - criteria.andNameEqualTo(name) - .andWorkspaceIdEqualTo(request.getWorkspaceId()); - if (StringUtils.isNotBlank(request.getId())) { - criteria.andIdNotEqualTo(request.getId()); - } - - if (environmentGroupMapper.countByExample(environmentGroupExample) > 0) { - MSException.throwException(Translator.get("environment_group_name") + request.getName() + Translator.get("environment_group_exist")); - } - } - - public List getRelateProjectGroup(String projectId) { - return extEnvironmentGroupMapper.getRelateProject(SessionUtils.getCurrentWorkspaceId(), projectId); - } - - public void copy(String id) { - if (StringUtils.isBlank(id)) { - return; - } - EnvironmentGroup environmentGroup = environmentGroupMapper.selectByPrimaryKey(id); - EnvironmentGroupRequest copy = new EnvironmentGroupRequest(); - String copyId = UUID.randomUUID().toString(); - copy.setId(copyId); - - String copyNameId = copyId.substring(0, 3); - String copyName = environmentGroup.getName() + "_" + copyNameId + "_COPY"; - copy.setName(copyName); - copy.setWorkspaceId(environmentGroup.getWorkspaceId()); - this.checkEnvironmentGroup(copy); - - copy.setCreateTime(System.currentTimeMillis()); - copy.setUpdateTime(System.currentTimeMillis()); - copy.setCreateUser(SessionUtils.getUserId()); - copy.setDescription(environmentGroup.getDescription()); - environmentGroupMapper.insertSelective(copy); - - EnvironmentGroupProjectExample example = new EnvironmentGroupProjectExample(); - example.createCriteria().andEnvironmentGroupIdEqualTo(id); - List environmentGroupProjects = environmentGroupProjectMapper.selectByExample(example); - SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - EnvironmentGroupProjectMapper mapper = sqlSession.getMapper(EnvironmentGroupProjectMapper.class); - - for (EnvironmentGroupProject environmentGroupProject : environmentGroupProjects) { - environmentGroupProject.setId(UUID.randomUUID().toString()); - environmentGroupProject.setEnvironmentGroupId(copyId); - mapper.insertSelective(environmentGroupProject); - } - sqlSession.flushStatements(); - if (sqlSession != null && sqlSessionFactory != null) { - SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); - } - } - - public void modify(EnvironmentGroupRequest request) { - if (StringUtils.isBlank(request.getId()) || StringUtils.isBlank(request.getName())) { - return; - } - checkEnvironmentGroup(request); - environmentGroupMapper.updateByPrimaryKeySelective(request); - } - - public void batchAdd(EnvironmentGroupRequest request) { - Map map = request.getMap(); - if (map == null || map.isEmpty()) { - return; - } - SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - EnvironmentGroupProjectMapper mapper = sqlSession.getMapper(EnvironmentGroupProjectMapper.class); - - List groupIds = request.getGroupIds(); - List projects = projectMapper.selectByExample(new ProjectExample()); - List projectIds = projects.stream().map(Project::getId).collect(Collectors.toList()); - List apiTestEnvironments = apiTestEnvironmentMapper.selectByExample(new ApiTestEnvironmentExample()); - List envIds = apiTestEnvironments.stream().map(ApiTestEnvironment::getId).collect(Collectors.toList()); - for (String groupId : groupIds) { - EnvironmentGroup group = environmentGroupMapper.selectByPrimaryKey(groupId); - if (group == null) { - continue; - } - - for (Map.Entry entry : map.entrySet()) { - String projectId = entry.getKey(); - String envId = entry.getValue(); - if (!projectIds.contains(projectId) || !envIds.contains(envId)) { - continue; - } - - EnvironmentGroupProjectExample example = new EnvironmentGroupProjectExample(); - example.createCriteria().andEnvironmentGroupIdEqualTo(groupId) - .andProjectIdEqualTo(projectId); - List egpList = environmentGroupProjectMapper.selectByExample(example); - if (CollectionUtils.isNotEmpty(egpList)) { - EnvironmentGroupProject environmentGroupProject = egpList.get(0); - environmentGroupProject.setEnvironmentId(envId); - mapper.updateByPrimaryKeySelective(environmentGroupProject); - } else { - EnvironmentGroupProject egp = new EnvironmentGroupProject(); - egp.setId(UUID.randomUUID().toString()); - egp.setEnvironmentGroupId(groupId); - egp.setProjectId(projectId); - egp.setEnvironmentId(envId); - mapper.insertSelective(egp); - } - } - } - sqlSession.flushStatements(); - if (sqlSession != null && sqlSessionFactory != null) { - SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); - } - } - - public List getEnvOptionGroup(List projectIds) { - EnvironmentGroupExample example = new EnvironmentGroupExample(); - example.createCriteria().andWorkspaceIdEqualTo(SessionUtils.getCurrentWorkspaceId()); - List result = new ArrayList<>(); - List groups = environmentGroupMapper.selectByExample(example); - List ids = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(projectIds)) { - List projects = projectMapper.selectByExample(new ProjectExample()); - List allProjectIds = projects.stream().map(Project::getId).collect(Collectors.toList()); - ids = projectIds.stream().filter(allProjectIds::contains).collect(Collectors.toList()); - } - for (EnvironmentGroup group : groups) { - Map envMap = environmentGroupProjectService.getEnvMap(group.getId()); - EnvironmentGroupDTO dto = new EnvironmentGroupDTO(); - BeanUtils.copyProperties(group, dto); - if (CollectionUtils.isNotEmpty(ids)) { - boolean b = envMap.keySet().containsAll(ids); - if (BooleanUtils.isFalse(b)) { - dto.setDisabled(true); - } - } - result.add(dto); - } - return result; - } - - public EnvironmentGroup selectById(String id) { - return environmentGroupMapper.selectByPrimaryKey(id); - } -} diff --git a/project-management/backend/src/main/java/io/metersphere/service/UserGroupService.java b/project-management/backend/src/main/java/io/metersphere/service/UserGroupService.java deleted file mode 100644 index f6e1ef3ebd..0000000000 --- a/project-management/backend/src/main/java/io/metersphere/service/UserGroupService.java +++ /dev/null @@ -1,45 +0,0 @@ -package io.metersphere.service; - -import io.metersphere.base.domain.UserGroup; -import io.metersphere.base.domain.UserGroupExample; -import io.metersphere.base.mapper.UserGroupMapper; -import org.apache.commons.lang3.StringUtils; -import org.springframework.stereotype.Service; - -import jakarta.annotation.Resource; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -@Service -public class UserGroupService { - @Resource - private UserGroupMapper userGroupMapper; - - public List> getUserGroup(String userId) { - List> list = new ArrayList<>(); - UserGroupExample userRoleExample = new UserGroupExample(); - userRoleExample.createCriteria().andUserIdEqualTo(userId); - List userGroups = userGroupMapper.selectByExample(userRoleExample); - List collect = userGroups.stream() - .map(UserGroup::getGroupId) - .distinct() - .collect(Collectors.toList()); - for (String id : collect) { - Map map = new HashMap<>(2); - map.put("id", id); - map.put("ids", new ArrayList<>()); - for (UserGroup userGroup : userGroups) { - String groupId = userGroup.getGroupId(); - if (StringUtils.equals(groupId, id)) { - List ids = (List) map.get("ids"); - ids.add(userGroup.getSourceId()); - } - } - list.add(map); - } - return list; - } -}