服务层重构与优化:补全所有ServiceImpl实现类,修复RestTemplate注入,完善DTO与配置,保证编译与启动通过
This commit is contained in:
+85
-139
@@ -1,7 +1,6 @@
|
||||
package com.emotion.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -14,13 +13,12 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 对话服务实现类
|
||||
*
|
||||
* 会话服务实现类
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
* @date 2025-07-24
|
||||
*/
|
||||
@Service
|
||||
public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Conversation> implements ConversationService {
|
||||
@@ -29,25 +27,13 @@ public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Con
|
||||
public IPage<Conversation> getPage(BasePageRequest request) {
|
||||
Page<Conversation> page = new Page<>(request.getCurrent(), request.getSize());
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
// 关键词搜索
|
||||
|
||||
if (StringUtils.hasText(request.getKeyword())) {
|
||||
wrapper.like(Conversation::getTitle, request.getKeyword());
|
||||
}
|
||||
|
||||
wrapper.eq(Conversation::getIsDeleted, 0);
|
||||
|
||||
// 排序
|
||||
if (StringUtils.hasText(request.getOrderBy())) {
|
||||
if ("asc".equalsIgnoreCase(request.getOrderDirection())) {
|
||||
wrapper.orderByAsc(Conversation::getCreateTime);
|
||||
} else {
|
||||
wrapper.orderByDesc(Conversation::getCreateTime);
|
||||
}
|
||||
} else {
|
||||
wrapper.orderByDesc(Conversation::getCreateTime);
|
||||
wrapper.and(w -> w.like(Conversation::getTitle, request.getKeyword())
|
||||
.or().like(Conversation::getSummary, request.getKeyword()));
|
||||
}
|
||||
|
||||
wrapper.eq(Conversation::getIsDeleted, 0).orderByDesc(Conversation::getCreateTime);
|
||||
return this.page(page, wrapper);
|
||||
}
|
||||
|
||||
@@ -56,14 +42,8 @@ public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Con
|
||||
Page<Conversation> page = new Page<>(request.getCurrent(), request.getSize());
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getUserId, userId)
|
||||
.eq(Conversation::getIsDeleted, 0);
|
||||
|
||||
// 关键词搜索
|
||||
if (StringUtils.hasText(request.getKeyword())) {
|
||||
wrapper.like(Conversation::getTitle, request.getKeyword());
|
||||
}
|
||||
|
||||
wrapper.orderByDesc(Conversation::getCreateTime);
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getCreateTime);
|
||||
return this.page(page, wrapper);
|
||||
}
|
||||
|
||||
@@ -77,50 +57,52 @@ public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Con
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Conversation> getByType(String type) {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getType, type)
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Conversation> getByStatus(String status) {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getStatus, status)
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Conversation> getByUserIdAndType(String userId, String type) {
|
||||
public List<Conversation> getActiveByUserId(String userId) {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getUserId, userId)
|
||||
.eq(Conversation::getType, type)
|
||||
.eq(Conversation::getConversationStatus, "active")
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getCreateTime);
|
||||
.orderByDesc(Conversation::getLastActiveTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Conversation> getByUserIdAndStatus(String userId, String status) {
|
||||
public Conversation getByCozeConversationId(String cozeConversationId) {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getUserId, userId)
|
||||
.eq(Conversation::getStatus, status)
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
wrapper.eq(Conversation::getCozeConversationId, cozeConversationId)
|
||||
.eq(Conversation::getIsDeleted, 0);
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Conversation> getByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.between(Conversation::getCreateTime, startTime, endTime)
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
public boolean updateMessageCount(String conversationId, Integer messageCount) {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setId(conversationId);
|
||||
conversation.setMessageCount(messageCount);
|
||||
return this.updateById(conversation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateStatus(String conversationId, Integer status) {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setId(conversationId);
|
||||
// 根据status值设置对应的状态字符串
|
||||
String statusStr = "active";
|
||||
if (status == 1) {
|
||||
statusStr = "ended";
|
||||
} else if (status == 2) {
|
||||
statusStr = "archived";
|
||||
}
|
||||
conversation.setConversationStatus(statusStr);
|
||||
return this.updateById(conversation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEndTime(String conversationId, LocalDateTime endTime) {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setId(conversationId);
|
||||
conversation.setEndTime(endTime);
|
||||
return this.updateById(conversation);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -132,98 +114,62 @@ public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Con
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByType(String type) {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getType, type)
|
||||
.eq(Conversation::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByStatus(String status) {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getStatus, status)
|
||||
.eq(Conversation::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Conversation> getRecentByUserId(String userId, Integer limit) {
|
||||
public Long countActiveByUserId(String userId) {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getUserId, userId)
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getCreateTime)
|
||||
.last("LIMIT " + limit);
|
||||
return this.list(wrapper);
|
||||
.eq(Conversation::getConversationStatus, "active")
|
||||
.eq(Conversation::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Conversation> getActiveConversations() {
|
||||
public List<Conversation> getForArchive(Integer days) {
|
||||
LocalDateTime archiveTime = LocalDateTime.now().minusDays(days);
|
||||
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getStatus, "active")
|
||||
wrapper.eq(Conversation::getConversationStatus, "active")
|
||||
.lt(Conversation::getLastActiveTime, archiveTime)
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getLastMessageTime);
|
||||
.orderByAsc(Conversation::getLastActiveTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Conversation> getArchivedConversations() {
|
||||
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Conversation::getStatus, "archived")
|
||||
.eq(Conversation::getIsDeleted, 0)
|
||||
.orderByDesc(Conversation::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
public boolean batchArchive(List<String> conversationIds) {
|
||||
for (String conversationId : conversationIds) {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setId(conversationId);
|
||||
conversation.setConversationStatus("archived");
|
||||
this.updateById(conversation);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateMessageCount(String id, Integer increment) {
|
||||
LambdaUpdateWrapper<Conversation> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(Conversation::getId, id)
|
||||
.setSql("message_count = message_count + " + increment)
|
||||
.set(Conversation::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
public Conversation createConversation(String userId, String title, String cozeConversationId) {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setUserId(userId);
|
||||
conversation.setTitle(title);
|
||||
conversation.setCozeConversationId(cozeConversationId);
|
||||
conversation.setUserType("registered");
|
||||
conversation.setType("chat");
|
||||
conversation.setConversationStatus("active");
|
||||
conversation.setStartTime(LocalDateTime.now());
|
||||
conversation.setLastActiveTime(LocalDateTime.now());
|
||||
conversation.setMessageCount(0);
|
||||
conversation.setTotalTokens(0);
|
||||
conversation.setTotalCost(java.math.BigDecimal.ZERO);
|
||||
|
||||
@Override
|
||||
public boolean updateLastMessageTime(String id, LocalDateTime lastMessageTime) {
|
||||
LambdaUpdateWrapper<Conversation> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(Conversation::getId, id)
|
||||
.set(Conversation::getLastMessageTime, lastMessageTime)
|
||||
.set(Conversation::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateStatus(String id, String status) {
|
||||
LambdaUpdateWrapper<Conversation> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(Conversation::getId, id)
|
||||
.set(Conversation::getStatus, status)
|
||||
.set(Conversation::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean archiveConversation(String id) {
|
||||
return updateStatus(id, "archived");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean activateConversation(String id) {
|
||||
return updateStatus(id, "active");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Conversation createConversation(String userId, String title, String type, String clientIp) {
|
||||
Conversation conversation = Conversation.builder()
|
||||
.id(UUID.randomUUID().toString())
|
||||
.userId(userId)
|
||||
.title(title)
|
||||
.type(type)
|
||||
.status("active")
|
||||
.messageCount(0)
|
||||
.clientIp(clientIp)
|
||||
.build();
|
||||
this.save(conversation);
|
||||
return conversation;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean endConversation(String conversationId) {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setId(conversationId);
|
||||
conversation.setConversationStatus("ended");
|
||||
conversation.setEndTime(LocalDateTime.now());
|
||||
return this.updateById(conversation);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user