1.未读数优化
This commit is contained in:
zhongzb
2023-08-20 18:55:22 +08:00
parent 68a4296b7b
commit cf707a4473
4 changed files with 5 additions and 13 deletions

View File

@@ -25,5 +25,5 @@ public interface ContactService {
Integer getMsgUnReadCount(Message message);
Map<Long, MsgReadInfoDTO> getMsgReadInfo(List<Message> messages, boolean needUnread);
Map<Long, MsgReadInfoDTO> getMsgReadInfo(List<Message> messages);
}

View File

@@ -51,23 +51,17 @@ public class ContactServiceImpl implements ContactService {
}
@Override
public Map<Long, MsgReadInfoDTO> getMsgReadInfo(List<Message> messages, boolean needUnread) {
public Map<Long, MsgReadInfoDTO> getMsgReadInfo(List<Message> messages) {
Map<Long, List<Message>> roomGroup = messages.stream().collect(Collectors.groupingBy(Message::getRoomId));
AssertUtil.equal(roomGroup.size(), 1, "只能查相同房间下的消息");
Long roomId = roomGroup.keySet().iterator().next();
Integer totalCount = null;
if (needUnread) {
totalCount = contactDao.getTotalCount(roomId);
}
Integer finalTotalCount = totalCount;
Integer totalCount = contactDao.getTotalCount(roomId);
return messages.stream().map(message -> {
MsgReadInfoDTO readInfoDTO = new MsgReadInfoDTO();
readInfoDTO.setMsgId(message.getId());
Integer readCount = contactDao.getReadCount(message);
readInfoDTO.setReadCount(readCount);
if (needUnread) {
readInfoDTO.setUnReadCount(finalTotalCount - readCount - 1);
}
readInfoDTO.setUnReadCount(totalCount - readCount - 1);
return readInfoDTO;
}).collect(Collectors.toMap(MsgReadInfoDTO::getMsgId, Function.identity()));
}