From 01a375210e628f61102177dc46cc361ea1762a77 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 6 Feb 2024 15:43:48 +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=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5=E6=96=B0?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=9C=BA=E5=99=A8=E4=BA=BA=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/NoticeMessageTaskService.java | 25 ++++++++++++------- .../NoticeMessageTaskControllerTests.java | 22 ++++++++++++++-- 2 files changed, 36 insertions(+), 11 deletions(-) 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 ba5f3c45a5..f43eb00efb 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 @@ -54,8 +54,6 @@ public class NoticeMessageTaskService { @Resource private MessageTaskBlobMapper messageTaskBlobMapper; @Resource - private ProjectMapper projectMapper; - @Resource private ExtProjectUserRoleMapper extProjectUserRoleMapper; @Resource protected CustomFieldMapper customFieldMapper; @@ -79,12 +77,12 @@ public class NoticeMessageTaskService { //检查设置的通知是否存在,如果存在则更新 List messageTasks = updateMessageTasks(messageTaskRequest, userId, mapper, blobMapper, existUserIds); //保存消息任务 - List messageTaskReceivers = CollectionUtils.isEmpty(messageTasks) ? new ArrayList<>() : messageTasks.stream().map(MessageTask::getReceiver).toList(); + 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, messageTaskReceivers); + insertMessageTask(messageTaskRequest, userId, mapper, blobMapper, existUserIds, robotMap); sqlSession.flushStatements(); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); if (CollectionUtils.isNotEmpty(stringListMap.get(NO_USER_NAMES))) { @@ -118,12 +116,18 @@ public class NoticeMessageTaskService { * @param mapper MessageTaskMapper * @param blobMapper MessageTaskBlobMapper * @param existUserIds 系统中还存在的入参传过来的接收人 - * @param messageTaskReceivers 更新过后还有多少接收人需要保存 + * @param robotMap 更新过后机器人消息通知map */ - private void insertMessageTask(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List existUserIds, List messageTaskReceivers) { + private void insertMessageTask(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List existUserIds, Map> robotMap) { + //判断是否需要完整的新增机器人数据 + List messageTasks = robotMap.get(messageTaskRequest.getRobotId()); for (String receiverId : existUserIds) { - if (CollectionUtils.isNotEmpty(messageTaskReceivers) && messageTaskReceivers.contains(receiverId)) { - continue; + //如果为空则需要完整新增,新增人,则需要过滤数据里是否有这个人,有则过滤 + if (!CollectionUtils.isEmpty(messageTasks)) { + List receivers = messageTasks.stream().map(MessageTask::getReceiver).toList(); + if (CollectionUtils.isNotEmpty(receivers) && receivers.contains(receiverId)) { + continue; + } } MessageTask messageTask = new MessageTask(); buildMessageTask(messageTaskRequest, userId, receiverId, messageTask); @@ -171,6 +175,7 @@ public class NoticeMessageTaskService { boolean useDefaultSubject = messageTaskRequest.getUseDefaultSubject() == null || messageTaskRequest.getUseDefaultSubject(); boolean useDefaultTemplate = messageTaskRequest.getUseDefaultTemplate() == null || messageTaskRequest.getUseDefaultTemplate(); + //查询在当前事件和已存在的通知人员下的数据,即数据库已存在的数据 MessageTaskExample messageTaskExample = new MessageTaskExample(); messageTaskExample.createCriteria().andReceiverIn(existUserIds).andProjectIdEqualTo(messageTaskRequest.getProjectId()).andTaskTypeEqualTo(messageTaskRequest.getTaskType()).andEventEqualTo(messageTaskRequest.getEvent()); List messageTasks = messageTaskMapper.selectByExample(messageTaskExample); @@ -181,7 +186,8 @@ public class NoticeMessageTaskService { for (MessageTask messageTask : messageTasks) { messageTask.setUpdateTime(System.currentTimeMillis()); messageTask.setUpdateUser(userId); - if (StringUtils.isNotBlank(messageTaskRequest.getRobotId())) { + //如果有机器人id,则是修改机器人开关和消息配置 + if (StringUtils.isNotBlank(messageTaskRequest.getRobotId()) && StringUtils.equalsIgnoreCase(messageTask.getProjectRobotId(), messageTaskRequest.getRobotId())) { messageTask.setEnable(enable); messageTask.setUseDefaultSubject(useDefaultSubject); messageTask.setUseDefaultTemplate(useDefaultTemplate); @@ -189,6 +195,7 @@ public class NoticeMessageTaskService { messageTask.setSubject(messageTaskRequest.getSubject()); } } else { + //如果没有机器人id,则只是修改人其余配置不变 useDefaultTemplate = messageTask.getUseDefaultTemplate(); } mapper.updateByPrimaryKeySelective(messageTask); 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 2c3596587e..8b820f5de8 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 @@ -144,14 +144,32 @@ public class NoticeMessageTaskControllerTests extends BaseTest { Assertions.assertTrue(messageTasks1.get(0).getEnable()); Assertions.assertTrue(StringUtils.equalsIgnoreCase(messageTasks1.get(0).getSubject(), "看看改不改")); - messageTaskExample = new MessageTaskExample(); + 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(1, messageTasks.size()); + Assertions.assertEquals(2, 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); Assertions.assertEquals(0, messageTasks.size()); + + //projectId 存在 用户有部分被删除的测试 + messageTaskRequest = new MessageTaskRequest(); + messageTaskRequest.setProjectId("project-message-test"); + messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK); + messageTaskRequest.setEvent(NoticeConstants.Event.CREATE); + userIds = new ArrayList<>(); + userIds.add("project-message-user-1"); + messageTaskRequest.setReceiverIds(userIds); + messageTaskRequest.setRobotId("test_message_robot5"); + messageTaskRequest.setEnable(true); + messageTaskRequest.setSubject("看看改不改"); + messageTaskRequest.setUseDefaultSubject(false); + this.requestPostWithOk("/notice/message/task/save",messageTaskRequest); + messageTaskExample = new MessageTaskExample(); + messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE).andProjectRobotIdEqualTo("test_message_robot5"); + messageTasks = messageTaskMapper.selectByExample(messageTaskExample); + Assertions.assertEquals(1, messageTasks.size()); } @Test