优化
This commit is contained in:
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.entity.EmotionRecord;
|
||||
import com.emotion.service.EmotionRecordService;
|
||||
import com.emotion.util.CurrentUserUtil;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -11,7 +12,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
@@ -23,7 +23,7 @@ import java.util.*;
|
||||
* @date 2025-07-22
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/emotion-records")
|
||||
@RequestMapping("/emotion-records")
|
||||
@Tag(name = "情绪记录管理", description = "用户情绪记录的增删改查功能")
|
||||
public class EmotionRecordController {
|
||||
|
||||
@@ -64,16 +64,18 @@ public class EmotionRecordController {
|
||||
/**
|
||||
* 获取用户情绪记录列表
|
||||
*/
|
||||
@Operation(summary = "获取用户情绪记录列表", description = "分页获取指定用户的情绪记录,按创建时间倒序")
|
||||
@GetMapping("/user/{userId}")
|
||||
@Operation(summary = "获取用户情绪记录列表", description = "分页获取当前用户的情绪记录,按创建时间倒序")
|
||||
@GetMapping("/user")
|
||||
public Result<IPage<EmotionRecord>> getRecordList(
|
||||
@Parameter(description = "用户ID") @PathVariable String userId,
|
||||
@Parameter(description = "页码,从1开始") @RequestParam(defaultValue = "1") Integer current,
|
||||
@Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size) {
|
||||
|
||||
log.info("获取用户情绪记录列表: userId={}, current={}, size={}", userId, current, size);
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
log.info("获取用户情绪记录列表: userId={}, current={}, size={}", userId, current, size);
|
||||
|
||||
IPage<EmotionRecord> page = emotionRecordService.getByUserIdWithPage(userId, current, size);
|
||||
|
||||
log.info("获取用户情绪记录成功: userId={}, total={}, records={}",
|
||||
@@ -81,12 +83,44 @@ public class EmotionRecordController {
|
||||
|
||||
return Result.success(page);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
log.warn("用户认证失败: {}", e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("获取用户情绪记录失败: userId={}", userId, e);
|
||||
log.error("获取用户情绪记录失败", e);
|
||||
return Result.error("获取情绪记录失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户最近情绪记录
|
||||
*/
|
||||
@Operation(summary = "获取用户最近情绪记录", description = "获取当前用户最近的情绪记录列表")
|
||||
@GetMapping("/user/recent")
|
||||
public Result<List<EmotionRecord>> getRecentRecords(
|
||||
@Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
log.info("获取用户最近情绪记录: userId={}, limit={}", userId, limit);
|
||||
|
||||
List<EmotionRecord> records = emotionRecordService.getRecentByUserId(userId, limit);
|
||||
|
||||
log.info("获取用户最近情绪记录成功: userId={}, records={}", userId, records.size());
|
||||
|
||||
return Result.success(records);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
log.warn("用户认证失败: {}", e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("获取用户最近情绪记录失败", e);
|
||||
return Result.error("获取最近记录失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取情绪记录详情
|
||||
*/
|
||||
|
||||
@@ -2,8 +2,8 @@ package com.emotion.controller;
|
||||
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.service.AIChatService;
|
||||
import com.emotion.util.CurrentUserUtil;
|
||||
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;
|
||||
@@ -13,13 +13,13 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 情绪总结控制器
|
||||
*
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-25
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/emotion-summary")
|
||||
@RequestMapping("/emotion-summary")
|
||||
@Tag(name = "情绪总结管理", description = "用户情绪记录总结和分析功能")
|
||||
public class EmotionSummaryController {
|
||||
|
||||
@@ -27,39 +27,46 @@ public class EmotionSummaryController {
|
||||
private AIChatService aiChatService;
|
||||
|
||||
@Operation(summary = "生成用户当天的情绪记录总结", description = "基于用户当天的聊天记录生成情绪分析和记录")
|
||||
@PostMapping("/generate/{userId}")
|
||||
public Result<Map<String, Object>> generateEmotionSummary(
|
||||
@Parameter(description = "用户ID") @PathVariable String userId) {
|
||||
|
||||
log.info("收到生成情绪记录总结请求: userId={}", userId);
|
||||
|
||||
@PostMapping("/generate")
|
||||
public Result<Map<String, Object>> generateEmotionSummary() {
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
log.info("收到生成情绪记录总结请求: userId={}", userId);
|
||||
|
||||
// 调用AI服务生成情绪总结
|
||||
Map<String, Object> result = aiChatService.generateEmotionSummary(userId);
|
||||
|
||||
|
||||
if ((Boolean) result.get("success")) {
|
||||
log.info("情绪记录总结生成成功: userId={}", userId);
|
||||
return Result.success(result, "情绪记录总结生成成功");
|
||||
return Result.success("情绪记录总结生成成功", result);
|
||||
} else {
|
||||
String message = (String) result.get("message");
|
||||
log.warn("情绪记录总结生成失败: userId={}, message={}", userId, message);
|
||||
return Result.error(message);
|
||||
}
|
||||
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
log.warn("用户认证失败: {}", e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("生成情绪记录总结时发生异常: userId={}", userId, e);
|
||||
log.error("生成情绪记录总结时发生异常", e);
|
||||
return Result.error("生成情绪记录总结失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取用户情绪记录总结状态", description = "检查用户今天是否已经生成过情绪记录")
|
||||
@GetMapping("/status/{userId}")
|
||||
public Result<Map<String, Object>> getEmotionSummaryStatus(
|
||||
@Parameter(description = "用户ID") @PathVariable String userId) {
|
||||
|
||||
log.info("查询用户情绪记录总结状态: userId={}", userId);
|
||||
|
||||
@GetMapping("/status")
|
||||
public Result<Map<String, Object>> getEmotionSummaryStatus() {
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
log.info("查询用户情绪记录总结状态: userId={}", userId);
|
||||
|
||||
// 这里可以添加检查用户今天是否已经生成过情绪记录的逻辑
|
||||
// 暂时返回基本状态信息
|
||||
Map<String, Object> status = Map.of(
|
||||
@@ -67,11 +74,14 @@ public class EmotionSummaryController {
|
||||
"canGenerate", true,
|
||||
"message", "可以生成情绪记录总结"
|
||||
);
|
||||
|
||||
|
||||
return Result.success(status);
|
||||
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
log.warn("用户认证失败: {}", e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("查询情绪记录总结状态时发生异常: userId={}", userId, e);
|
||||
log.error("查询情绪记录总结状态时发生异常", e);
|
||||
return Result.error("查询状态失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.emotion.dto.request.MessageCreateRequest;
|
||||
import com.emotion.dto.response.MessageResponse;
|
||||
import com.emotion.entity.Message;
|
||||
import com.emotion.service.MessageService;
|
||||
import com.emotion.util.CurrentUserUtil;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -126,36 +127,76 @@ public class MessageController {
|
||||
/**
|
||||
* 根据用户ID分页查询消息
|
||||
*/
|
||||
@GetMapping("/user/{userId}/page")
|
||||
public Result<PageResult<MessageResponse>> getPageByUserId(@PathVariable String userId,
|
||||
@Valid PageRequest request) {
|
||||
IPage<Message> page = messageService.getByUserIdWithPage(userId, Math.toIntExact(request.getCurrent()), Math.toIntExact(request.getSize()));
|
||||
List<MessageResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
@GetMapping("/user/page")
|
||||
public Result<PageResult<MessageResponse>> getPageByUserId(@Valid PageRequest request) {
|
||||
|
||||
PageResult<MessageResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(page.getCurrent());
|
||||
pageResult.setSize(page.getSize());
|
||||
pageResult.setTotal(page.getTotal());
|
||||
pageResult.setPages(page.getPages());
|
||||
pageResult.setRecords(responses);
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
return Result.success(pageResult);
|
||||
IPage<Message> page = messageService.getByUserIdWithPage(userId, Math.toIntExact(request.getCurrent()),
|
||||
Math.toIntExact(request.getSize()));
|
||||
List<MessageResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<MessageResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(page.getCurrent());
|
||||
pageResult.setSize(page.getSize());
|
||||
pageResult.setTotal(page.getTotal());
|
||||
pageResult.setPages(page.getPages());
|
||||
pageResult.setRecords(responses);
|
||||
|
||||
return Result.success(pageResult);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID和关键词搜索消息
|
||||
*/
|
||||
@GetMapping("/user/{userId}/search")
|
||||
public Result<List<MessageResponse>> searchByUserId(@PathVariable String userId,
|
||||
@GetMapping("/user/search")
|
||||
public Result<List<MessageResponse>> searchByUserId(
|
||||
@RequestParam String keyword,
|
||||
@RequestParam(defaultValue = "50") Integer limit) {
|
||||
List<Message> messages = messageService.searchByUserIdAndKeyword(userId, keyword, limit);
|
||||
List<MessageResponse> responses = messages.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
List<Message> messages = messageService.searchByUserIdAndKeyword(userId, keyword, limit);
|
||||
List<MessageResponse> responses = messages.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户最近的聊天记录
|
||||
*/
|
||||
@GetMapping("/user/recent")
|
||||
public Result<List<MessageResponse>> getRecentMessages(
|
||||
@RequestParam(defaultValue = "10") Integer limit) {
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
List<Message> messages = messageService.getRecentByUserId(userId, limit);
|
||||
List<MessageResponse> responses = messages.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user