From e19e4480bd3b45eb5ced8c80b4178a8fe7bcef51 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Wed, 26 Apr 2023 14:32:59 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=B7=BB=E5=8A=A0=E6=88=90=E5=91=98?= =?UTF-8?q?=E8=A6=86=E7=9B=96BUG=E4=BF=AE=E5=A4=8D=E3=80=91=E3=80=90?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E7=A7=81=E8=81=8A=E6=8E=A5=E5=8F=A3=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cbtc/ATS/operation/Operation.java | 10 +++ .../cbtc/SimulationIdGenerator.java | 8 +++ .../cbtc/SimulationServiceImpl.java | 7 ++- .../cbtc/conversation/ConversationGroup.java | 26 ++++++++ .../ConversationGroupHandlerService.java | 61 ++++++++++++++++++- .../ConversationGroupListener.java | 15 +++++ .../ConversationGroupOperateHandler.java | 26 ++++++++ .../storage/vo/StorageConversationGroup.java | 14 +++-- .../vo/ConversationGroupSocketMessageVO.java | 3 + .../cbtc/data/vo/ConversationGroupVO.java | 29 ++++++--- ...tionConversationGroupPrivateChatEvent.java | 20 ++++++ .../simulation/cbtc/member/MemberManager.java | 6 ++ 12 files changed, 211 insertions(+), 14 deletions(-) create mode 100644 src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupPrivateChatEvent.java diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/ATS/operation/Operation.java b/src/main/java/club/joylink/rtss/simulation/cbtc/ATS/operation/Operation.java index cff391dfe..1ad36dacb 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/ATS/operation/Operation.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/ATS/operation/Operation.java @@ -1632,6 +1632,16 @@ public class Operation { * 会话群组获取未读消息 */ Conversation_Group_Read_Message(new Label[]{Label.CLIENT}, true), + + /** + * 跟指定用户文字私聊 + */ + Conversation_Group_Private_Text_Chat(new Label[]{Label.CLIENT}, true), + + /** + * 跟指定用户发语音信息 + */ + Conversation_Group_Private_Audio_Base64(new Label[]{Label.CLIENT}, true), //---------------------------- PSL ------------------------------ /** * PSL盘按下按钮 diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/SimulationIdGenerator.java b/src/main/java/club/joylink/rtss/simulation/cbtc/SimulationIdGenerator.java index 3dc0a0393..459c3c5bd 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/SimulationIdGenerator.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/SimulationIdGenerator.java @@ -58,6 +58,14 @@ public class SimulationIdGenerator { return String.valueOf(memberId.incrementAndGet()); } + public Long getCurrentMemberId() { + return memberId.get(); + } + + public void setMemberId(Long id) { + this.memberId.set(id); + } + /** * 剧本编制/预览/演出时,更新成员id基础值 */ diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/SimulationServiceImpl.java b/src/main/java/club/joylink/rtss/simulation/cbtc/SimulationServiceImpl.java index b819a6f89..b569ed75e 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/SimulationServiceImpl.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/SimulationServiceImpl.java @@ -383,8 +383,11 @@ public class SimulationServiceImpl implements SimulationService { if (CollectionUtils.isEmpty(conversationGroupVOList)) { return; } - Map groupMap = conversationGroupVOList.stream().map(g -> new ConversationGroup(simulation, g)) - .collect(Collectors.toMap(ConversationGroup::getId, group -> group)); + Map groupMap = conversationGroupVOList.stream().map(g ->{ + ConversationGroup group = new ConversationGroup(simulation, g); + group.initGroupType(); + return group; + }).collect(Collectors.toMap(ConversationGroup::getId, group -> group)); simulation.initDefaultConversationGroupMap(groupMap); } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroup.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroup.java index 1a370cdbc..514f094a8 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroup.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroup.java @@ -4,6 +4,7 @@ import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import club.joylink.rtss.vo.conversation.ConversationGroupVO; import lombok.Getter; +import lombok.Setter; import org.springframework.util.CollectionUtils; import java.time.LocalDateTime; @@ -45,6 +46,11 @@ public class ConversationGroup extends Chat { */ private AtomicInteger messageId = new AtomicInteger(0); + /** + * 群组类型 + */ + private ConversationType type; + /** * 用户组信息 * @@ -223,4 +229,24 @@ public class ConversationGroup extends Chat { } return member.getMember(); } + + public void initGroupType() { + if (this.type == null) { + this.type = ConversationType.GROUP_CHAT; + } + } + + public void initPrivateType() { + if (this.type == null) { + this.type = ConversationType.PRIVATE_CHAT; + } + } + + public boolean isGroup() { + return Objects.equals(this.type, ConversationType.GROUP_CHAT); + } + + public boolean isPrivate() { + return Objects.equals(this.type, ConversationType.PRIVATE_CHAT); + } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java index 2bcd4f193..ba28c121f 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java @@ -52,7 +52,8 @@ public class ConversationGroupHandlerService { if (member == null) { return List.of(); } else { - return allGroup.stream().filter(g -> g.isConversationMember(member)).map(ConversationGroupVO::new).collect(Collectors.toList()); + return allGroup.stream().filter(g -> g.isConversationMember(member)) + .map(g -> new ConversationGroupVO(g, member)).collect(Collectors.toList()); } } @@ -73,6 +74,7 @@ public class ConversationGroupHandlerService { List simulationMembers = memberIds.stream().map(simulation::getSimulationMemberById).collect(Collectors.toList()); Long groupId = simulation.getMaxConversationGroupId(); conversationGroup = new ConversationGroup(groupId, imageUrl, name, simulation.getCorrectSystemTime(), member, simulationMembers); + conversationGroup.initGroupType(); simulation.addConversationGroup(conversationGroup); // 通知用户消息 applicationEventPublisher.publishEvent(new SimulationConversationGroupCreateEvent(this, simulation, conversationGroup)); @@ -306,6 +308,63 @@ public class ConversationGroupHandlerService { applicationEventPublisher.publishEvent(new SimulationConversationGroupMessageStatusEvent(this, simulation, conversationGroup, messageList)); } + /** + * 文字私聊 + * @param simulation 仿真 + * @param member 用户 + * @param memberId 指定用户 + * @param content 消息内容 + * @return 消息信息 + */ + public ConversationGroupMessageVO privateTextChat(Simulation simulation, SimulationMember member, String memberId, String content) { + ConversationGroup conversationGroup = getPrivateConversation(simulation, member, memberId); + return sendMessage(simulation, member, conversationGroup.getId(), () -> { + VoiceRecognitionResult recognitionResult = new VoiceRecognitionResult(); + recognitionResult.setResult(content); + return recognitionResult; + }); + } + + /** + * 语音私聊 + * @param simulation 仿真 + * @param member 用户 + * @param memberId 指定用户 + * @param fileBase64Str 语音消息 + * @return 消息信息 + */ + public ConversationGroupMessageVO privateAudioChat(Simulation simulation, SimulationMember member, String memberId, String fileBase64Str) { + ConversationGroup conversationGroup = getPrivateConversation(simulation, member, memberId); + return sendMessage(simulation, member, conversationGroup.getId(), () -> { + VoiceRecognitionVO vo = VoiceRecognitionVO.load(fileBase64Str); + return iVoiceService.voiceRecognition(vo); + }); + } + + /** + * 获取私聊会话 + * @param simulation 仿真 + * @param member 发起人 + * @param memberId 对话人 + * @return 会话 + */ + private ConversationGroup getPrivateConversation(Simulation simulation, SimulationMember member, String memberId) { + SimulationMember communicator = simulation.getSimulationMemberById(memberId); + ConversationGroup conversationGroup = simulation.getSimulationConversationGroupMap().values() + .stream().filter(group -> group.isPrivate() && group.contains(communicator)).findFirst().orElse(null); + if (conversationGroup == null) { + Long groupId = simulation.getMaxConversationGroupId(); + conversationGroup = new ConversationGroup(groupId, null, null, simulation.getCorrectSystemTime(), + member, Collections.singletonList(communicator)); + conversationGroup.getMemberList().forEach(ConversationMember::setLeader); + conversationGroup.initPrivateType(); + simulation.addConversationGroup(conversationGroup); + // 语音识别开始 + applicationEventPublisher.publishEvent(new SimulationConversationGroupPrivateChatEvent(this, simulation, conversationGroup)); + } + return conversationGroup; + } + /** * 对群组操作时基础判断 * diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupListener.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupListener.java index 56675fdb4..51213d8c2 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupListener.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupListener.java @@ -3,6 +3,7 @@ package club.joylink.rtss.simulation.cbtc.conversation; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupMessageVO; import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupSocketMessageVO; +import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupVO; import club.joylink.rtss.simulation.cbtc.event.conversation.*; import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import club.joylink.rtss.vo.client.SocketMessageVO; @@ -32,6 +33,20 @@ public class ConversationGroupListener { @Autowired private ConversationGroupHandlerService conversationGroupHandlerService; + + @EventListener + public void handlePrivateChat(SimulationConversationGroupPrivateChatEvent event) { + ConversationGroup conversationGroup = event.getConversationGroup(); + ConversationGroupSocketMessageVO messageVO = ConversationGroupSocketMessageVO.MessageType.JOIN.generateMessageVO(conversationGroup, null); + conversationGroup.getSimulationMemberList().forEach(member -> { + if (!member.isRobot()) { + String name = ConversationGroupVO.getGroupName(conversationGroup, member); + messageVO.setName(name); + doSendMessage(event.getSimulation(), Set.of(member.getUserId()), messageVO); + } + }); + } + /** * 处理创建群组事件 * @param event 事件信息 diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupOperateHandler.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupOperateHandler.java index 9bda5a532..18af3559b 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupOperateHandler.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupOperateHandler.java @@ -111,6 +111,32 @@ public class ConversationGroupOperateHandler { return conversationGroupManagerService.audioBase64(simulation, member, id, fileBase64Str); } + /** + * 跟指定用户文字私聊 + * @param simulation 仿真 + * @param member 发起用户 + * @param memberId 指定用户 + * @param content 文字信息 + * @return 消息信息 + */ + @OperateHandlerMapping(type = Operation.Type.Conversation_Group_Private_Text_Chat) + public ConversationGroupMessageVO privateTextChat(Simulation simulation, SimulationMember member, String memberId, String content) { + return conversationGroupManagerService.privateTextChat(simulation, member, memberId, content); + } + + /** + * 跟指定用户发语音信息 + * @param simulation 仿真信息 + * @param member 发起用户 + * @param memberId 指定用户 + * @param fileBase64Str 语音消息 + * @return 消息内容 + */ + @OperateHandlerMapping(type = Operation.Type.Conversation_Group_Private_Audio_Base64) + public ConversationGroupMessageVO privateAudioChat(Simulation simulation, SimulationMember member, String memberId, String fileBase64Str) { + return conversationGroupManagerService.privateAudioChat(simulation, member, memberId, fileBase64Str); + } + /** * 阅读群组信息 */ diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/data/storage/vo/StorageConversationGroup.java b/src/main/java/club/joylink/rtss/simulation/cbtc/data/storage/vo/StorageConversationGroup.java index 150f50074..d6fd336f8 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/data/storage/vo/StorageConversationGroup.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/data/storage/vo/StorageConversationGroup.java @@ -1,10 +1,7 @@ package club.joylink.rtss.simulation.cbtc.data.storage.vo; import club.joylink.rtss.simulation.cbtc.Simulation; -import club.joylink.rtss.simulation.cbtc.conversation.CommunicationObject; -import club.joylink.rtss.simulation.cbtc.conversation.Conversation; -import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; -import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroupMessage; +import club.joylink.rtss.simulation.cbtc.conversation.*; import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.Getter; import lombok.NoArgsConstructor; @@ -15,6 +12,7 @@ import org.springframework.util.StringUtils; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -42,6 +40,8 @@ public class StorageConversationGroup { private Integer messageId; + private ConversationType type; + /** * 保存背景时会话群组消息 */ @@ -49,6 +49,7 @@ public class StorageConversationGroup { this.id = conversationGroup.getId(); this.imageUrl = conversationGroup.getImageUrl(); this.name = conversationGroup.getName(); + this.type = conversationGroup.getType(); if (conversationGroup.getCreator() != null) { this.creatorId = conversationGroup.getCreator().getId(); } @@ -81,6 +82,11 @@ public class StorageConversationGroup { if (!CollectionUtils.isEmpty(this.messageList)) { this.messageList.forEach(message -> group.addMessage(message.convert2SimulationObj(simulation))); } + if (Objects.equals(this.type, ConversationType.PRIVATE_CHAT)) { + group.initPrivateType(); + } else { + group.initGroupType(); + } return group; } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupSocketMessageVO.java b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupSocketMessageVO.java index 87fb125bb..4d6d7447b 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupSocketMessageVO.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupSocketMessageVO.java @@ -1,10 +1,12 @@ package club.joylink.rtss.simulation.cbtc.data.vo; import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; +import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.Getter; import lombok.Setter; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -18,6 +20,7 @@ public class ConversationGroupSocketMessageVO { /** * 群组名称 */ + @Setter private String name; /** diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupVO.java b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupVO.java index e06129004..4eef49b73 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupVO.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupVO.java @@ -1,6 +1,8 @@ package club.joylink.rtss.simulation.cbtc.data.vo; import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; +import club.joylink.rtss.simulation.cbtc.conversation.ConversationType; +import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -8,6 +10,7 @@ import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.List; +import java.util.Objects; @Getter @Setter @@ -28,6 +31,8 @@ public class ConversationGroupVO { private List messageList; + private ConversationType type; + public ConversationGroupVO(ConversationGroup conversation) { this.id = conversation.getId(); this.name = conversation.getName(); @@ -36,15 +41,25 @@ public class ConversationGroupVO { this.leaderId = conversation.getLeader() != null ? conversation.getLeader().getId() : null; this.memberList = ConversationMemberVO.convert2VOList(conversation.getMemberList()); this.messageList = ConversationGroupMessageVO.convert2VO(conversation.getMessageList()); + this.type = conversation.getType(); } - public static List convert2VOList(List list) { - List voList = new ArrayList<>(); - if (!CollectionUtils.isEmpty(list)) { - for (ConversationGroup conversation : list) { - voList.add(new ConversationGroupVO(conversation)); - } + public ConversationGroupVO(ConversationGroup conversation, SimulationMember member) { + this(conversation); + this.name = getGroupName(conversation, member); + } + + /** + * 私聊的时候名称使用对方的 + * @param conversation 会话 + * @param member 成员 + * @return 名称 + */ + public static String getGroupName(ConversationGroup conversation, SimulationMember member) { + if (conversation.isPrivate()) { + return conversation.getMemberList().stream().filter(c -> !Objects.equals(c.getMember(), member)) + .map(c -> c.getMember().getMemberName()).findFirst().orElse(conversation.getName()); } - return voList; + return conversation.getName(); } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupPrivateChatEvent.java b/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupPrivateChatEvent.java new file mode 100644 index 000000000..264a67c5b --- /dev/null +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupPrivateChatEvent.java @@ -0,0 +1,20 @@ +package club.joylink.rtss.simulation.cbtc.event.conversation; + +import club.joylink.rtss.simulation.cbtc.Simulation; +import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; +import club.joylink.rtss.simulation.cbtc.event.AbstractSimulationEvent; +import lombok.Getter; + +/** + * 私聊会话事件 + */ +@Getter +public class SimulationConversationGroupPrivateChatEvent extends AbstractSimulationEvent { + + private ConversationGroup conversationGroup; + + public SimulationConversationGroupPrivateChatEvent(Object source, Simulation simulation, ConversationGroup conversationGroup) { + super(source, simulation); + this.conversationGroup = conversationGroup; + } +} diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/member/MemberManager.java b/src/main/java/club/joylink/rtss/simulation/cbtc/member/MemberManager.java index f2cb66171..967deac42 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/member/MemberManager.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/member/MemberManager.java @@ -13,6 +13,7 @@ import club.joylink.rtss.simulation.cbtc.event.SimulationUserPlayChangeEvent; import club.joylink.rtss.simulation.cbtc.exception.SimulationException; import club.joylink.rtss.simulation.cbtc.exception.SimulationExceptionType; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.math.NumberUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @@ -179,6 +180,11 @@ public class MemberManager { } if (id == null) { id = simulation.getIdGenerator().nextMemberId(); + } else { + Long generatorId = simulation.getIdGenerator().getCurrentMemberId(); + if (NumberUtils.isCreatable(id) && generatorId < Long.parseLong(id)) { // 如果ID大于当前记录ID则将ID置于当前大小, 防止ID重复导致出错 + simulation.getIdGenerator().setMemberId(Long.parseLong(id)); + } } SimulationMember member = new SimulationMember(id, type, device); member.setName(name);