|
@@ -9,14 +9,15 @@ import io.github.nnkwrik.imservice.dao.ChatMapper;
|
|
import io.github.nnkwrik.imservice.dao.HistoryMapper;
|
|
import io.github.nnkwrik.imservice.dao.HistoryMapper;
|
|
import io.github.nnkwrik.imservice.model.po.History;
|
|
import io.github.nnkwrik.imservice.model.po.History;
|
|
import io.github.nnkwrik.imservice.model.po.HistoryExample;
|
|
import io.github.nnkwrik.imservice.model.po.HistoryExample;
|
|
-import io.github.nnkwrik.imservice.model.po.LastChat;
|
|
|
|
import io.github.nnkwrik.imservice.model.vo.ChatIndex;
|
|
import io.github.nnkwrik.imservice.model.vo.ChatIndex;
|
|
|
|
+import io.github.nnkwrik.imservice.model.vo.WsMessage;
|
|
import io.github.nnkwrik.imservice.redis.RedisClient;
|
|
import io.github.nnkwrik.imservice.redis.RedisClient;
|
|
import io.github.nnkwrik.imservice.service.IndexService;
|
|
import io.github.nnkwrik.imservice.service.IndexService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -53,17 +54,18 @@ public class IndexServiceImpl implements IndexService {
|
|
Map<Integer, String> chatUserMap = new HashMap<>(); //k=chatId,v=需要去用户服务查的id
|
|
Map<Integer, String> chatUserMap = new HashMap<>(); //k=chatId,v=需要去用户服务查的id
|
|
|
|
|
|
List<Integer> chatIds = chatMapper.getChatIdsByUser(currentUser);
|
|
List<Integer> chatIds = chatMapper.getChatIdsByUser(currentUser);
|
|
- List<LastChat> unreadMessage = redisClient.multiGet(
|
|
|
|
|
|
+ List<List<WsMessage>> unreadMessage = redisClient.multiGet(
|
|
chatIds.stream()
|
|
chatIds.stream()
|
|
.map(id -> id + "")
|
|
.map(id -> id + "")
|
|
.collect(Collectors.toList()));
|
|
.collect(Collectors.toList()));
|
|
|
|
|
|
//从redis(未读)和sql(已读)中各尝试取10个
|
|
//从redis(未读)和sql(已读)中各尝试取10个
|
|
- List<LastChat> unread = getDisplayUnread(unreadMessage, size, offsetTime);
|
|
|
|
- dealUnread(currentUser, unread, resultVoList, chatGoodsMap, chatUserMap);
|
|
|
|
|
|
+ Map<Integer, Integer> unreadCount = new HashMap<>();
|
|
|
|
+ List<WsMessage> unread = getDisplayUnread(unreadMessage, unreadCount, size, offsetTime);
|
|
|
|
+ dealUnread(currentUser, unread, unreadCount, resultVoList, chatGoodsMap, chatUserMap);
|
|
|
|
|
|
List<Integer> unreadChatIds = unread.stream()
|
|
List<Integer> unreadChatIds = unread.stream()
|
|
- .map(chat -> chat.getLastMsg().getChatId())
|
|
|
|
|
|
+ .map(chat -> chat.getChatId())
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
PageHelper.offsetPage(0, size);
|
|
PageHelper.offsetPage(0, size);
|
|
List<HistoryExample> read = historyMapper.getLastReadChat(unreadChatIds, currentUser, offsetTime);
|
|
List<HistoryExample> read = historyMapper.getLastReadChat(unreadChatIds, currentUser, offsetTime);
|
|
@@ -73,7 +75,7 @@ public class IndexServiceImpl implements IndexService {
|
|
resultVoList = sortAndLimitMsg(size, resultVoList, chatGoodsMap, chatUserMap);
|
|
resultVoList = sortAndLimitMsg(size, resultVoList, chatGoodsMap, chatUserMap);
|
|
|
|
|
|
|
|
|
|
- //添加用户和商品信息
|
|
|
|
|
|
+ //添加用户和商品信息,offsetTime
|
|
resultVoList = setGoodsAndUser4Chat(resultVoList, chatGoodsMap, chatUserMap);
|
|
resultVoList = setGoodsAndUser4Chat(resultVoList, chatGoodsMap, chatUserMap);
|
|
|
|
|
|
|
|
|
|
@@ -84,12 +86,12 @@ public class IndexServiceImpl implements IndexService {
|
|
public int getUnreadCount(String userId) {
|
|
public int getUnreadCount(String userId) {
|
|
//去查userId参与的chat的id
|
|
//去查userId参与的chat的id
|
|
List chatIdList = chatMapper.getChatIdsByUser(userId);
|
|
List chatIdList = chatMapper.getChatIdsByUser(userId);
|
|
- List<LastChat> lastChatList = redisClient.multiGet(chatIdList);
|
|
|
|
|
|
+ List<List<WsMessage>> lastChatList = redisClient.multiGet(chatIdList);
|
|
|
|
|
|
//过滤自己发送的
|
|
//过滤自己发送的
|
|
int unreadCount = lastChatList.stream()
|
|
int unreadCount = lastChatList.stream()
|
|
- .filter(chat -> !chat.getLastMsg().getSenderId().equals(userId))
|
|
|
|
- .mapToInt(LastChat::getUnreadCount)
|
|
|
|
|
|
+ .filter(chat -> !chat.get(0).getSenderId().equals(userId))
|
|
|
|
+ .mapToInt(List::size)
|
|
.sum();
|
|
.sum();
|
|
|
|
|
|
return unreadCount;
|
|
return unreadCount;
|
|
@@ -97,40 +99,49 @@ public class IndexServiceImpl implements IndexService {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- private List<LastChat> getDisplayUnread(List<LastChat> unreadMessage, int size, Date offsetTime) {
|
|
|
|
- List<LastChat> displayUnread = unreadMessage.stream()
|
|
|
|
- .filter(msg -> msg != null && offsetTime.compareTo(msg.getLastMsg().getSendTime()) > 0)
|
|
|
|
- .sorted((a, b) -> b.getLastMsg().getSendTime().compareTo(a.getLastMsg().getSendTime()))
|
|
|
|
|
|
+ private List<WsMessage> getDisplayUnread(List<List<WsMessage>> unreadMessage,
|
|
|
|
+ Map<Integer, Integer> unreadCount,
|
|
|
|
+ int size, Date offsetTime) {
|
|
|
|
+ if (ObjectUtils.isEmpty(unreadMessage)) return new ArrayList<>();
|
|
|
|
+ List<WsMessage> displayUnread = unreadMessage.stream()
|
|
|
|
+ .map(msgList -> {
|
|
|
|
+ unreadCount.put(msgList.get(0).getChatId(), msgList.size());
|
|
|
|
+ return msgList.get(msgList.size() - 1);
|
|
|
|
+ })
|
|
|
|
+ .filter(msg -> msg != null && offsetTime.compareTo(msg.getSendTime()) > 0)
|
|
|
|
+ .sorted((a, b) -> b.getSendTime().compareTo(a.getSendTime()))
|
|
.limit(size)
|
|
.limit(size)
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
return displayUnread;
|
|
return displayUnread;
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
private void dealUnread(String currentUserId,
|
|
private void dealUnread(String currentUserId,
|
|
- List<LastChat> unread,
|
|
|
|
|
|
+ List<WsMessage> unread,
|
|
|
|
+ Map<Integer, Integer> unreadCount,
|
|
List<ChatIndex> resultVoList,
|
|
List<ChatIndex> resultVoList,
|
|
Map<Integer, Integer> chatGoodsMap,
|
|
Map<Integer, Integer> chatGoodsMap,
|
|
Map<Integer, String> chatUserMap) {
|
|
Map<Integer, String> chatUserMap) {
|
|
|
|
|
|
unread.stream().forEach(po -> {
|
|
unread.stream().forEach(po -> {
|
|
//稍后去其他服务查询
|
|
//稍后去其他服务查询
|
|
- chatGoodsMap.put(po.getLastMsg().getChatId(), po.getLastMsg().getGoodsId());
|
|
|
|
|
|
+ chatGoodsMap.put(po.getChatId(), po.getGoodsId());
|
|
|
|
|
|
|
|
|
|
//设置未读数,是自己发送的则不显示未读消息数
|
|
//设置未读数,是自己发送的则不显示未读消息数
|
|
ChatIndex vo = new ChatIndex();
|
|
ChatIndex vo = new ChatIndex();
|
|
- if (po.getLastMsg().getSenderId().equals(currentUserId)) {
|
|
|
|
- chatUserMap.put(po.getLastMsg().getChatId(), po.getLastMsg().getReceiverId());
|
|
|
|
|
|
+ if (po.getSenderId().equals(currentUserId)) {
|
|
|
|
+ chatUserMap.put(po.getChatId(), po.getReceiverId());
|
|
vo.setUnreadCount(0);
|
|
vo.setUnreadCount(0);
|
|
} else {
|
|
} else {
|
|
- vo.setUnreadCount(po.getUnreadCount());
|
|
|
|
- chatUserMap.put(po.getLastMsg().getChatId(), po.getLastMsg().getSenderId());
|
|
|
|
|
|
+ vo.setUnreadCount(unreadCount.get(po.getChatId()));
|
|
|
|
+ chatUserMap.put(po.getChatId(), po.getSenderId());
|
|
}
|
|
}
|
|
|
|
|
|
//设置最后一条信息
|
|
//设置最后一条信息
|
|
History lastChat = new History();
|
|
History lastChat = new History();
|
|
- BeanUtils.copyProperties(po.getLastMsg(), lastChat);
|
|
|
|
|
|
+ BeanUtils.copyProperties(po, lastChat);
|
|
vo.setLastChat(lastChat);
|
|
vo.setLastChat(lastChat);
|
|
|
|
|
|
resultVoList.add(vo);
|
|
resultVoList.add(vo);
|
|
@@ -167,9 +178,9 @@ public class IndexServiceImpl implements IndexService {
|
|
}
|
|
}
|
|
|
|
|
|
private List<ChatIndex> sortAndLimitMsg(int size,
|
|
private List<ChatIndex> sortAndLimitMsg(int size,
|
|
- List<ChatIndex> voList,
|
|
|
|
- Map<Integer, Integer> chatGoodsMap,
|
|
|
|
- Map<Integer, String> chatUserMap) {
|
|
|
|
|
|
+ List<ChatIndex> voList,
|
|
|
|
+ Map<Integer, Integer> chatGoodsMap,
|
|
|
|
+ Map<Integer, String> chatUserMap) {
|
|
List<ChatIndex> limited = voList.stream()
|
|
List<ChatIndex> limited = voList.stream()
|
|
.sorted((a, b) -> b.getLastChat().getSendTime().compareTo(a.getLastChat().getSendTime()))
|
|
.sorted((a, b) -> b.getLastChat().getSendTime().compareTo(a.getLastChat().getSendTime()))
|
|
.limit(size)
|
|
.limit(size)
|
|
@@ -225,6 +236,8 @@ public class IndexServiceImpl implements IndexService {
|
|
}
|
|
}
|
|
vo.setGoods(simpleGoods);
|
|
vo.setGoods(simpleGoods);
|
|
|
|
|
|
|
|
+ vo.setOffsetTime(vo.getLastChat().getSendTime());
|
|
|
|
+
|
|
});
|
|
});
|
|
|
|
|
|
return voList;
|
|
return voList;
|