fix(项目管理): 修复保存消息不正确的接口

This commit is contained in:
guoyuqi 2024-02-06 21:21:34 +08:00 committed by Craftsman
parent def0db3f50
commit 1ad018b614
4 changed files with 72 additions and 19 deletions

View File

@ -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<MessageTask> getRobotType(@Param("projectId") String projectId, @Param("taskType") String taskType, @Param("event") String event);
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.project.mapper.ExtMessageTaskMapper">
<select id="getRobotType" resultType="io.metersphere.project.domain.MessageTask">
select * from message_task where project_id = #{projectId}
and task_type = #{taskType} and event = #{event} and enable = true group by project_robot_id
</select>
</mapper>

View File

@ -22,6 +22,7 @@ import io.metersphere.system.notice.utils.MessageTemplateUtils;
import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
@ -57,6 +58,8 @@ public class NoticeMessageTaskService {
private ExtProjectUserRoleMapper extProjectUserRoleMapper; private ExtProjectUserRoleMapper extProjectUserRoleMapper;
@Resource @Resource
protected CustomFieldMapper customFieldMapper; protected CustomFieldMapper customFieldMapper;
@Resource
protected ExtMessageTaskMapper extMessageTaskMapper;
public static final String USER_IDS = "user_ids"; public static final String USER_IDS = "user_ids";
@ -74,15 +77,14 @@ public class NoticeMessageTaskService {
//检查用户是否存在 //检查用户是否存在
Map<String, List<String>> stringListMap = checkUserExistProject(messageTaskRequest.getReceiverIds(), projectId); Map<String, List<String>> stringListMap = checkUserExistProject(messageTaskRequest.getReceiverIds(), projectId);
List<String> existUserIds = stringListMap.get(USER_IDS); List<String> existUserIds = stringListMap.get(USER_IDS);
List<MessageTask> robotType = extMessageTaskMapper.getRobotType(projectId, messageTaskRequest.getTaskType(), messageTaskRequest.event);
Map<String, List<MessageTask>> robotEnasleMap = robotType.stream().collect(Collectors.groupingBy(MessageTask::getProjectRobotId));
//检查设置的通知是否存在如果存在则更新 //检查设置的通知是否存在如果存在则更新
List<MessageTask> messageTasks = updateMessageTasks(messageTaskRequest, userId, mapper, blobMapper, existUserIds); List<MessageTask> messageTasks = updateMessageTasks(messageTaskRequest, userId, mapper, blobMapper, existUserIds);
//保存消息任务
Map<String, List<MessageTask>> robotMap = messageTasks.stream().collect(Collectors.groupingBy(MessageTask::getProjectRobotId)); Map<String, List<MessageTask>> robotMap = messageTasks.stream().collect(Collectors.groupingBy(MessageTask::getProjectRobotId));
//如果新增时只选了用户没有选机器人默认机器人为站内信 //保存消息任务
ProjectRobot projectRobot = getDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId()); insertMessageTask(messageTaskRequest, userId, mapper, blobMapper, existUserIds, robotMap, robotEnasleMap);
String robotId = projectRobot.getId();
messageTaskRequest.setRobotId(robotId);
insertMessageTask(messageTaskRequest, userId, mapper, blobMapper, existUserIds, robotMap);
sqlSession.flushStatements(); sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
if (CollectionUtils.isNotEmpty(stringListMap.get(NO_USER_NAMES))) { if (CollectionUtils.isNotEmpty(stringListMap.get(NO_USER_NAMES))) {
@ -117,8 +119,39 @@ public class NoticeMessageTaskService {
* @param blobMapper MessageTaskBlobMapper * @param blobMapper MessageTaskBlobMapper
* @param existUserIds 系统中还存在的入参传过来的接收人 * @param existUserIds 系统中还存在的入参传过来的接收人
* @param robotMap 更新过后机器人消息通知map * @param robotMap 更新过后机器人消息通知map
* @param robotEnableMap 数据库里已开起的机器人
*/ */
private void insertMessageTask(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List<String> existUserIds, Map<String, List<MessageTask>> robotMap) { private void insertMessageTask(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List<String> existUserIds, Map<String, List<MessageTask>> robotMap, Map<String, List<MessageTask>> 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<String, List<MessageTask>> robotMap, String robotId) {
List<MessageTask> 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<String> existUserIds, Map<String, List<MessageTask>> robotMap) {
//判断是否需要完整的新增机器人数据 //判断是否需要完整的新增机器人数据
List<MessageTask> messageTasks = robotMap.get(messageTaskRequest.getRobotId()); List<MessageTask> messageTasks = robotMap.get(messageTaskRequest.getRobotId());
for (String receiverId : existUserIds) { for (String receiverId : existUserIds) {
@ -322,7 +355,7 @@ public class NoticeMessageTaskService {
public List<MessageTaskDTO> getMessageList(String projectId) throws IOException { public List<MessageTaskDTO> getMessageList(String projectId) throws IOException {
//获取返回数据结构 //获取返回数据结构
StringBuilder jsonStr = new StringBuilder(); StringBuilder jsonStr = new StringBuilder();
try{ try {
InputStream inputStream = getClass().getResourceAsStream("/message_task.json"); InputStream inputStream = getClass().getResourceAsStream("/message_task.json");
assert inputStream != null; assert inputStream != null;
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
@ -332,7 +365,7 @@ public class NoticeMessageTaskService {
} }
reader.close(); reader.close();
inputStream.close(); inputStream.close();
} catch (IOException e){ } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -383,8 +416,8 @@ public class NoticeMessageTaskService {
messageTaskTypeDTO.setTaskTypeName(taskTypeMap.get(messageTaskTypeDTO.getTaskType())); messageTaskTypeDTO.setTaskTypeName(taskTypeMap.get(messageTaskTypeDTO.getTaskType()));
List<String> eventList = List.of(NoticeConstants.Event.CREATE, NoticeConstants.Event.UPDATE, NoticeConstants.Event.DELETE); List<String> eventList = List.of(NoticeConstants.Event.CREATE, NoticeConstants.Event.UPDATE, NoticeConstants.Event.DELETE);
for (MessageTaskDetailDTO messageTaskDetailDTO : messageTaskTypeDTO.getMessageTaskDetailDTOList()) { for (MessageTaskDetailDTO messageTaskDetailDTO : messageTaskTypeDTO.getMessageTaskDetailDTOList()) {
if (StringUtils.equalsIgnoreCase(messageTaskTypeDTO.getTaskType(),NoticeConstants.TaskType.API_DEFINITION_TASK) && eventList.contains(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())); messageTaskDetailDTO.setEventName("API" + StringUtils.SPACE + eventMap.get(messageTaskDetailDTO.getEvent()));
} else { } else {
messageTaskDetailDTO.setEventName(eventMap.get(messageTaskDetailDTO.getEvent())); messageTaskDetailDTO.setEventName(eventMap.get(messageTaskDetailDTO.getEvent()));
} }
@ -489,7 +522,6 @@ public class NoticeMessageTaskService {
} }
public MessageTemplateConfigDTO getTemplateDetail(String projectId, String taskType, String event, String robotId) { public MessageTemplateConfigDTO getTemplateDetail(String projectId, String taskType, String event, String robotId) {
MessageTaskExample messageTaskExample = new MessageTaskExample(); MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo(projectId).andTaskTypeEqualTo(taskType).andEventEqualTo(event); messageTaskExample.createCriteria().andProjectIdEqualTo(projectId).andTaskTypeEqualTo(taskType).andEventEqualTo(event);

View File

@ -147,7 +147,7 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
messageTaskExample = new MessageTaskExample(); messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE); messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE);
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample); List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
Assertions.assertEquals(2, messageTasks.size()); Assertions.assertEquals(1, messageTasks.size());
messageTaskExample = new MessageTaskExample(); messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE).andReceiverEqualTo("project-message-user-3"); 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); messageTasks = messageTaskMapper.selectByExample(messageTaskExample);