175 lines
6.7 KiB
Java
175 lines
6.7 KiB
Java
package com.emotion.service.impl;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.emotion.common.BasePageRequest;
|
|
import com.emotion.entity.Conversation;
|
|
import com.emotion.mapper.ConversationMapper;
|
|
import com.emotion.service.ConversationService;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 会话服务实现类
|
|
*
|
|
* @author emotion-museum
|
|
* @date 2025-07-24
|
|
*/
|
|
@Service
|
|
public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Conversation> implements ConversationService {
|
|
|
|
@Override
|
|
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.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);
|
|
}
|
|
|
|
@Override
|
|
public IPage<Conversation> getPageByUserId(BasePageRequest request, String userId) {
|
|
Page<Conversation> page = new Page<>(request.getCurrent(), request.getSize());
|
|
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(Conversation::getUserId, userId)
|
|
.eq(Conversation::getIsDeleted, 0)
|
|
.orderByDesc(Conversation::getCreateTime);
|
|
return this.page(page, wrapper);
|
|
}
|
|
|
|
@Override
|
|
public List<Conversation> getByUserId(String userId) {
|
|
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(Conversation::getUserId, userId)
|
|
.eq(Conversation::getIsDeleted, 0)
|
|
.orderByDesc(Conversation::getCreateTime);
|
|
return this.list(wrapper);
|
|
}
|
|
|
|
@Override
|
|
public List<Conversation> getActiveByUserId(String userId) {
|
|
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(Conversation::getUserId, userId)
|
|
.eq(Conversation::getConversationStatus, "active")
|
|
.eq(Conversation::getIsDeleted, 0)
|
|
.orderByDesc(Conversation::getLastActiveTime);
|
|
return this.list(wrapper);
|
|
}
|
|
|
|
@Override
|
|
public Conversation getByCozeConversationId(String cozeConversationId) {
|
|
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(Conversation::getCozeConversationId, cozeConversationId)
|
|
.eq(Conversation::getIsDeleted, 0);
|
|
return this.getOne(wrapper);
|
|
}
|
|
|
|
@Override
|
|
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
|
|
public Long countByUserId(String userId) {
|
|
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(Conversation::getUserId, userId)
|
|
.eq(Conversation::getIsDeleted, 0);
|
|
return this.count(wrapper);
|
|
}
|
|
|
|
@Override
|
|
public Long countActiveByUserId(String userId) {
|
|
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(Conversation::getUserId, userId)
|
|
.eq(Conversation::getConversationStatus, "active")
|
|
.eq(Conversation::getIsDeleted, 0);
|
|
return this.count(wrapper);
|
|
}
|
|
|
|
@Override
|
|
public List<Conversation> getForArchive(Integer days) {
|
|
LocalDateTime archiveTime = LocalDateTime.now().minusDays(days);
|
|
|
|
LambdaQueryWrapper<Conversation> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(Conversation::getConversationStatus, "active")
|
|
.lt(Conversation::getLastActiveTime, archiveTime)
|
|
.eq(Conversation::getIsDeleted, 0)
|
|
.orderByAsc(Conversation::getLastActiveTime);
|
|
return this.list(wrapper);
|
|
}
|
|
|
|
@Override
|
|
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 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);
|
|
|
|
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);
|
|
}
|
|
} |