优化
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.EmotionSummaryGenerateRequest;
|
||||
import com.emotion.dto.request.EmotionSummaryStatusRequest;
|
||||
import com.emotion.dto.response.EmotionSummaryGenerateResponse;
|
||||
import com.emotion.dto.response.EmotionSummaryStatusResponse;
|
||||
import com.emotion.service.AiChatService;
|
||||
import com.emotion.util.CurrentUserUtil;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
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 org.springframework.scheduling.annotation.Async;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* 情绪总结控制器
|
||||
@@ -20,64 +24,49 @@ import java.util.Map;
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/emotion-summary")
|
||||
@RequestMapping("/emotionSummary")
|
||||
@Tag(name = "情绪总结管理", description = "用户情绪记录总结和分析功能")
|
||||
public class EmotionSummaryController {
|
||||
|
||||
@Autowired
|
||||
private AiChatService aiChatService;
|
||||
|
||||
/**
|
||||
* 生成用户当天的情绪记录总结
|
||||
*/
|
||||
@Operation(summary = "生成用户当天的情绪记录总结", description = "基于用户当天的聊天记录生成情绪分析和记录")
|
||||
@PostMapping("/generate")
|
||||
public Result<Map<String, Object>> generateEmotionSummary() {
|
||||
try {
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
log.info("收到生成情绪记录总结请求: userId={}", userId);
|
||||
// 调用AI服务异步生成情绪总结(阻塞获取结果)
|
||||
Map<String, Object> result = aiChatService.generateEmotionSummaryAsync(userId).get();
|
||||
if ((Boolean) result.get("success")) {
|
||||
log.info("情绪记录总结生成成功: userId={}", userId);
|
||||
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("生成情绪记录总结时发生异常", e);
|
||||
return Result.error("生成情绪记录总结失败: " + e.getMessage());
|
||||
@PostMapping(value = "/generate")
|
||||
public Result<EmotionSummaryGenerateResponse> generateEmotionSummary(
|
||||
@RequestBody @Valid EmotionSummaryGenerateRequest request) {
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
log.info("收到生成情绪记录总结请求: userId={}", userId);
|
||||
|
||||
// 调用AI服务生成情绪总结
|
||||
EmotionSummaryGenerateResponse response = aiChatService.generateEmotionSummaryWithResponse(userId);
|
||||
|
||||
if (response.getSuccess()) {
|
||||
log.info("情绪记录总结生成成功: userId={}", userId);
|
||||
return Result.success("情绪记录总结生成成功", response);
|
||||
} else {
|
||||
log.warn("情绪记录总结生成失败: userId={}, message={}", userId, response.getMessage());
|
||||
return Result.error(response.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户情绪记录总结状态
|
||||
*/
|
||||
@Operation(summary = "获取用户情绪记录总结状态", description = "检查用户今天是否已经生成过情绪记录")
|
||||
@GetMapping("/status")
|
||||
public Result<Map<String, Object>> getEmotionSummaryStatus() {
|
||||
@GetMapping(value = "/status")
|
||||
public Result<EmotionSummaryStatusResponse> getEmotionSummaryStatus(
|
||||
@Validated EmotionSummaryStatusRequest request) {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
log.info("查询用户情绪记录总结状态: userId={}", userId);
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
// 调用AI服务获取状态信息
|
||||
EmotionSummaryStatusResponse response = aiChatService.getEmotionSummaryStatusWithResponse(userId);
|
||||
|
||||
log.info("查询用户情绪记录总结状态: userId={}", userId);
|
||||
|
||||
// 这里可以添加检查用户今天是否已经生成过情绪记录的逻辑
|
||||
// 暂时返回基本状态信息
|
||||
Map<String, Object> status = Map.of(
|
||||
"userId", userId,
|
||||
"canGenerate", true,
|
||||
"message", "可以生成情绪记录总结"
|
||||
);
|
||||
|
||||
return Result.success(status);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
log.warn("用户认证失败: {}", e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("查询情绪记录总结状态时发生异常", e);
|
||||
return Result.error("查询状态失败: " + e.getMessage());
|
||||
}
|
||||
return Result.success(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user