|
|
@@ -27,8 +27,10 @@ import java.time.ZoneId;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@@ -212,20 +214,18 @@ public class ICorporateWeChatSpaceServiceImpl implements ICorporateWeChatSpaceSe
|
|
|
JSONObject requestData = new JSONObject();
|
|
|
requestData.put("query_word", request.getQueryWord());
|
|
|
|
|
|
- // 构造 chat_info
|
|
|
+ // 构造 chat_info(与之前相同,省略)
|
|
|
if (request.getChatType() != null) {
|
|
|
JSONObject chatInfo = new JSONObject();
|
|
|
chatInfo.put("chat_type", request.getChatType());
|
|
|
-
|
|
|
- if (request.getChatType() == 1) { // 单聊
|
|
|
+ if (request.getChatType() == 1) {
|
|
|
JSONArray idList = new JSONArray();
|
|
|
idList.add(new JSONObject().fluentPut("open_userid", request.getStaffUserId()));
|
|
|
idList.add(new JSONObject().fluentPut("external_userid", request.getCustomerId()));
|
|
|
chatInfo.put("id_list", idList);
|
|
|
- } else if (request.getChatType() == 2) { // 群聊
|
|
|
+ } else if (request.getChatType() == 2) {
|
|
|
chatInfo.put("chat_id", request.getChatId());
|
|
|
}
|
|
|
-
|
|
|
if (request.getMsgTypeList() != null && !request.getMsgTypeList().isEmpty()) {
|
|
|
chatInfo.put("msg_type_list", request.getMsgTypeList());
|
|
|
}
|
|
|
@@ -265,30 +265,40 @@ public class ICorporateWeChatSpaceServiceImpl implements ICorporateWeChatSpaceSe
|
|
|
|
|
|
// 4. 提取 msgid 列表
|
|
|
JSONArray msgListArray = respData.getJSONArray("msg_list");
|
|
|
- if (msgListArray == null || msgListArray.isEmpty()) {
|
|
|
- return new SearchResultVO(new JSONArray(), 0, "");
|
|
|
- }
|
|
|
-
|
|
|
List<String> msgIds = new ArrayList<>();
|
|
|
- for (Object obj : msgListArray) {
|
|
|
- JSONObject item = (JSONObject) obj;
|
|
|
- msgIds.add(item.getString("msgid"));
|
|
|
+ if (msgListArray != null) {
|
|
|
+ for (Object obj : msgListArray) {
|
|
|
+ msgIds.add(((JSONObject) obj).getString("msgid"));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 5. 从本地数据库批量查询消息详情
|
|
|
- List<QwConversationMessage> messages = messageMapper.selectByMsgIds(request.getCorpId(), msgIds);
|
|
|
- if (messages.isEmpty()) {
|
|
|
- return new SearchResultVO(new JSONArray(), respData.getIntValue("has_more"), respData.getString("next_cursor"));
|
|
|
+ // 5. 从本地数据库查询消息详情(按 msgid 顺序)
|
|
|
+ List<QwConversationMessage> messages = new ArrayList<>();
|
|
|
+ if (!msgIds.isEmpty()) {
|
|
|
+ // 注意:selectByMsgIds 应保持与 msgIds 相同的顺序(可额外排序)
|
|
|
+ messages = messageMapper.selectByMsgIdsOrderByMsgId(request.getCorpId(), msgIds);
|
|
|
+ // 如果 mapper 不支持顺序,可手动排序
|
|
|
+ Map<String, QwConversationMessage> msgMap = messages.stream()
|
|
|
+ .collect(Collectors.toMap(QwConversationMessage::getMsgid, Function.identity()));
|
|
|
+ List<QwConversationMessage> ordered = new ArrayList<>();
|
|
|
+ for (String id : msgIds) {
|
|
|
+ QwConversationMessage m = msgMap.get(id);
|
|
|
+ if (m != null) ordered.add(m);
|
|
|
+ }
|
|
|
+ messages = ordered;
|
|
|
}
|
|
|
|
|
|
- // 6. 构建返回数据
|
|
|
+ // 6. 构建返回数据(格式与 fetchConversations 保持一致)
|
|
|
JSONArray resultList = new JSONArray();
|
|
|
for (QwConversationMessage msg : messages) {
|
|
|
JSONObject item = buildMessageJson(msg, request.getCorpId());
|
|
|
resultList.add(item);
|
|
|
}
|
|
|
|
|
|
- return new SearchResultVO(resultList, respData.getIntValue("has_more"), respData.getString("next_cursor"));
|
|
|
+ // 7. 返回游标分页结果
|
|
|
+ Integer hasMore = respData.getIntValue("has_more");
|
|
|
+ String nextCursor = respData.getString("next_cursor");
|
|
|
+ return new SearchResultVO(resultList, hasMore, nextCursor);
|
|
|
}
|
|
|
|
|
|
/**
|