This commit is contained in:
2026-01-22 17:33:28 +08:00
parent 1bbf177b2c
commit b46759dc73
105 changed files with 2929 additions and 433 deletions

View File

@@ -0,0 +1,106 @@
package com.hertz.ai.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hertz.ai.entity.Conversation;
import com.hertz.ai.entity.Message;
import com.hertz.ai.mapper.ConversationMapper;
import com.hertz.ai.mapper.MessageMapper;
import com.hertz.ai.service.ConversationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
@Service
@RequiredArgsConstructor
public class ConversationServiceImpl implements ConversationService {
private final ConversationMapper conversationMapper;
private final MessageMapper messageMapper;
@Override
@Transactional
public Conversation createConversation(String title, Long userId) {
Conversation conversation = new Conversation();
conversation.setUserId(userId);
conversation.setTitle(title != null && !title.isEmpty() ? title : "New Chat");
conversation.setCreatedAt(LocalDateTime.now());
conversation.setUpdatedAt(LocalDateTime.now());
conversationMapper.insert(conversation);
return conversation;
}
@Override
public List<Conversation> getConversations(Long userId) {
return conversationMapper.selectList(new LambdaQueryWrapper<Conversation>()
.eq(Conversation::getUserId, userId)
.orderByDesc(Conversation::getUpdatedAt));
}
@Override
@Transactional
public void deleteConversation(Long id, Long userId) {
Conversation conversation = conversationMapper.selectById(id);
if (conversation != null && conversation.getUserId().equals(userId)) {
messageMapper.delete(new LambdaQueryWrapper<Message>()
.eq(Message::getConversationId, id));
conversationMapper.deleteById(id);
} else {
throw new RuntimeException("Conversation not found or access denied");
}
}
@Override
public List<Conversation> searchConversations(String query, Long userId) {
return conversationMapper.selectList(new LambdaQueryWrapper<Conversation>()
.eq(Conversation::getUserId, userId)
.like(Conversation::getTitle, query)
.orderByDesc(Conversation::getUpdatedAt));
}
@Override
@Transactional
public Message saveMessage(Long conversationId, String role, String content) {
Message message = new Message();
message.setConversationId(conversationId);
message.setRole(role);
message.setContent(content);
message.setCreatedAt(LocalDateTime.now());
messageMapper.insert(message);
Conversation conversation = conversationMapper.selectById(conversationId);
if (conversation != null) {
conversation.setUpdatedAt(LocalDateTime.now());
conversationMapper.updateById(conversation);
}
return message;
}
@Override
public List<Message> getMessages(Long conversationId) {
return messageMapper.selectList(new LambdaQueryWrapper<Message>()
.eq(Message::getConversationId, conversationId)
.orderByAsc(Message::getCreatedAt));
}
@Override
public Conversation getConversation(Long id) {
return conversationMapper.selectById(id);
}
@Override
@Transactional
public void updateConversationTitle(Long id, String title, Long userId) {
Conversation conversation = conversationMapper.selectById(id);
if (conversation != null && conversation.getUserId().equals(userId)) {
conversation.setTitle(title);
conversation.setUpdatedAt(LocalDateTime.now());
conversationMapper.updateById(conversation);
} else {
throw new RuntimeException("Conversation not found or access denied");
}
}
}