|
|
@@ -1,15 +1,20 @@
|
|
1
|
1
|
package com.huimv.employment.service.conversation;
|
|
2
|
2
|
|
|
|
3
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
4
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
3
|
5
|
import com.huimv.employment.dao.entity.FeConversationMessage;
|
|
4
|
6
|
import com.huimv.employment.dao.mapper.FeConversationMessageMapper;
|
|
|
7
|
+import com.huimv.employment.service.conversation.dto.ConversationMessagePageResponse;
|
|
|
8
|
+import com.huimv.employment.service.conversation.dto.ConversationMessageResponse;
|
|
5
|
9
|
import org.springframework.stereotype.Service;
|
|
6
|
10
|
import org.springframework.transaction.annotation.Transactional;
|
|
7
|
11
|
import org.springframework.util.StringUtils;
|
|
8
|
12
|
|
|
9
|
13
|
import java.time.LocalDateTime;
|
|
|
14
|
+import java.util.stream.Collectors;
|
|
10
|
15
|
|
|
11
|
16
|
/**
|
|
12
|
|
- * AI 对话消息落库:user 消息在流式开始前写入,assistant 消息在流式结束后写入。
|
|
|
17
|
+ * AI 对话消息落库与查询。
|
|
13
|
18
|
*/
|
|
14
|
19
|
@Service
|
|
15
|
20
|
public class ConversationMessageService {
|
|
|
@@ -18,6 +23,11 @@ public class ConversationMessageService {
|
|
18
|
23
|
private static final String ROLE_ASSISTANT = "assistant";
|
|
19
|
24
|
private static final String CONTENT_TYPE_TEXT = "text";
|
|
20
|
25
|
|
|
|
26
|
+ private static final int MIN_PAGE = 1;
|
|
|
27
|
+ private static final int MIN_SIZE = 1;
|
|
|
28
|
+ private static final int MAX_SIZE = 200;
|
|
|
29
|
+ private static final int DEFAULT_SIZE = 50;
|
|
|
30
|
+
|
|
21
|
31
|
private final ConversationService conversationService;
|
|
22
|
32
|
private final FeConversationMessageMapper feConversationMessageMapper;
|
|
23
|
33
|
|
|
|
@@ -27,6 +37,30 @@ public class ConversationMessageService {
|
|
27
|
37
|
this.feConversationMessageMapper = feConversationMessageMapper;
|
|
28
|
38
|
}
|
|
29
|
39
|
|
|
|
40
|
+ /**
|
|
|
41
|
+ * 分页查询指定会话下的消息,按时间正序。
|
|
|
42
|
+ */
|
|
|
43
|
+ public ConversationMessagePageResponse listByConversation(Long userId, Long conversationId, Integer page, Integer size) {
|
|
|
44
|
+ conversationService.requireOwned(userId, conversationId);
|
|
|
45
|
+ int resolvedPage = page == null || page < MIN_PAGE ? MIN_PAGE : page;
|
|
|
46
|
+ int resolvedSize = size == null || size < MIN_SIZE ? DEFAULT_SIZE : Math.min(size, MAX_SIZE);
|
|
|
47
|
+
|
|
|
48
|
+ Page<FeConversationMessage> pageParam = new Page<>(resolvedPage, resolvedSize);
|
|
|
49
|
+ LambdaQueryWrapper<FeConversationMessage> wrapper = new LambdaQueryWrapper<FeConversationMessage>()
|
|
|
50
|
+ .eq(FeConversationMessage::getConversationId, conversationId)
|
|
|
51
|
+ .orderByAsc(FeConversationMessage::getCreateTime)
|
|
|
52
|
+ .orderByAsc(FeConversationMessage::getId);
|
|
|
53
|
+ Page<FeConversationMessage> result = feConversationMessageMapper.selectPage(pageParam, wrapper);
|
|
|
54
|
+
|
|
|
55
|
+ ConversationMessagePageResponse response = new ConversationMessagePageResponse();
|
|
|
56
|
+ response.setItems(result.getRecords().stream().map(this::toResponse).collect(Collectors.toList()));
|
|
|
57
|
+ response.setTotal(result.getTotal());
|
|
|
58
|
+ response.setPage(resolvedPage);
|
|
|
59
|
+ response.setSize(resolvedSize);
|
|
|
60
|
+ response.setHasMore((long) resolvedPage * resolvedSize < result.getTotal());
|
|
|
61
|
+ return response;
|
|
|
62
|
+ }
|
|
|
63
|
+
|
|
30
|
64
|
@Transactional(rollbackFor = Exception.class)
|
|
31
|
65
|
public void saveUserMessage(Long conversationId, String userText) {
|
|
32
|
66
|
if (conversationId == null || !StringUtils.hasText(userText)) {
|
|
|
@@ -54,4 +88,16 @@ public class ConversationMessageService {
|
|
54
|
88
|
message.setCreateTime(LocalDateTime.now());
|
|
55
|
89
|
feConversationMessageMapper.insert(message);
|
|
56
|
90
|
}
|
|
|
91
|
+
|
|
|
92
|
+ private ConversationMessageResponse toResponse(FeConversationMessage entity) {
|
|
|
93
|
+ ConversationMessageResponse response = new ConversationMessageResponse();
|
|
|
94
|
+ response.setId(entity.getId());
|
|
|
95
|
+ response.setRole(entity.getRole());
|
|
|
96
|
+ response.setContent(entity.getContent());
|
|
|
97
|
+ response.setContentType(entity.getContentType());
|
|
|
98
|
+ response.setCardPayload(entity.getCardPayload());
|
|
|
99
|
+ response.setNextAction(entity.getNextAction());
|
|
|
100
|
+ response.setCreateTime(entity.getCreateTime());
|
|
|
101
|
+ return response;
|
|
|
102
|
+ }
|
|
57
|
103
|
}
|