From cf50a9f1fa88b357258997cd46fa5d38f64ee3c3 Mon Sep 17 00:00:00 2001 From: huazhongmin Date: Mon, 8 Sep 2025 18:01:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/EmotionAnalysisController.java | 122 +------------- .../request/EmotionAnalysisCreateRequest.java | 5 + .../request/EmotionAnalysisPageRequest.java | 6 + .../request/EmotionAnalysisUpdateRequest.java | 6 + .../service/EmotionAnalysisService.java | 88 +---------- .../impl/EmotionAnalysisServiceImpl.java | 149 +----------------- 6 files changed, 31 insertions(+), 345 deletions(-) diff --git a/backend-single/src/main/java/com/emotion/controller/EmotionAnalysisController.java b/backend-single/src/main/java/com/emotion/controller/EmotionAnalysisController.java index bd08d4d..54560b3 100644 --- a/backend-single/src/main/java/com/emotion/controller/EmotionAnalysisController.java +++ b/backend-single/src/main/java/com/emotion/controller/EmotionAnalysisController.java @@ -8,14 +8,10 @@ import com.emotion.dto.request.EmotionAnalysisUpdateRequest; import com.emotion.dto.response.EmotionAnalysisResponse; import com.emotion.service.EmotionAnalysisService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.format.annotation.DateTimeFormat; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; -import java.time.LocalDateTime; import java.util.List; /** @@ -25,7 +21,7 @@ import java.util.List; * @date 2025-07-23 */ @RestController -@RequestMapping("/emotion-analysis") +@RequestMapping("/emotionAnalysis") public class EmotionAnalysisController { @Autowired @@ -34,25 +30,15 @@ public class EmotionAnalysisController { /** * 分页查询情绪分析记录 */ - @GetMapping("/page") + @GetMapping(value = "/page") public Result> getPage(@Validated EmotionAnalysisPageRequest request) { return Result.success(emotionAnalysisService.getPageWithResponse(request)); } - /** - * 根据用户ID分页查询情绪分析记录 - */ - @GetMapping("/user/page") - public Result> getPageByUserId( - @RequestParam String userId, - @Validated EmotionAnalysisPageRequest request) { - return Result.success(emotionAnalysisService.getPageByUserIdWithResponse(userId, request)); - } - /** * 根据ID获取情绪分析记录 */ - @GetMapping + @GetMapping(value = "/detail") public Result getById(@RequestParam String id) { EmotionAnalysisResponse response = emotionAnalysisService.getEmotionAnalysisResponseById(id); if (response == null) { @@ -61,22 +47,10 @@ public class EmotionAnalysisController { return Result.success(response); } - /** - * 根据消息ID获取情绪分析记录 - */ - @GetMapping("/message") - public Result getByMessageId(@RequestParam String messageId) { - EmotionAnalysisResponse response = emotionAnalysisService.getEmotionAnalysisResponseByMessageId(messageId); - if (response == null) { - return Result.notFound("情绪分析记录不存在"); - } - return Result.success(response); - } - /** * 创建情绪分析记录 */ - @PostMapping + @PostMapping(value = "/create") public Result create(@RequestBody @Valid EmotionAnalysisCreateRequest request) { return Result.success(emotionAnalysisService.createEmotionAnalysisWithResponse(request)); } @@ -84,7 +58,7 @@ public class EmotionAnalysisController { /** * 更新情绪分析记录 */ - @PutMapping + @PutMapping(value = "/update") public Result update(@RequestBody @Valid EmotionAnalysisUpdateRequest request) { EmotionAnalysisResponse response = emotionAnalysisService.updateEmotionAnalysisWithResponse(request); if (response == null) { @@ -96,7 +70,7 @@ public class EmotionAnalysisController { /** * 删除情绪分析记录 */ - @DeleteMapping + @DeleteMapping(value = "/delete") public Result delete(@RequestParam String id) { boolean deleted = emotionAnalysisService.deleteEmotionAnalysis(id); if (!deleted) { @@ -104,88 +78,4 @@ public class EmotionAnalysisController { } return Result.success(); } - - /** - * 根据主要情绪查询分析记录 - */ - @GetMapping("/emotion") - public Result> getByPrimaryEmotion(@RequestParam String primaryEmotion) { - List responses = emotionAnalysisService - .getEmotionAnalysisResponsesByPrimaryEmotion(primaryEmotion); - return Result.success(responses); - } - - /** - * 根据情绪极性查询分析记录 - */ - @GetMapping("/polarity") - public Result> getByPolarity(@RequestParam String polarity) { - List responses = emotionAnalysisService - .getEmotionAnalysisResponsesByPolarity(polarity); - return Result.success(responses); - } - - /** - * 根据用户ID和情绪类型查询分析记录 - */ - @GetMapping("/user/emotion") - public Result> getByUserIdAndEmotion( - @RequestParam String userId, - @RequestParam String primaryEmotion) { - List responses = emotionAnalysisService - .getEmotionAnalysisResponsesByUserIdAndEmotion(userId, primaryEmotion); - return Result.success(responses); - } - - /** - * 根据时间范围查询用户情绪分析记录 - */ - @GetMapping("/user/time-range") - public Result> getByUserIdAndTimeRange( - @RequestParam String userId, - @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime, - @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) { - List responses = emotionAnalysisService - .getEmotionAnalysisResponsesByUserIdAndTimeRange(userId, startTime, endTime); - return Result.success(responses); - } - - /** - * 统计用户的情绪分析记录数量 - */ - @GetMapping("/user/count") - public Result countByUserId(@RequestParam String userId) { - Long count = emotionAnalysisService.countByUserId(userId); - return Result.success(count); - } - - /** - * 查询用户最近的情绪分析记录 - */ - @GetMapping("/user/recent") - public Result> getRecentByUserId( - @RequestParam String userId, - @RequestParam(defaultValue = "10") Integer limit) { - List responses = emotionAnalysisService - .getEmotionAnalysisResponsesRecentByUserId(userId, limit); - return Result.success(responses); - } - - /** - * 查询用户的平均情绪强度 - */ - @GetMapping("/user/avg-intensity") - public Result getAvgIntensityByUserId(@RequestParam String userId) { - Double avgIntensity = emotionAnalysisService.getAvgIntensityByUserId(userId); - return Result.success(avgIntensity); - } - - /** - * 查询用户最常见的情绪类型 - */ - @GetMapping("/user/most-frequent-emotion") - public Result getMostFrequentEmotionByUserId(@RequestParam String userId) { - String emotion = emotionAnalysisService.getMostFrequentEmotionByUserId(userId); - return Result.success(emotion); - } } \ No newline at end of file diff --git a/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisCreateRequest.java b/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisCreateRequest.java index c0d4ea6..7c35a48 100644 --- a/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisCreateRequest.java +++ b/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisCreateRequest.java @@ -4,6 +4,7 @@ import lombok.Data; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; /** * 情绪分析创建请求类 @@ -18,23 +19,27 @@ public class EmotionAnalysisCreateRequest { * 消息ID */ @NotBlank(message = "消息ID不能为空") + @Size(max = 64, message = "消息ID长度不能超过64个字符") private String messageId; /** * 用户ID */ @NotBlank(message = "用户ID不能为空") + @Size(max = 32, message = "用户ID长度不能超过32个字符") private String userId; /** * 主要情绪 */ @NotBlank(message = "主要情绪不能为空") + @Size(max = 32, message = "主要情绪长度不能超过32个字符") private String primaryEmotion; /** * 情绪极性 */ + @Size(max = 9, message = "情绪极性长度不能超过9个字符") private String polarity; /** diff --git a/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisPageRequest.java b/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisPageRequest.java index 7e3cfd5..c2e2897 100644 --- a/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisPageRequest.java +++ b/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisPageRequest.java @@ -4,6 +4,8 @@ import com.emotion.common.BasePageRequest; import lombok.Data; import lombok.EqualsAndHashCode; +import javax.validation.constraints.Size; + /** * 情绪分析分页请求类 * @@ -17,20 +19,24 @@ public class EmotionAnalysisPageRequest extends BasePageRequest { /** * 用户ID(可选) */ + @Size(max = 32, message = "用户ID长度不能超过32个字符") private String userId; /** * 消息ID(可选) */ + @Size(max = 64, message = "消息ID长度不能超过64个字符") private String messageId; /** * 主要情绪(可选) */ + @Size(max = 32, message = "主要情绪长度不能超过32个字符") private String primaryEmotion; /** * 情绪极性(可选) */ + @Size(max = 9, message = "情绪极性长度不能超过9个字符") private String polarity; } \ No newline at end of file diff --git a/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisUpdateRequest.java b/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisUpdateRequest.java index cff03b9..c6bc42d 100644 --- a/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisUpdateRequest.java +++ b/backend-single/src/main/java/com/emotion/dto/request/EmotionAnalysisUpdateRequest.java @@ -3,6 +3,7 @@ package com.emotion.dto.request; import lombok.Data; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; /** * 情绪分析更新请求类 @@ -17,26 +18,31 @@ public class EmotionAnalysisUpdateRequest { * 情绪分析ID */ @NotBlank(message = "情绪分析ID不能为空") + @Size(max = 32, message = "情绪分析ID长度不能超过32个字符") private String id; /** * 消息ID */ + @Size(max = 64, message = "消息ID长度不能超过64个字符") private String messageId; /** * 用户ID */ + @Size(max = 32, message = "用户ID长度不能超过32个字符") private String userId; /** * 主要情绪 */ + @Size(max = 32, message = "主要情绪长度不能超过32个字符") private String primaryEmotion; /** * 情绪极性 */ + @Size(max = 9, message = "情绪极性长度不能超过9个字符") private String polarity; /** diff --git a/backend-single/src/main/java/com/emotion/service/EmotionAnalysisService.java b/backend-single/src/main/java/com/emotion/service/EmotionAnalysisService.java index f52ab2f..1caf0cf 100644 --- a/backend-single/src/main/java/com/emotion/service/EmotionAnalysisService.java +++ b/backend-single/src/main/java/com/emotion/service/EmotionAnalysisService.java @@ -35,67 +35,7 @@ public interface EmotionAnalysisService extends IService { * 根据消息ID查询情绪分析记录 */ EmotionAnalysis getByMessageId(String messageId); - - /** - * 根据主要情绪查询分析记录 - */ - List getByPrimaryEmotion(String primaryEmotion); - - /** - * 根据情绪极性查询分析记录 - */ - List getByPolarity(String polarity); - - /** - * 根据用户ID和情绪类型查询分析记录 - */ - List getByUserIdAndEmotion(String userId, String primaryEmotion); - - /** - * 根据时间范围查询情绪分析记录 - */ - List getByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime); - - /** - * 统计用户的情绪分析记录数量 - */ - Long countByUserId(String userId); - - /** - * 统计指定情绪类型的记录数量 - */ - Long countByPrimaryEmotion(String primaryEmotion); - - /** - * 统计用户指定情绪类型的记录数量 - */ - Long countByUserIdAndEmotion(String userId, String primaryEmotion); - - /** - * 查询用户最近的情绪分析记录 - */ - List getRecentByUserId(String userId, Integer limit); - - /** - * 查询高置信度的情绪分析记录 - */ - List getByMinConfidence(Double minConfidence); - - /** - * 查询用户的平均情绪强度 - */ - Double getAvgIntensityByUserId(String userId); - - /** - * 查询用户指定时间段的平均情绪强度 - */ - Double getAvgIntensityByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime); - - /** - * 查询用户最常见的情绪类型 - */ - String getMostFrequentEmotionByUserId(String userId); - + /** * 创建情绪分析记录 */ @@ -138,30 +78,4 @@ public interface EmotionAnalysisService extends IService { * 删除情绪分析记录 */ boolean deleteEmotionAnalysis(String id); - - /** - * 根据主要情绪查询分析记录响应 - */ - List getEmotionAnalysisResponsesByPrimaryEmotion(String primaryEmotion); - - /** - * 根据情绪极性查询分析记录响应 - */ - List getEmotionAnalysisResponsesByPolarity(String polarity); - - /** - * 根据用户ID和情绪类型查询分析记录响应 - */ - List getEmotionAnalysisResponsesByUserIdAndEmotion(String userId, String primaryEmotion); - - /** - * 根据时间范围查询用户情绪分析记录响应 - */ - List getEmotionAnalysisResponsesByUserIdAndTimeRange(String userId, - LocalDateTime startTime, LocalDateTime endTime); - - /** - * 查询用户最近的情绪分析记录响应 - */ - List getEmotionAnalysisResponsesRecentByUserId(String userId, Integer limit); } \ No newline at end of file diff --git a/backend-single/src/main/java/com/emotion/service/impl/EmotionAnalysisServiceImpl.java b/backend-single/src/main/java/com/emotion/service/impl/EmotionAnalysisServiceImpl.java index aaa1370..cde1fc3 100644 --- a/backend-single/src/main/java/com/emotion/service/impl/EmotionAnalysisServiceImpl.java +++ b/backend-single/src/main/java/com/emotion/service/impl/EmotionAnalysisServiceImpl.java @@ -84,106 +84,6 @@ public class EmotionAnalysisServiceImpl extends ServiceImpl getByPrimaryEmotion(String primaryEmotion) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(EmotionAnalysis::getPrimaryEmotion, primaryEmotion) - .eq(EmotionAnalysis::getIsDeleted, 0) - .orderByDesc(EmotionAnalysis::getCreateTime); - return this.list(wrapper); - } - - @Override - public List getByPolarity(String polarity) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(EmotionAnalysis::getPolarity, polarity) - .eq(EmotionAnalysis::getIsDeleted, 0) - .orderByDesc(EmotionAnalysis::getCreateTime); - return this.list(wrapper); - } - - @Override - public List getByUserIdAndEmotion(String userId, String primaryEmotion) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(EmotionAnalysis::getCreateBy, userId) - .eq(EmotionAnalysis::getPrimaryEmotion, primaryEmotion) - .eq(EmotionAnalysis::getIsDeleted, 0) - .orderByDesc(EmotionAnalysis::getCreateTime); - return this.list(wrapper); - } - - @Override - public List getByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(EmotionAnalysis::getCreateBy, userId) - .between(EmotionAnalysis::getCreateTime, startTime, endTime) - .eq(EmotionAnalysis::getIsDeleted, 0) - .orderByDesc(EmotionAnalysis::getCreateTime); - return this.list(wrapper); - } - - @Override - public Long countByUserId(String userId) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(EmotionAnalysis::getCreateBy, userId) - .eq(EmotionAnalysis::getIsDeleted, 0); - return this.count(wrapper); - } - - @Override - public Long countByPrimaryEmotion(String primaryEmotion) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(EmotionAnalysis::getPrimaryEmotion, primaryEmotion) - .eq(EmotionAnalysis::getIsDeleted, 0); - return this.count(wrapper); - } - - @Override - public Long countByUserIdAndEmotion(String userId, String primaryEmotion) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(EmotionAnalysis::getCreateBy, userId) - .eq(EmotionAnalysis::getPrimaryEmotion, primaryEmotion) - .eq(EmotionAnalysis::getIsDeleted, 0); - return this.count(wrapper); - } - - @Override - public List getRecentByUserId(String userId, Integer limit) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(EmotionAnalysis::getCreateBy, userId) - .eq(EmotionAnalysis::getIsDeleted, 0) - .orderByDesc(EmotionAnalysis::getCreateTime) - .last("LIMIT " + limit); - return this.list(wrapper); - } - - @Override - public List getByMinConfidence(Double minConfidence) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.ge(EmotionAnalysis::getConfidence, minConfidence) - .eq(EmotionAnalysis::getIsDeleted, 0) - .orderByDesc(EmotionAnalysis::getConfidence); - return this.list(wrapper); - } - - @Override - public Double getAvgIntensityByUserId(String userId) { - // 这里需要自定义SQL查询平均值,暂时返回0 - return 0.0; - } - - @Override - public Double getAvgIntensityByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime) { - // 这里需要自定义SQL查询平均值,暂时返回0 - return 0.0; - } - - @Override - public String getMostFrequentEmotionByUserId(String userId) { - // 这里需要自定义SQL查询最频繁的情绪,暂时返回null - return null; - } - @Override public EmotionAnalysis createEmotionAnalysis(String messageId, String userId, String primaryEmotion, String polarity, Double intensity, Double confidence) { @@ -287,48 +187,6 @@ public class EmotionAnalysisServiceImpl extends ServiceImpl getEmotionAnalysisResponsesByPrimaryEmotion(String primaryEmotion) { - List analyses = getByPrimaryEmotion(primaryEmotion); - return analyses.stream() - .map(this::convertToResponse) - .collect(Collectors.toList()); - } - - @Override - public List getEmotionAnalysisResponsesByPolarity(String polarity) { - List analyses = getByPolarity(polarity); - return analyses.stream() - .map(this::convertToResponse) - .collect(Collectors.toList()); - } - - @Override - public List getEmotionAnalysisResponsesByUserIdAndEmotion(String userId, - String primaryEmotion) { - List analyses = getByUserIdAndEmotion(userId, primaryEmotion); - return analyses.stream() - .map(this::convertToResponse) - .collect(Collectors.toList()); - } - - @Override - public List getEmotionAnalysisResponsesByUserIdAndTimeRange(String userId, - LocalDateTime startTime, LocalDateTime endTime) { - List analyses = getByUserIdAndTimeRange(userId, startTime, endTime); - return analyses.stream() - .map(this::convertToResponse) - .collect(Collectors.toList()); - } - - @Override - public List getEmotionAnalysisResponsesRecentByUserId(String userId, Integer limit) { - List analyses = getRecentByUserId(userId, limit); - return analyses.stream() - .map(this::convertToResponse) - .collect(Collectors.toList()); - } - /** * 创建PageResult对象 */ @@ -355,6 +213,13 @@ public class EmotionAnalysisServiceImpl extends ServiceImpl