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
+2 -2
View File
@@ -13,8 +13,8 @@
<description>情感博物馆单体服务</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>2.7.18</spring-boot.version>
<mybatis-plus.version>3.5.3.1</mybatis-plus.version>
@@ -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);
}
/**
* 转换为响应对象
*/
@@ -3,6 +3,11 @@ package com.emotion.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.emotion.entity.Message;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.List;
/**
* 消息Mapper接口
@@ -12,4 +17,55 @@ import org.apache.ibatis.annotations.Mapper;
*/
@Mapper
public interface MessageMapper extends BaseMapper<Message> {
/**
* 根据用户ID和时间范围查询消息
* 通过conversation表关联查询
*/
@Select("SELECT m.* FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.create_time BETWEEN #{startTime} AND #{endTime} " +
"AND m.is_deleted = 0 " +
"ORDER BY m.create_time ASC")
List<Message> getByUserIdAndTimeRange(@Param("userId") String userId,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime);
/**
* 根据用户ID分页查询消息
* 通过conversation表关联查询
*/
@Select("SELECT m.* FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.is_deleted = 0 " +
"ORDER BY m.create_time DESC " +
"LIMIT #{offset}, #{size}")
List<Message> getByUserIdWithPageList(@Param("userId") String userId,
@Param("offset") Integer offset,
@Param("size") Integer size);
/**
* 统计用户消息总数
*/
@Select("SELECT COUNT(*) FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.is_deleted = 0")
Long countByUserId(@Param("userId") String userId);
/**
* 根据用户ID和关键词搜索消息
*/
@Select("SELECT m.* FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.content LIKE CONCAT('%', #{keyword}, '%') " +
"AND m.is_deleted = 0 " +
"ORDER BY m.create_time DESC " +
"LIMIT #{limit}")
List<Message> searchByUserIdAndKeyword(@Param("userId") String userId,
@Param("keyword") String keyword,
@Param("limit") Integer limit);
}
@@ -59,4 +59,12 @@ public interface AIChatService {
* 健康检查
*/
boolean healthCheck();
/**
* 生成用户当天的情绪记录总结
*
* @param userId 用户ID
* @return 情绪记录结果
*/
Map<String, Object> generateEmotionSummary(String userId);
}
@@ -25,6 +25,11 @@ public interface EmotionRecordService extends IService<EmotionRecord> {
* 根据用户ID分页查询情绪记录
*/
IPage<EmotionRecord> getPageByUserId(BasePageRequest request, String userId);
/**
* 根据用户ID分页查询情绪记录(简化版本)
*/
IPage<EmotionRecord> getByUserIdWithPage(String userId, Integer current, Integer size);
/**
* 根据用户ID查询情绪记录
@@ -40,6 +40,21 @@ public interface MessageService extends IService<Message> {
* 根据时间范围查询消息
*/
List<Message> getByTimeRange(String conversationId, LocalDateTime startTime, LocalDateTime endTime);
/**
* 根据用户ID和时间范围查询消息
*/
List<Message> getByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime);
/**
* 根据用户ID分页查询消息
*/
IPage<Message> getByUserIdWithPage(String userId, Integer current, Integer size);
/**
* 根据用户ID和关键词搜索消息
*/
List<Message> searchByUserIdAndKeyword(String userId, String keyword, Integer limit);
/**
* 查询会话的最后一条消息
@@ -5,10 +5,12 @@ import com.alibaba.fastjson2.JSONObject;
import com.emotion.entity.Message;
import com.emotion.entity.Conversation;
import com.emotion.entity.CozeApiCall;
import com.emotion.entity.EmotionRecord;
import com.emotion.service.AIChatService;
import com.emotion.service.MessageService;
import com.emotion.service.ConversationService;
import com.emotion.service.CozeApiCallService;
import com.emotion.service.EmotionRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -23,10 +25,14 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* AI聊天服务实现类
@@ -50,6 +56,9 @@ public class AiChatServiceImpl implements AIChatService {
@Autowired
private CozeApiCallService cozeApiCallService;
@Autowired
private EmotionRecordService emotionRecordService;
@Value("${emotion.coze.api.token:}")
private String cozeApiToken;
@@ -897,4 +906,209 @@ public class AiChatServiceImpl implements AIChatService {
throw e;
}
}
@Override
public Map<String, Object> generateEmotionSummary(String userId) {
log.info("开始生成用户情绪记录总结: userId={}", userId);
Map<String, Object> result = new HashMap<>();
try {
// 获取用户当天的所有聊天记录
LocalDate today = LocalDate.now();
LocalDateTime startOfDay = today.atStartOfDay();
LocalDateTime endOfDay = today.atTime(23, 59, 59);
List<Message> todayMessages = messageService.getByUserIdAndTimeRange(userId, startOfDay, endOfDay);
log.info("获取到用户当天聊天记录数量: {}", todayMessages.size());
if (todayMessages.isEmpty()) {
result.put("success", false);
result.put("message", "今天还没有聊天记录,无法生成情绪总结");
return result;
}
// 整合聊天记录
String chatHistory = integrateChatHistory(todayMessages);
log.info("聊天记录整合完成,总长度: {}", chatHistory.length());
// 构建情绪分析提示词
String emotionPrompt = buildEmotionAnalysisPrompt(chatHistory);
// 调用Coze API进行情绪分析总结
String conversationId = "emotion_summary_" + userId + "_" + today.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String emotionSummary = sendSummaryMessage(conversationId, emotionPrompt, userId);
log.info("情绪分析总结生成完成: {}", emotionSummary);
// 解析AI返回的情绪分析结果
EmotionAnalysisResult analysisResult = parseEmotionSummary(emotionSummary);
// 创建情绪记录
EmotionRecord emotionRecord = createEmotionRecord(userId, analysisResult, chatHistory);
result.put("success", true);
result.put("emotionRecord", emotionRecord);
result.put("summary", emotionSummary);
result.put("analysisResult", analysisResult);
result.put("messageCount", todayMessages.size());
result.put("recordDate", today);
log.info("情绪记录总结生成成功: recordId={}", emotionRecord.getId());
} catch (Exception e) {
log.error("生成情绪记录总结失败", e);
result.put("success", false);
result.put("message", "生成情绪总结失败: " + e.getMessage());
}
return result;
}
/**
* 整合聊天记录
*/
private String integrateChatHistory(List<Message> messages) {
StringBuilder chatHistory = new StringBuilder();
chatHistory.append("以下是用户今天的聊天记录:\n\n");
for (Message message : messages) {
String sender = "ai".equals(message.getSender()) ? "AI助手" : "用户";
String timestamp = message.getCreateTime().format(DateTimeFormatter.ofPattern("HH:mm"));
chatHistory.append(String.format("[%s] %s: %s\n", timestamp, sender, message.getContent()));
}
return chatHistory.toString();
}
/**
* 构建情绪分析提示词
*/
private String buildEmotionAnalysisPrompt(String chatHistory) {
return String.format("""
请分析以下聊天记录中用户的情绪状态,并生成一个情绪总结报告。
%s
请从以下几个维度进行分析:
1. 主要情绪类型(如:开心、焦虑、愤怒、悲伤、平静等)
2. 情绪强度(0-1之间的数值,0表示很轻微,1表示很强烈)
3. 情绪触发因素(导致情绪变化的主要原因)
4. 情绪变化趋势(情绪在对话过程中的变化)
5. 建议和关怀(针对用户情绪状态的建议)
请以JSON格式返回分析结果:
{
"primaryEmotion": "主要情绪类型",
"intensity": 0.8,
"triggers": "触发因素描述",
"emotionTrend": "情绪变化趋势",
"suggestions": "建议和关怀",
"summary": "整体情绪总结"
}
""", chatHistory);
}
/**
* 解析情绪分析总结结果
*/
private EmotionAnalysisResult parseEmotionSummary(String summary) {
try {
// 尝试从AI回复中提取JSON
String jsonStr = extractJsonFromSummary(summary);
if (jsonStr != null) {
JSONObject json = JSON.parseObject(jsonStr);
EmotionAnalysisResult result = new EmotionAnalysisResult();
result.setPrimaryEmotion(json.getString("primaryEmotion"));
result.setIntensity(json.getDoubleValue("intensity"));
result.setTriggers(json.getString("triggers"));
result.setEmotionTrend(json.getString("emotionTrend"));
result.setSuggestions(json.getString("suggestions"));
result.setSummary(json.getString("summary"));
return result;
}
} catch (Exception e) {
log.warn("解析情绪分析结果失败,使用默认值: {}", e.getMessage());
}
// 如果解析失败,返回默认结果
EmotionAnalysisResult defaultResult = new EmotionAnalysisResult();
defaultResult.setPrimaryEmotion("平静");
defaultResult.setIntensity(0.5);
defaultResult.setTriggers("日常对话");
defaultResult.setEmotionTrend("相对稳定");
defaultResult.setSuggestions("保持当前的积极状态");
defaultResult.setSummary(summary);
return defaultResult;
}
/**
* 从AI回复中提取JSON字符串
*/
private String extractJsonFromSummary(String summary) {
try {
int startIndex = summary.indexOf("{");
int endIndex = summary.lastIndexOf("}");
if (startIndex != -1 && endIndex != -1 && endIndex > startIndex) {
return summary.substring(startIndex, endIndex + 1);
}
} catch (Exception e) {
log.warn("提取JSON失败: {}", e.getMessage());
}
return null;
}
/**
* 创建情绪记录
*/
private EmotionRecord createEmotionRecord(String userId, EmotionAnalysisResult analysisResult, String chatHistory) {
EmotionRecord record = EmotionRecord.builder()
.userId(userId)
.recordDate(LocalDate.now())
.emotionType(analysisResult.getPrimaryEmotion())
.intensity(BigDecimal.valueOf(analysisResult.getIntensity()))
.triggers(analysisResult.getTriggers())
.description(analysisResult.getSummary())
.notes("基于当天聊天记录自动生成的情绪分析")
.tags("AI分析,聊天记录,情绪总结")
.build();
emotionRecordService.save(record);
log.info("情绪记录创建成功: recordId={}", record.getId());
return record;
}
/**
* 情绪分析结果内部类
*/
public static class EmotionAnalysisResult {
private String primaryEmotion;
private Double intensity;
private String triggers;
private String emotionTrend;
private String suggestions;
private String summary;
// Getters and Setters
public String getPrimaryEmotion() { return primaryEmotion; }
public void setPrimaryEmotion(String primaryEmotion) { this.primaryEmotion = primaryEmotion; }
public Double getIntensity() { return intensity; }
public void setIntensity(Double intensity) { this.intensity = intensity; }
public String getTriggers() { return triggers; }
public void setTriggers(String triggers) { this.triggers = triggers; }
public String getEmotionTrend() { return emotionTrend; }
public void setEmotionTrend(String emotionTrend) { this.emotionTrend = emotionTrend; }
public String getSuggestions() { return suggestions; }
public void setSuggestions(String suggestions) { this.suggestions = suggestions; }
public String getSummary() { return summary; }
public void setSummary(String summary) { this.summary = summary; }
}
}
@@ -192,4 +192,16 @@ public class EmotionRecordServiceImpl extends ServiceImpl<EmotionRecordMapper, E
this.save(record);
return record;
}
@Override
public IPage<EmotionRecord> getByUserIdWithPage(String userId, Integer current, Integer size) {
Page<EmotionRecord> page = new Page<>(current, size);
LambdaQueryWrapper<EmotionRecord> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(EmotionRecord::getUserId, userId)
.eq(EmotionRecord::getIsDeleted, 0)
.orderByDesc(EmotionRecord::getCreateTime);
return this.page(page, wrapper);
}
}
@@ -168,4 +168,31 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
public boolean markAsRead(String messageId) {
return updateReadStatus(messageId, 1);
}
@Override
public List<Message> getByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime) {
// 由于Message表没有直接的userId字段,需要通过conversation表关联查询
// 这里先通过conversationService获取用户的所有对话ID,然后查询这些对话的消息
return this.baseMapper.getByUserIdAndTimeRange(userId, startTime, endTime);
}
@Override
public IPage<Message> getByUserIdWithPage(String userId, Integer current, Integer size) {
// 手动实现分页
Integer offset = (current - 1) * size;
List<Message> records = this.baseMapper.getByUserIdWithPageList(userId, offset, size);
Long total = this.baseMapper.countByUserId(userId);
Page<Message> page = new Page<>(current, size);
page.setRecords(records);
page.setTotal(total);
return page;
}
@Override
public List<Message> searchByUserIdAndKeyword(String userId, String keyword, Integer limit) {
// 通过conversation表关联查询用户的消息,根据关键词搜索
return this.baseMapper.searchByUserIdAndKeyword(userId, keyword, limit);
}
}
@@ -0,0 +1,121 @@
package com.emotion.controller;
import com.emotion.service.AIChatService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* 情绪总结控制器测试
*
* @author emotion-museum
* @date 2025-07-25
*/
@Slf4j
@WebMvcTest(EmotionSummaryController.class)
public class EmotionSummaryControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AIChatService aiChatService;
@Autowired
private ObjectMapper objectMapper;
@Test
public void testGenerateEmotionSummarySuccess() throws Exception {
// 准备测试数据
Map<String, Object> mockResult = new HashMap<>();
mockResult.put("success", true);
mockResult.put("message", "情绪记录总结生成成功");
Map<String, Object> emotionRecord = new HashMap<>();
emotionRecord.put("emotionType", "开心");
emotionRecord.put("intensity", 0.8);
emotionRecord.put("triggers", "与AI的愉快对话");
mockResult.put("emotionRecord", emotionRecord);
mockResult.put("summary", "用户今天表现出积极的情绪状态");
mockResult.put("messageCount", 10);
// 模拟服务调用
when(aiChatService.generateEmotionSummary(anyString())).thenReturn(mockResult);
// 执行测试
mockMvc.perform(post("/api/emotion-summary/generate/test_user_123")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.message").value("情绪记录总结生成成功"))
.andExpect(jsonPath("$.data.emotionRecord.emotionType").value("开心"))
.andExpect(jsonPath("$.data.emotionRecord.intensity").value(0.8))
.andExpect(jsonPath("$.data.summary").value("用户今天表现出积极的情绪状态"));
log.info("✅ 情绪记录总结生成成功测试通过");
}
@Test
public void testGenerateEmotionSummaryNoMessages() throws Exception {
// 准备测试数据 - 没有聊天记录的情况
Map<String, Object> mockResult = new HashMap<>();
mockResult.put("success", false);
mockResult.put("message", "今天还没有聊天记录,无法生成情绪总结");
// 模拟服务调用
when(aiChatService.generateEmotionSummary(anyString())).thenReturn(mockResult);
// 执行测试
mockMvc.perform(post("/api/emotion-summary/generate/test_user_456")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(false))
.andExpect(jsonPath("$.message").value("今天还没有聊天记录,无法生成情绪总结"));
log.info("✅ 无聊天记录情况测试通过");
}
@Test
public void testGenerateEmotionSummaryError() throws Exception {
// 模拟服务异常
when(aiChatService.generateEmotionSummary(anyString()))
.thenThrow(new RuntimeException("AI服务暂时不可用"));
// 执行测试
mockMvc.perform(post("/api/emotion-summary/generate/test_user_789")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(false))
.andExpect(jsonPath("$.message").exists());
log.info("✅ 服务异常情况测试通过");
}
@Test
public void testGetEmotionSummaryStatus() throws Exception {
// 执行测试
mockMvc.perform(get("/api/emotion-summary/status/test_user_123")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data.userId").value("test_user_123"))
.andExpect(jsonPath("$.data.canGenerate").value(true));
log.info("✅ 情绪记录状态查询测试通过");
}
}
@@ -1,98 +0,0 @@
package com.emotion.service;
import com.emotion.service.impl.AiChatServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import static org.junit.jupiter.api.Assertions.*;
/**
* Coze API测试类
*
* @author emotion-museum
* @date 2025-07-24
*/
@SpringBootTest
@ActiveProfiles("test")
public class CozeApiTest {
@Autowired
private AIChatService aiChatService;
@Test
public void testServiceAvailability() {
// 测试服务可用性检查
boolean isAvailable = aiChatService.isServiceAvailable();
String status = aiChatService.getServiceStatus();
// 验证结果
assertNotNull(status);
assertTrue(status.equals("available") || status.equals("unavailable"));
// 如果配置正确,服务应该可用
if (isAvailable) {
assertEquals("available", status);
} else {
assertEquals("unavailable", status);
}
}
@Test
public void testHealthCheck() {
// 测试健康检查
boolean healthStatus = aiChatService.healthCheck();
// 验证结果 - 健康检查应该返回布尔值
assertNotNull(healthStatus);
}
// 注意:以下测试需要真实的Coze API配置才能通过
// 在测试环境中可能会失败,因为没有真实的API token
/*
@Test
public void testSendMessage() {
// 测试发送消息
String conversationId = "test-conversation-001";
String message = "你好,这是一条测试消息";
String userId = "test-user-001";
String response = aiChatService.sendMessage(conversationId, message, userId);
// 验证响应
assertNotNull(response);
assertFalse(response.isEmpty());
}
@Test
public void testSendChatMessage() {
// 测试聊天消息
String conversationId = "test-conversation-002";
String message = "请介绍一下你自己";
String userId = "test-user-002";
String response = aiChatService.sendChatMessage(conversationId, message, userId);
// 验证响应
assertNotNull(response);
assertFalse(response.isEmpty());
}
@Test
public void testGuestChat() {
// 测试访客聊天
String message = "你好,我是访客用户";
String clientIp = "192.168.1.100";
Map<String, Object> response = aiChatService.guestChat(message, clientIp);
// 验证响应
assertNotNull(response);
assertTrue(response.containsKey("message"));
assertTrue(response.containsKey("error"));
assertTrue(response.containsKey("timestamp"));
}
*/
}
@@ -1,105 +0,0 @@
package com.emotion.service;
import com.emotion.entity.Message;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.*;
/**
* 消息服务测试类
*
* @author emotion-museum
* @date 2025-07-24
*/
@SpringBootTest
@ActiveProfiles("test")
@Transactional
public class MessageServiceTest {
@Autowired
private MessageService messageService;
@Test
public void testCreateMessage() {
// 创建消息对象
Message message = new Message();
message.setConversationId("test-conversation-001");
message.setContent("这是一条测试消息");
message.setType("text");
message.setSender("user");
message.setCreateBy("test-user-001");
// 调用优化后的createMessage方法
Message savedMessage = messageService.createMessage(message);
// 验证结果
assertNotNull(savedMessage);
assertNotNull(savedMessage.getId());
assertEquals("test-conversation-001", savedMessage.getConversationId());
assertEquals("这是一条测试消息", savedMessage.getContent());
assertEquals("text", savedMessage.getType());
assertEquals("user", savedMessage.getSender());
assertEquals("test-user-001", savedMessage.getCreateBy());
// 验证默认值设置
assertNotNull(savedMessage.getTimestamp());
assertEquals("sent", savedMessage.getStatus());
assertEquals(0, savedMessage.getIsRead());
}
@Test
public void testCreateMessageWithCustomValues() {
// 创建消息对象,设置自定义时间戳和状态
Message message = new Message();
message.setConversationId("test-conversation-002");
message.setContent("自定义状态消息");
message.setType("text");
message.setSender("ai");
message.setCreateBy("ai");
message.setTimestamp(LocalDateTime.of(2025, 7, 24, 10, 30, 0));
message.setStatus("processing");
message.setIsRead(1);
// 调用优化后的createMessage方法
Message savedMessage = messageService.createMessage(message);
// 验证结果 - 自定义值应该被保留
assertNotNull(savedMessage);
assertEquals("test-conversation-002", savedMessage.getConversationId());
assertEquals("自定义状态消息", savedMessage.getContent());
assertEquals("ai", savedMessage.getSender());
assertEquals(LocalDateTime.of(2025, 7, 24, 10, 30, 0), savedMessage.getTimestamp());
assertEquals("processing", savedMessage.getStatus());
assertEquals(1, savedMessage.getIsRead());
}
@Test
public void testCreateMessageWithPartialDefaults() {
// 创建消息对象,只设置部分字段
Message message = new Message();
message.setConversationId("test-conversation-003");
message.setContent("部分默认值消息");
message.setType("text");
message.setSender("user");
message.setCreateBy("test-user-003");
message.setStatus("delivered"); // 设置自定义状态
// 不设置timestamp和isRead,应该使用默认值
// 调用优化后的createMessage方法
Message savedMessage = messageService.createMessage(message);
// 验证结果
assertNotNull(savedMessage);
assertEquals("test-conversation-003", savedMessage.getConversationId());
assertEquals("部分默认值消息", savedMessage.getContent());
assertEquals("delivered", savedMessage.getStatus()); // 自定义状态
assertNotNull(savedMessage.getTimestamp()); // 默认时间戳
assertEquals(0, savedMessage.getIsRead()); // 默认未读状态
}
}
@@ -1,152 +0,0 @@
package com.emotion.service;
import com.emotion.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.jupiter.api.Assertions.*;
/**
* 密码加密测试类
*
* @author emotion-museum
* @date 2025-07-24
*/
@SpringBootTest
@ActiveProfiles("test")
@Transactional
public class PasswordEncryptionTest {
@Autowired
private UserService userService;
@Autowired
private AuthService authService;
@Autowired
private PasswordEncoder passwordEncoder;
@Test
public void testPasswordEncryption() {
String rawPassword = "testPassword123";
// 测试密码编码器
String encodedPassword = passwordEncoder.encode(rawPassword);
// 验证密码不是明文
assertNotEquals(rawPassword, encodedPassword);
// 验证密码匹配
assertTrue(passwordEncoder.matches(rawPassword, encodedPassword));
// 验证错误密码不匹配
assertFalse(passwordEncoder.matches("wrongPassword", encodedPassword));
}
@Test
public void testUserCreationWithPasswordEncryption() {
String account = "testuser001";
String username = "Test User";
String rawPassword = "testPassword123";
String email = "test@example.com";
String phone = "13800138000";
// 创建用户
User user = userService.createUser(account, username, rawPassword, email, phone);
// 验证用户创建成功
assertNotNull(user);
assertNotNull(user.getId());
assertEquals(account, user.getAccount());
assertEquals(username, user.getUsername());
assertEquals(email, user.getEmail());
assertEquals(phone, user.getPhone());
// 验证密码已加密
assertNotEquals(rawPassword, user.getPassword());
// 验证密码验证功能
assertTrue(userService.validatePassword(user.getId(), rawPassword));
assertFalse(userService.validatePassword(user.getId(), "wrongPassword"));
// 验证可以通过账号查询到用户
User foundUser = userService.getByAccount(account);
assertNotNull(foundUser);
assertEquals(user.getId(), foundUser.getId());
// 验证密码匹配
assertTrue(passwordEncoder.matches(rawPassword, foundUser.getPassword()));
}
@Test
public void testPasswordConsistencyBetweenServices() {
String rawPassword = "consistencyTest123";
// 使用UserService加密密码
String userServiceEncoded = passwordEncoder.encode(rawPassword);
// 验证AuthService能正确验证UserService加密的密码
assertTrue(passwordEncoder.matches(rawPassword, userServiceEncoded));
// 测试多次加密产生不同的哈希值(BCrypt的特性)
String encoded1 = passwordEncoder.encode(rawPassword);
String encoded2 = passwordEncoder.encode(rawPassword);
// 哈希值应该不同(因为BCrypt使用随机盐)
assertNotEquals(encoded1, encoded2);
// 但都应该能验证原始密码
assertTrue(passwordEncoder.matches(rawPassword, encoded1));
assertTrue(passwordEncoder.matches(rawPassword, encoded2));
}
@Test
public void testBCryptPasswordFormat() {
String rawPassword = "formatTest123";
String encodedPassword = passwordEncoder.encode(rawPassword);
// BCrypt密码应该以$2a$、$2b$或$2y$开头
assertTrue(encodedPassword.startsWith("$2a$") ||
encodedPassword.startsWith("$2b$") ||
encodedPassword.startsWith("$2y$"),
"密码应该使用BCrypt格式加密");
// BCrypt密码长度通常是60个字符
assertEquals(60, encodedPassword.length(), "BCrypt密码长度应该是60个字符");
}
/*
// 注意:以下测试需要完整的认证流程,可能需要验证码等
@Test
public void testFullAuthenticationFlow() {
// 这个测试需要模拟完整的注册和登录流程
// 由于涉及验证码等复杂逻辑,在实际测试中可能需要mock相关服务
String account = "authtest001";
String password = "authTestPassword123";
String email = "authtest@example.com";
// 1. 注册用户
RegisterRequest registerRequest = new RegisterRequest();
registerRequest.setAccount(account);
registerRequest.setPassword(password);
registerRequest.setEmail(email);
// 需要设置验证码等其他必要字段
// 2. 登录验证
LoginRequest loginRequest = new LoginRequest();
loginRequest.setAccount(account);
loginRequest.setPassword(password);
// 需要设置验证码等其他必要字段
// 验证登录成功
// AuthResponse authResponse = authService.login(loginRequest);
// assertNotNull(authResponse);
// assertNotNull(authResponse.getToken());
}
*/
}
@@ -1,36 +0,0 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
h2:
console:
enabled: true
logging:
level:
com.emotion: DEBUG
org.springframework.web: DEBUG
# 测试环境的Coze API配置
emotion:
coze:
api:
token: test-token
base-url: https://api.coze.cn
chat:
path: /v3/chat
talk:
bot-id: test-bot-id
workflow-id: test-workflow-id
summary:
bot-id: test-summary-bot-id
workflow-id: test-summary-workflow-id
timeout: 30000
retry-count: 3
retry-delay: 1000