feat(chat): 新增删除聊天记录接口并支持逻辑删除
This commit is contained in:
@@ -9,6 +9,7 @@ import com.yolo.keyborad.common.ErrorCode;
|
||||
import com.yolo.keyborad.common.ResultUtils;
|
||||
import com.yolo.keyborad.exception.BusinessException;
|
||||
import com.yolo.keyborad.mapper.QdrantPayloadMapper;
|
||||
import com.yolo.keyborad.model.dto.chat.ChatHistoryDeleteReq;
|
||||
import com.yolo.keyborad.model.dto.chat.ChatHistoryPageReq;
|
||||
import com.yolo.keyborad.model.dto.chat.ChatMessageReq;
|
||||
import com.yolo.keyborad.model.dto.chat.ChatReq;
|
||||
@@ -19,7 +20,6 @@ import com.yolo.keyborad.model.vo.AudioTaskVO;
|
||||
import com.yolo.keyborad.model.vo.ChatMessageHistoryVO;
|
||||
import com.yolo.keyborad.model.vo.ChatMessageVO;
|
||||
import com.yolo.keyborad.model.vo.ChatSessionVO;
|
||||
import com.yolo.keyborad.model.vo.ChatVoiceVO;
|
||||
import com.yolo.keyborad.service.ChatService;
|
||||
import com.yolo.keyborad.service.KeyboardAiChatMessageService;
|
||||
import com.yolo.keyborad.service.KeyboardAiChatSessionService;
|
||||
@@ -138,6 +138,18 @@ public class ChatController {
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
@PostMapping("/history/delete")
|
||||
@Operation(summary = "删除聊天记录", description = "根据聊天记录ID逻辑删除聊天消息")
|
||||
public BaseResponse<Boolean> deleteHistory(@RequestBody ChatHistoryDeleteReq req) {
|
||||
if (req == null || req.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "聊天记录ID不能为空");
|
||||
}
|
||||
|
||||
Long userId = StpUtil.getLoginIdAsLong();
|
||||
aiChatMessageService.deleteMessageById(userId, req.getId());
|
||||
return ResultUtils.success(true);
|
||||
}
|
||||
|
||||
@PostMapping("/session/reset")
|
||||
@Operation(summary = "重置会话", description = "重置与AI角色的聊天会话,将当前会话设为不活跃并创建新会话,后续聊天记录将绑定到新会话")
|
||||
public BaseResponse<ChatSessionVO> resetSession(@RequestBody SessionResetReq req) {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.yolo.keyborad.model.dto.chat;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2026/2/4
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "删除聊天记录请求")
|
||||
public class ChatHistoryDeleteReq {
|
||||
|
||||
@Schema(description = "聊天记录ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private Long id;
|
||||
}
|
||||
|
||||
@@ -79,4 +79,8 @@ public class KeyboardAiChatMessage {
|
||||
@TableField(value = "session_id")
|
||||
@Schema(description = "会话Id")
|
||||
private Long sessionId;
|
||||
|
||||
@TableField(value = "deleted")
|
||||
@Schema(description = "是否删除")
|
||||
private Boolean deleted;
|
||||
}
|
||||
@@ -41,4 +41,12 @@ public interface KeyboardAiChatMessageService extends IService<KeyboardAiChatMes
|
||||
* @return 聊过天的AI角色ID列表(按最近聊天时间倒序)
|
||||
*/
|
||||
List<Long> getChattedCompanionIds(Long userId);
|
||||
|
||||
/**
|
||||
* 根据聊天记录ID逻辑删除消息
|
||||
*
|
||||
* @param userId 当前用户ID
|
||||
* @param messageId 聊天记录ID
|
||||
*/
|
||||
void deleteMessageById(Long userId, Long messageId);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ 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.yolo.keyborad.mapper.KeyboardAiChatMessageMapper;
|
||||
import com.yolo.keyborad.common.ErrorCode;
|
||||
import com.yolo.keyborad.exception.BusinessException;
|
||||
import com.yolo.keyborad.model.entity.KeyboardAiChatMessage;
|
||||
import com.yolo.keyborad.model.entity.KeyboardAiChatSession;
|
||||
import com.yolo.keyborad.model.vo.ChatMessageHistoryVO;
|
||||
@@ -46,6 +48,7 @@ public class KeyboardAiChatMessageServiceImpl extends ServiceImpl<KeyboardAiChat
|
||||
queryWrapper.eq(KeyboardAiChatMessage::getUserId, userId)
|
||||
.eq(KeyboardAiChatMessage::getCompanionId, companionId)
|
||||
.eq(KeyboardAiChatMessage::getSessionId, activeSession.getId())
|
||||
.eq(KeyboardAiChatMessage::getDeleted,false)
|
||||
.orderByDesc(KeyboardAiChatMessage::getCreatedAt)
|
||||
.orderByDesc(KeyboardAiChatMessage::getId);
|
||||
IPage<KeyboardAiChatMessage> entityPage = this.page(page, queryWrapper);
|
||||
@@ -110,4 +113,23 @@ public class KeyboardAiChatMessageServiceImpl extends ServiceImpl<KeyboardAiChat
|
||||
.distinct()
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteMessageById(Long userId, Long messageId) {
|
||||
KeyboardAiChatMessage message = this.getById(messageId);
|
||||
if (message == null || Boolean.TRUE.equals(message.getDeleted())) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "聊天记录不存在");
|
||||
}
|
||||
if (!userId.equals(message.getUserId())) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
|
||||
KeyboardAiChatMessage update = new KeyboardAiChatMessage();
|
||||
update.setId(messageId);
|
||||
update.setDeleted(true);
|
||||
boolean success = this.updateById(update);
|
||||
if (!success) {
|
||||
throw new BusinessException(ErrorCode.OPERATION_ERROR, "删除失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user