147 lines
5.7 KiB
Java
147 lines
5.7 KiB
Java
package com.emotion.controller;
|
|
|
|
import com.emotion.common.PageResult;
|
|
import com.emotion.common.Result;
|
|
import com.emotion.dto.request.MessageCreateRequest;
|
|
import com.emotion.dto.request.MessagePageRequest;
|
|
import com.emotion.dto.request.MessageSearchRequest;
|
|
import com.emotion.dto.request.MessageRecentRequest;
|
|
import com.emotion.dto.response.MessageResponse;
|
|
import com.emotion.service.MessageService;
|
|
import com.emotion.service.ScriptMessageService;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.validation.Valid;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 消息控制器
|
|
*
|
|
* @author huazhongmin
|
|
* @date 2025-09-08
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/message")
|
|
@Slf4j
|
|
@Tag(name = "消息管理", description = "会话消息的查询、创建、搜索、更新和删除接口")
|
|
public class MessageController {
|
|
|
|
@Autowired
|
|
private MessageService messageService;
|
|
|
|
@Autowired
|
|
private ScriptMessageService scriptMessageService;
|
|
|
|
/**
|
|
* 创建消息
|
|
*/
|
|
@Operation(summary = "创建消息", description = "在指定会话中创建一条新消息。")
|
|
@PostMapping(value = "/create")
|
|
public Result<MessageResponse> create(@Valid @RequestBody MessageCreateRequest request) {
|
|
MessageResponse response = messageService.createMessageFromRequest(request);
|
|
return Result.success(response);
|
|
}
|
|
|
|
/**
|
|
* 根据ID获取消息
|
|
*/
|
|
@Operation(summary = "获取消息详情", description = "根据 ID 获取消息的详细信息。")
|
|
@GetMapping(value = "/detail")
|
|
public Result<MessageResponse> getById(@Parameter(description = "消息 ID") @RequestParam String id) {
|
|
MessageResponse response = messageService.getMessageById(id);
|
|
if (response == null) {
|
|
return Result.notFound("消息不存在");
|
|
}
|
|
return Result.success(response);
|
|
}
|
|
|
|
/**
|
|
* 分页查询消息
|
|
*/
|
|
@Operation(summary = "分页查询消息", description = "分页查询指定会话的消息列表。")
|
|
@GetMapping(value = "/page")
|
|
public Result<PageResult<MessageResponse>> getPage(@Validated MessagePageRequest request) {
|
|
PageResult<MessageResponse> pageResult = messageService.getPageWithResponse(request);
|
|
return Result.success(pageResult);
|
|
}
|
|
|
|
/**
|
|
* 搜索消息
|
|
*/
|
|
@Operation(summary = "搜索消息", description = "根据关键词在指定会话中搜索消息内容。")
|
|
@PostMapping(value = "/search")
|
|
public Result<PageResult<MessageResponse>> search(@Valid @RequestBody MessageSearchRequest request) {
|
|
PageResult<MessageResponse> pageResult = messageService.searchWithResponse(request);
|
|
return Result.success(pageResult);
|
|
}
|
|
|
|
/**
|
|
* 获取最近的消息
|
|
*/
|
|
@Operation(summary = "获取最近消息", description = "获取指定会话最近的消息列表。")
|
|
@PostMapping(value = "/recent")
|
|
public Result<PageResult<MessageResponse>> getRecentMessages(@Valid @RequestBody MessageRecentRequest request) {
|
|
PageResult<MessageResponse> pageResult = messageService.getRecentWithResponse(request);
|
|
return Result.success(pageResult);
|
|
}
|
|
|
|
@Operation(summary = "获取会话消息列表", description = "获取指定会话下的完整消息历史。")
|
|
@GetMapping(value = "/listByConversation")
|
|
public Result<List<MessageResponse>> listByConversation(
|
|
@Parameter(description = "会话 ID") @RequestParam String conversationId,
|
|
@RequestParam(required = false, defaultValue = "false") Boolean includeVersions) {
|
|
List<MessageResponse> messages = scriptMessageService.listByConversation(conversationId, includeVersions);
|
|
return Result.success(messages);
|
|
}
|
|
|
|
@Operation(summary = "获取消息版本链", description = "获取指定剧本消息的所有历史版本。")
|
|
@GetMapping(value = "/versions")
|
|
public Result<List<MessageResponse>> getVersions(
|
|
@Parameter(description = "消息 ID") @RequestParam String messageId) {
|
|
List<MessageResponse> versions = scriptMessageService.getVersions(messageId);
|
|
return Result.success(versions);
|
|
}
|
|
|
|
/**
|
|
* 更新消息
|
|
*/
|
|
@Operation(summary = "更新消息", description = "修改指定消息的内容。")
|
|
@PutMapping(value = "/update")
|
|
public Result<MessageResponse> update(@RequestParam String id, @RequestParam String content) {
|
|
MessageResponse response = messageService.updateMessage(id, content);
|
|
if (response == null) {
|
|
return Result.notFound("消息不存在");
|
|
}
|
|
return Result.success(response);
|
|
}
|
|
|
|
/**
|
|
* 删除消息
|
|
*/
|
|
@Operation(summary = "删除消息", description = "删除指定的消息。")
|
|
@DeleteMapping(value = "/delete")
|
|
public Result<Void> delete(@Parameter(description = "消息 ID") @RequestParam String id) {
|
|
boolean deleted = messageService.deleteMessage(id);
|
|
if (!deleted) {
|
|
return Result.error("删除失败");
|
|
}
|
|
return Result.success();
|
|
}
|
|
|
|
@Operation(summary = "删除版本消息", description = "删除指定的剧本版本消息,不能删除当前生效版本。")
|
|
@DeleteMapping(value = "/deleteVersion")
|
|
public Result<Void> deleteVersion(@Parameter(description = "消息 ID") @RequestParam String id) {
|
|
boolean deleted = scriptMessageService.deleteVersion(id);
|
|
if (!deleted) {
|
|
return Result.error("删除失败");
|
|
}
|
|
return Result.success();
|
|
}
|
|
}
|