From 1ad018b61462521648a44ece53f2593e08591a61 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 6 Feb 2024 21:21:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BF=9D=E5=AD=98=E6=B6=88=E6=81=AF=E4=B8=8D?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/mapper/ExtMessageTaskMapper.java | 11 +++ .../project/mapper/ExtMessageTaskMapper.xml | 10 +++ .../service/NoticeMessageTaskService.java | 68 ++++++++++++++----- .../NoticeMessageTaskControllerTests.java | 2 +- 4 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtMessageTaskMapper.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtMessageTaskMapper.xml diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtMessageTaskMapper.java b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtMessageTaskMapper.java new file mode 100644 index 0000000000..b1aa0d996e --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtMessageTaskMapper.java @@ -0,0 +1,11 @@ +package io.metersphere.project.mapper; + +import io.metersphere.project.domain.MessageTask; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface ExtMessageTaskMapper { + + List getRobotType(@Param("projectId") String projectId, @Param("taskType") String taskType, @Param("event") String event); +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtMessageTaskMapper.xml b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtMessageTaskMapper.xml new file mode 100644 index 0000000000..c464b1a203 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtMessageTaskMapper.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java index f43eb00efb..beb47088c6 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java @@ -22,6 +22,7 @@ import io.metersphere.system.notice.utils.MessageTemplateUtils; import io.metersphere.system.uid.IDGenerator; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -57,6 +58,8 @@ public class NoticeMessageTaskService { private ExtProjectUserRoleMapper extProjectUserRoleMapper; @Resource protected CustomFieldMapper customFieldMapper; + @Resource + protected ExtMessageTaskMapper extMessageTaskMapper; public static final String USER_IDS = "user_ids"; @@ -74,15 +77,14 @@ public class NoticeMessageTaskService { //检查用户是否存在 Map> stringListMap = checkUserExistProject(messageTaskRequest.getReceiverIds(), projectId); List existUserIds = stringListMap.get(USER_IDS); + + List robotType = extMessageTaskMapper.getRobotType(projectId, messageTaskRequest.getTaskType(), messageTaskRequest.event); + Map> robotEnasleMap = robotType.stream().collect(Collectors.groupingBy(MessageTask::getProjectRobotId)); //检查设置的通知是否存在,如果存在则更新 List messageTasks = updateMessageTasks(messageTaskRequest, userId, mapper, blobMapper, existUserIds); - //保存消息任务 Map> robotMap = messageTasks.stream().collect(Collectors.groupingBy(MessageTask::getProjectRobotId)); - //如果新增时只选了用户,没有选机器人,默认机器人为站内信 - ProjectRobot projectRobot = getDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId()); - String robotId = projectRobot.getId(); - messageTaskRequest.setRobotId(robotId); - insertMessageTask(messageTaskRequest, userId, mapper, blobMapper, existUserIds, robotMap); + //保存消息任务 + insertMessageTask(messageTaskRequest, userId, mapper, blobMapper, existUserIds, robotMap, robotEnasleMap); sqlSession.flushStatements(); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); if (CollectionUtils.isNotEmpty(stringListMap.get(NO_USER_NAMES))) { @@ -111,14 +113,45 @@ public class NoticeMessageTaskService { /** * 新增MessageTask * - * @param messageTaskRequest 入参 - * @param userId 当前用户i的 - * @param mapper MessageTaskMapper - * @param blobMapper MessageTaskBlobMapper - * @param existUserIds 系统中还存在的入参传过来的接收人 - * @param robotMap 更新过后机器人消息通知map + * @param messageTaskRequest 入参 + * @param userId 当前用户i的 + * @param mapper MessageTaskMapper + * @param blobMapper MessageTaskBlobMapper + * @param existUserIds 系统中还存在的入参传过来的接收人 + * @param robotMap 更新过后机器人消息通知map + * @param robotEnableMap 数据库里已开起的机器人 */ - private void insertMessageTask(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List existUserIds, Map> robotMap) { + private void insertMessageTask(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List existUserIds, Map> robotMap, Map> robotEnableMap) { + if (MapUtils.isEmpty(robotEnableMap) || StringUtils.isNotBlank(messageTaskRequest.getRobotId())) { + //如果新增时只选了用户,没有选机器人,默认机器人为站内信 + ProjectRobot projectRobot = getDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId()); + String robotId = projectRobot.getId(); + boolean enable = getEnable(robotMap, robotId); + messageTaskRequest.setRobotId(robotId); + if (messageTaskRequest.getEnable() == null) { + messageTaskRequest.setEnable(enable); + } + setUserMessageTaskInfo(messageTaskRequest, userId, mapper, blobMapper, existUserIds, robotMap); + } else { + robotEnableMap.forEach((robotId, v) -> { + boolean enable = getEnable(robotMap, robotId); + messageTaskRequest.setRobotId(robotId); + messageTaskRequest.setEnable(enable); + setUserMessageTaskInfo(messageTaskRequest, userId, mapper, blobMapper, existUserIds, robotMap); + }); + } + } + + private boolean getEnable(Map> robotMap, String robotId) { + List messageTasks = robotMap.get(robotId); + if (CollectionUtils.isEmpty(messageTasks)) { + return false; + } else { + return messageTasks.get(0).getEnable(); + } + } + + private static void setUserMessageTaskInfo(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List existUserIds, Map> robotMap) { //判断是否需要完整的新增机器人数据 List messageTasks = robotMap.get(messageTaskRequest.getRobotId()); for (String receiverId : existUserIds) { @@ -322,7 +355,7 @@ public class NoticeMessageTaskService { public List getMessageList(String projectId) throws IOException { //获取返回数据结构 StringBuilder jsonStr = new StringBuilder(); - try{ + try { InputStream inputStream = getClass().getResourceAsStream("/message_task.json"); assert inputStream != null; BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); @@ -332,7 +365,7 @@ public class NoticeMessageTaskService { } reader.close(); inputStream.close(); - } catch (IOException e){ + } catch (IOException e) { throw new RuntimeException(e); } @@ -383,8 +416,8 @@ public class NoticeMessageTaskService { messageTaskTypeDTO.setTaskTypeName(taskTypeMap.get(messageTaskTypeDTO.getTaskType())); List eventList = List.of(NoticeConstants.Event.CREATE, NoticeConstants.Event.UPDATE, NoticeConstants.Event.DELETE); for (MessageTaskDetailDTO messageTaskDetailDTO : messageTaskTypeDTO.getMessageTaskDetailDTOList()) { - if (StringUtils.equalsIgnoreCase(messageTaskTypeDTO.getTaskType(),NoticeConstants.TaskType.API_DEFINITION_TASK) && eventList.contains(messageTaskDetailDTO.getEvent())) { - messageTaskDetailDTO.setEventName("API"+StringUtils.SPACE+eventMap.get(messageTaskDetailDTO.getEvent())); + if (StringUtils.equalsIgnoreCase(messageTaskTypeDTO.getTaskType(), NoticeConstants.TaskType.API_DEFINITION_TASK) && eventList.contains(messageTaskDetailDTO.getEvent())) { + messageTaskDetailDTO.setEventName("API" + StringUtils.SPACE + eventMap.get(messageTaskDetailDTO.getEvent())); } else { messageTaskDetailDTO.setEventName(eventMap.get(messageTaskDetailDTO.getEvent())); } @@ -489,7 +522,6 @@ public class NoticeMessageTaskService { } - public MessageTemplateConfigDTO getTemplateDetail(String projectId, String taskType, String event, String robotId) { MessageTaskExample messageTaskExample = new MessageTaskExample(); messageTaskExample.createCriteria().andProjectIdEqualTo(projectId).andTaskTypeEqualTo(taskType).andEventEqualTo(event); diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java index 8b820f5de8..0f72828a35 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java @@ -147,7 +147,7 @@ public class NoticeMessageTaskControllerTests extends BaseTest { messageTaskExample = new MessageTaskExample(); messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE); List messageTasks = messageTaskMapper.selectByExample(messageTaskExample); - Assertions.assertEquals(2, messageTasks.size()); + Assertions.assertEquals(1, messageTasks.size()); messageTaskExample = new MessageTaskExample(); messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE).andReceiverEqualTo("project-message-user-3"); messageTasks = messageTaskMapper.selectByExample(messageTaskExample);