|
2 | 2 |
|
3 | 3 | import com.back.domain.chatbot.dto.ChatRequestDto; |
4 | 4 | import com.back.domain.chatbot.dto.ChatResponseDto; |
| 5 | +import com.back.domain.chatbot.dto.SaveBotMessageDto; |
5 | 6 | import com.back.domain.chatbot.dto.StepRecommendationResponseDto; |
6 | 7 | import com.back.domain.chatbot.entity.ChatConversation; |
7 | 8 | import com.back.domain.chatbot.enums.MessageSender; |
|
22 | 23 | import org.springframework.core.io.Resource; |
23 | 24 | import org.springframework.data.domain.Page; |
24 | 25 | import org.springframework.data.domain.PageRequest; |
25 | | -import org.springframework.data.domain.Pageable; |
26 | 26 | import org.springframework.stereotype.Service; |
27 | 27 | import org.springframework.transaction.annotation.Transactional; |
28 | 28 | import org.springframework.util.StreamUtils; |
@@ -133,6 +133,11 @@ public ChatResponseDto sendMessage(ChatRequestDto requestDto) { |
133 | 133 | } |
134 | 134 | } |
135 | 135 |
|
| 136 | + // ============ 수정된 메서드들 ============ |
| 137 | + |
| 138 | + /** |
| 139 | + * 대화 컨텍스트 빌드 - 변경사항: sender로 구분하여 대화 재구성 |
| 140 | + */ |
136 | 141 | private String buildConversationContext(List<ChatConversation> recentChats) { |
137 | 142 | if (recentChats.isEmpty()) { |
138 | 143 | return ""; |
@@ -160,7 +165,7 @@ private String buildConversationContext(List<ChatConversation> recentChats) { |
160 | 165 | * 대화 저장 - 변경사항: 사용자 메시지와 봇 응답을 각각 별도로 저장 |
161 | 166 | */ |
162 | 167 | @Transactional |
163 | | - public void saveConversation(ChatRequestDto requestDto, String response) { |
| 168 | + private void saveConversation(ChatRequestDto requestDto, String response) { |
164 | 169 | // 1. 사용자 메시지 저장 |
165 | 170 | ChatConversation userMessage = ChatConversation.builder() |
166 | 171 | .userId(requestDto.getUserId()) |
@@ -188,6 +193,51 @@ public List<ChatConversation> getUserChatHistory(Long userId) { |
188 | 193 | return chatConversationRepository.findByUserIdOrderByCreatedAtDesc(userId); |
189 | 194 | } |
190 | 195 |
|
| 196 | + /** |
| 197 | + * FE에서 생성한 봇 메시지를 DB에 저장 |
| 198 | + * 예: 인사말, 안내 메시지, 에러 메시지 등 |
| 199 | + */ |
| 200 | + @Transactional |
| 201 | + public ChatConversation saveBotMessage(SaveBotMessageDto dto) { |
| 202 | + ChatConversation botMessage = ChatConversation.builder() |
| 203 | + .userId(dto.getUserId()) |
| 204 | + .message(dto.getMessage()) |
| 205 | + .sender(MessageSender.CHATBOT) |
| 206 | + .createdAt(LocalDateTime.now()) |
| 207 | + .build(); |
| 208 | + |
| 209 | + return chatConversationRepository.save(botMessage); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * 기본 인사말 생성 및 저장 |
| 214 | + * 채팅 시작 시 호출하여 인사말을 DB에 저장 |
| 215 | + */ |
| 216 | + @Transactional |
| 217 | + public ChatConversation createGreetingMessage(Long userId) { |
| 218 | + String greetingMessage = "안녕하세요! 🍹 바텐더 '쑤리'에요.\n" + |
| 219 | + "취향에 맞는 칵테일을 추천해드릴게요!\n" + |
| 220 | + "어떤 유형으로 찾아드릴까요?"; |
| 221 | + |
| 222 | + ChatConversation greeting = ChatConversation.builder() |
| 223 | + .userId(userId) |
| 224 | + .message(greetingMessage) |
| 225 | + .sender(MessageSender.CHATBOT) |
| 226 | + .createdAt(LocalDateTime.now()) |
| 227 | + .build(); |
| 228 | + |
| 229 | + return chatConversationRepository.save(greeting); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * 사용자의 첫 대화 여부 확인 |
| 234 | + * 첫 대화인 경우 인사말 자동 생성에 활용 가능 |
| 235 | + */ |
| 236 | + @Transactional(readOnly = true) |
| 237 | + public boolean isFirstConversation(Long userId) { |
| 238 | + return chatConversationRepository.findTop10ByUserIdOrderByCreatedAtDesc(userId).isEmpty(); |
| 239 | + } |
| 240 | + |
191 | 241 | // ============ 기존 메서드들 (변경 없음) ============ |
192 | 242 |
|
193 | 243 | private String buildSystemMessage(MessageType type) { |
|
0 commit comments