feat: 实现情绪记录功能和聊天历史查看

- 完成情绪记录生成功能,支持AI分析聊天内容生成情绪记录
- 实现聊天页面历史记录查看,支持分页和搜索
- 修改日记页面展示情绪记录而非普通日记
- 添加情绪记录的增删改查API
- 优化前端UI,添加情绪强度显示和详细信息展示
- 修复SCSS变量缺失问题
This commit is contained in:
2025-07-25 01:11:01 +08:00
parent 3292a74698
commit 86c2df4784
25 changed files with 1397 additions and 2210 deletions
@@ -1,8 +1,15 @@
package com.emotion.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.emotion.common.Result;
import com.emotion.entity.EmotionRecord;
import com.emotion.service.EmotionRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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;
@@ -11,16 +18,20 @@ import java.util.*;
/**
* 情绪记录控制器
*
*
* @author emotion-museum
* @date 2025-07-22
*/
@RestController
@RequestMapping("/emotion/record")
@RequestMapping("/api/emotion-records")
@Tag(name = "情绪记录管理", description = "用户情绪记录的增删改查功能")
public class EmotionRecordController {
private static final Logger log = LoggerFactory.getLogger(EmotionRecordController.class);
@Autowired
private EmotionRecordService emotionRecordService;
/**
* 创建情绪记录
*/
@@ -53,38 +64,26 @@ public class EmotionRecordController {
/**
* 获取用户情绪记录列表
*/
@GetMapping("/list/{userId}")
public Result<List<Map<String, Object>>> getRecordList(@PathVariable String userId,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size) {
log.info("获取情绪记录列表: userId={}, page={}, size={}", userId, page, size);
@Operation(summary = "获取用户情绪记录列表", description = "分页获取指定用户的情绪记录,按创建时间倒序")
@GetMapping("/user/{userId}")
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 {
List<Map<String, Object>> records = new ArrayList<>();
// 模拟数据
for (int i = 0; i < size; i++) {
Map<String, Object> record = new HashMap<>();
record.put("id", "record-" + (System.currentTimeMillis() + i));
record.put("userId", userId);
record.put("recordDate", LocalDate.now().minusDays(i));
record.put("emotionType", getRandomEmotion());
record.put("intensity", 0.5 + Math.random() * 0.5);
record.put("triggers", "工作压力");
record.put("description", "今天感觉" + getRandomEmotion());
record.put("tags", Arrays.asList("工作", "压力"));
record.put("weather", "晴天");
record.put("location", "办公室");
record.put("activity", "工作");
record.put("createTime", LocalDateTime.now().minusDays(i));
records.add(record);
}
return Result.success(records);
IPage<EmotionRecord> page = emotionRecordService.getByUserIdWithPage(userId, current, size);
log.info("获取用户情绪记录成功: userId={}, total={}, records={}",
userId, page.getTotal(), page.getRecords().size());
return Result.success(page);
} catch (Exception e) {
log.error("获取情绪记录列表失败: {}", e.getMessage());
return Result.error("获取列表失败");
log.error("获取用户情绪记录失败: userId={}", userId, e);
return Result.error("获取情绪记录失败: " + e.getMessage());
}
}
@@ -0,0 +1,78 @@
package com.emotion.controller;
import com.emotion.common.Result;
import com.emotion.service.AIChatService;
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.web.bind.annotation.*;
import java.util.Map;
/**
* 情绪总结控制器
*
* @author emotion-museum
* @date 2025-07-25
*/
@Slf4j
@RestController
@RequestMapping("/api/emotion-summary")
@Tag(name = "情绪总结管理", description = "用户情绪记录总结和分析功能")
public class EmotionSummaryController {
@Autowired
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);
try {
// 调用AI服务生成情绪总结
Map<String, Object> result = aiChatService.generateEmotionSummary(userId);
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 (Exception e) {
log.error("生成情绪记录总结时发生异常: userId={}", userId, 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);
try {
// 这里可以添加检查用户今天是否已经生成过情绪记录的逻辑
// 暂时返回基本状态信息
Map<String, Object> status = Map.of(
"userId", userId,
"canGenerate", true,
"message", "可以生成情绪记录总结"
);
return Result.success(status);
} catch (Exception e) {
log.error("查询情绪记录总结状态时发生异常: userId={}", userId, e);
return Result.error("查询状态失败: " + e.getMessage());
}
}
}
@@ -123,6 +123,41 @@ public class MessageController {
return Result.success(count);
}
/**
* 根据用户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());
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);
}
/**
* 根据用户ID和关键词搜索消息
*/
@GetMapping("/user/{userId}/search")
public Result<List<MessageResponse>> searchByUserId(@PathVariable String userId,
@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);
}
/**
* 转换为响应对象
*/