This commit is contained in:
2025-09-08 18:01:24 +08:00
parent d42d689bd7
commit cf50a9f1fa
6 changed files with 31 additions and 345 deletions
@@ -8,14 +8,10 @@ import com.emotion.dto.request.EmotionAnalysisUpdateRequest;
import com.emotion.dto.response.EmotionAnalysisResponse; import com.emotion.dto.response.EmotionAnalysisResponse;
import com.emotion.service.EmotionAnalysisService; import com.emotion.service.EmotionAnalysisService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
/** /**
@@ -25,7 +21,7 @@ import java.util.List;
* @date 2025-07-23 * @date 2025-07-23
*/ */
@RestController @RestController
@RequestMapping("/emotion-analysis") @RequestMapping("/emotionAnalysis")
public class EmotionAnalysisController { public class EmotionAnalysisController {
@Autowired @Autowired
@@ -34,25 +30,15 @@ public class EmotionAnalysisController {
/** /**
* 分页查询情绪分析记录 * 分页查询情绪分析记录
*/ */
@GetMapping("/page") @GetMapping(value = "/page")
public Result<PageResult<EmotionAnalysisResponse>> getPage(@Validated EmotionAnalysisPageRequest request) { public Result<PageResult<EmotionAnalysisResponse>> getPage(@Validated EmotionAnalysisPageRequest request) {
return Result.success(emotionAnalysisService.getPageWithResponse(request)); return Result.success(emotionAnalysisService.getPageWithResponse(request));
} }
/**
* 根据用户ID分页查询情绪分析记录
*/
@GetMapping("/user/page")
public Result<PageResult<EmotionAnalysisResponse>> getPageByUserId(
@RequestParam String userId,
@Validated EmotionAnalysisPageRequest request) {
return Result.success(emotionAnalysisService.getPageByUserIdWithResponse(userId, request));
}
/** /**
* 根据ID获取情绪分析记录 * 根据ID获取情绪分析记录
*/ */
@GetMapping @GetMapping(value = "/detail")
public Result<EmotionAnalysisResponse> getById(@RequestParam String id) { public Result<EmotionAnalysisResponse> getById(@RequestParam String id) {
EmotionAnalysisResponse response = emotionAnalysisService.getEmotionAnalysisResponseById(id); EmotionAnalysisResponse response = emotionAnalysisService.getEmotionAnalysisResponseById(id);
if (response == null) { if (response == null) {
@@ -61,22 +47,10 @@ public class EmotionAnalysisController {
return Result.success(response); return Result.success(response);
} }
/**
* 根据消息ID获取情绪分析记录
*/
@GetMapping("/message")
public Result<EmotionAnalysisResponse> 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<EmotionAnalysisResponse> create(@RequestBody @Valid EmotionAnalysisCreateRequest request) { public Result<EmotionAnalysisResponse> create(@RequestBody @Valid EmotionAnalysisCreateRequest request) {
return Result.success(emotionAnalysisService.createEmotionAnalysisWithResponse(request)); return Result.success(emotionAnalysisService.createEmotionAnalysisWithResponse(request));
} }
@@ -84,7 +58,7 @@ public class EmotionAnalysisController {
/** /**
* 更新情绪分析记录 * 更新情绪分析记录
*/ */
@PutMapping @PutMapping(value = "/update")
public Result<EmotionAnalysisResponse> update(@RequestBody @Valid EmotionAnalysisUpdateRequest request) { public Result<EmotionAnalysisResponse> update(@RequestBody @Valid EmotionAnalysisUpdateRequest request) {
EmotionAnalysisResponse response = emotionAnalysisService.updateEmotionAnalysisWithResponse(request); EmotionAnalysisResponse response = emotionAnalysisService.updateEmotionAnalysisWithResponse(request);
if (response == null) { if (response == null) {
@@ -96,7 +70,7 @@ public class EmotionAnalysisController {
/** /**
* 删除情绪分析记录 * 删除情绪分析记录
*/ */
@DeleteMapping @DeleteMapping(value = "/delete")
public Result<Void> delete(@RequestParam String id) { public Result<Void> delete(@RequestParam String id) {
boolean deleted = emotionAnalysisService.deleteEmotionAnalysis(id); boolean deleted = emotionAnalysisService.deleteEmotionAnalysis(id);
if (!deleted) { if (!deleted) {
@@ -104,88 +78,4 @@ public class EmotionAnalysisController {
} }
return Result.success(); return Result.success();
} }
/**
* 根据主要情绪查询分析记录
*/
@GetMapping("/emotion")
public Result<List<EmotionAnalysisResponse>> getByPrimaryEmotion(@RequestParam String primaryEmotion) {
List<EmotionAnalysisResponse> responses = emotionAnalysisService
.getEmotionAnalysisResponsesByPrimaryEmotion(primaryEmotion);
return Result.success(responses);
}
/**
* 根据情绪极性查询分析记录
*/
@GetMapping("/polarity")
public Result<List<EmotionAnalysisResponse>> getByPolarity(@RequestParam String polarity) {
List<EmotionAnalysisResponse> responses = emotionAnalysisService
.getEmotionAnalysisResponsesByPolarity(polarity);
return Result.success(responses);
}
/**
* 根据用户ID和情绪类型查询分析记录
*/
@GetMapping("/user/emotion")
public Result<List<EmotionAnalysisResponse>> getByUserIdAndEmotion(
@RequestParam String userId,
@RequestParam String primaryEmotion) {
List<EmotionAnalysisResponse> responses = emotionAnalysisService
.getEmotionAnalysisResponsesByUserIdAndEmotion(userId, primaryEmotion);
return Result.success(responses);
}
/**
* 根据时间范围查询用户情绪分析记录
*/
@GetMapping("/user/time-range")
public Result<List<EmotionAnalysisResponse>> 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<EmotionAnalysisResponse> responses = emotionAnalysisService
.getEmotionAnalysisResponsesByUserIdAndTimeRange(userId, startTime, endTime);
return Result.success(responses);
}
/**
* 统计用户的情绪分析记录数量
*/
@GetMapping("/user/count")
public Result<Long> countByUserId(@RequestParam String userId) {
Long count = emotionAnalysisService.countByUserId(userId);
return Result.success(count);
}
/**
* 查询用户最近的情绪分析记录
*/
@GetMapping("/user/recent")
public Result<List<EmotionAnalysisResponse>> getRecentByUserId(
@RequestParam String userId,
@RequestParam(defaultValue = "10") Integer limit) {
List<EmotionAnalysisResponse> responses = emotionAnalysisService
.getEmotionAnalysisResponsesRecentByUserId(userId, limit);
return Result.success(responses);
}
/**
* 查询用户的平均情绪强度
*/
@GetMapping("/user/avg-intensity")
public Result<Double> getAvgIntensityByUserId(@RequestParam String userId) {
Double avgIntensity = emotionAnalysisService.getAvgIntensityByUserId(userId);
return Result.success(avgIntensity);
}
/**
* 查询用户最常见的情绪类型
*/
@GetMapping("/user/most-frequent-emotion")
public Result<String> getMostFrequentEmotionByUserId(@RequestParam String userId) {
String emotion = emotionAnalysisService.getMostFrequentEmotionByUserId(userId);
return Result.success(emotion);
}
} }
@@ -4,6 +4,7 @@ import lombok.Data;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/** /**
* 情绪分析创建请求类 * 情绪分析创建请求类
@@ -18,23 +19,27 @@ public class EmotionAnalysisCreateRequest {
* 消息ID * 消息ID
*/ */
@NotBlank(message = "消息ID不能为空") @NotBlank(message = "消息ID不能为空")
@Size(max = 64, message = "消息ID长度不能超过64个字符")
private String messageId; private String messageId;
/** /**
* 用户ID * 用户ID
*/ */
@NotBlank(message = "用户ID不能为空") @NotBlank(message = "用户ID不能为空")
@Size(max = 32, message = "用户ID长度不能超过32个字符")
private String userId; private String userId;
/** /**
* 主要情绪 * 主要情绪
*/ */
@NotBlank(message = "主要情绪不能为空") @NotBlank(message = "主要情绪不能为空")
@Size(max = 32, message = "主要情绪长度不能超过32个字符")
private String primaryEmotion; private String primaryEmotion;
/** /**
* 情绪极性 * 情绪极性
*/ */
@Size(max = 9, message = "情绪极性长度不能超过9个字符")
private String polarity; private String polarity;
/** /**
@@ -4,6 +4,8 @@ import com.emotion.common.BasePageRequest;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import javax.validation.constraints.Size;
/** /**
* 情绪分析分页请求类 * 情绪分析分页请求类
* *
@@ -17,20 +19,24 @@ public class EmotionAnalysisPageRequest extends BasePageRequest {
/** /**
* 用户ID(可选) * 用户ID(可选)
*/ */
@Size(max = 32, message = "用户ID长度不能超过32个字符")
private String userId; private String userId;
/** /**
* 消息ID(可选) * 消息ID(可选)
*/ */
@Size(max = 64, message = "消息ID长度不能超过64个字符")
private String messageId; private String messageId;
/** /**
* 主要情绪(可选) * 主要情绪(可选)
*/ */
@Size(max = 32, message = "主要情绪长度不能超过32个字符")
private String primaryEmotion; private String primaryEmotion;
/** /**
* 情绪极性(可选) * 情绪极性(可选)
*/ */
@Size(max = 9, message = "情绪极性长度不能超过9个字符")
private String polarity; private String polarity;
} }
@@ -3,6 +3,7 @@ package com.emotion.dto.request;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
/** /**
* 情绪分析更新请求类 * 情绪分析更新请求类
@@ -17,26 +18,31 @@ public class EmotionAnalysisUpdateRequest {
* 情绪分析ID * 情绪分析ID
*/ */
@NotBlank(message = "情绪分析ID不能为空") @NotBlank(message = "情绪分析ID不能为空")
@Size(max = 32, message = "情绪分析ID长度不能超过32个字符")
private String id; private String id;
/** /**
* 消息ID * 消息ID
*/ */
@Size(max = 64, message = "消息ID长度不能超过64个字符")
private String messageId; private String messageId;
/** /**
* 用户ID * 用户ID
*/ */
@Size(max = 32, message = "用户ID长度不能超过32个字符")
private String userId; private String userId;
/** /**
* 主要情绪 * 主要情绪
*/ */
@Size(max = 32, message = "主要情绪长度不能超过32个字符")
private String primaryEmotion; private String primaryEmotion;
/** /**
* 情绪极性 * 情绪极性
*/ */
@Size(max = 9, message = "情绪极性长度不能超过9个字符")
private String polarity; private String polarity;
/** /**
@@ -36,66 +36,6 @@ public interface EmotionAnalysisService extends IService<EmotionAnalysis> {
*/ */
EmotionAnalysis getByMessageId(String messageId); EmotionAnalysis getByMessageId(String messageId);
/**
* 根据主要情绪查询分析记录
*/
List<EmotionAnalysis> getByPrimaryEmotion(String primaryEmotion);
/**
* 根据情绪极性查询分析记录
*/
List<EmotionAnalysis> getByPolarity(String polarity);
/**
* 根据用户ID和情绪类型查询分析记录
*/
List<EmotionAnalysis> getByUserIdAndEmotion(String userId, String primaryEmotion);
/**
* 根据时间范围查询情绪分析记录
*/
List<EmotionAnalysis> getByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime);
/**
* 统计用户的情绪分析记录数量
*/
Long countByUserId(String userId);
/**
* 统计指定情绪类型的记录数量
*/
Long countByPrimaryEmotion(String primaryEmotion);
/**
* 统计用户指定情绪类型的记录数量
*/
Long countByUserIdAndEmotion(String userId, String primaryEmotion);
/**
* 查询用户最近的情绪分析记录
*/
List<EmotionAnalysis> getRecentByUserId(String userId, Integer limit);
/**
* 查询高置信度的情绪分析记录
*/
List<EmotionAnalysis> 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<EmotionAnalysis> {
* 删除情绪分析记录 * 删除情绪分析记录
*/ */
boolean deleteEmotionAnalysis(String id); boolean deleteEmotionAnalysis(String id);
/**
* 根据主要情绪查询分析记录响应
*/
List<EmotionAnalysisResponse> getEmotionAnalysisResponsesByPrimaryEmotion(String primaryEmotion);
/**
* 根据情绪极性查询分析记录响应
*/
List<EmotionAnalysisResponse> getEmotionAnalysisResponsesByPolarity(String polarity);
/**
* 根据用户ID和情绪类型查询分析记录响应
*/
List<EmotionAnalysisResponse> getEmotionAnalysisResponsesByUserIdAndEmotion(String userId, String primaryEmotion);
/**
* 根据时间范围查询用户情绪分析记录响应
*/
List<EmotionAnalysisResponse> getEmotionAnalysisResponsesByUserIdAndTimeRange(String userId,
LocalDateTime startTime, LocalDateTime endTime);
/**
* 查询用户最近的情绪分析记录响应
*/
List<EmotionAnalysisResponse> getEmotionAnalysisResponsesRecentByUserId(String userId, Integer limit);
} }
@@ -84,106 +84,6 @@ public class EmotionAnalysisServiceImpl extends ServiceImpl<EmotionAnalysisMappe
return this.getOne(wrapper); return this.getOne(wrapper);
} }
@Override
public List<EmotionAnalysis> getByPrimaryEmotion(String primaryEmotion) {
LambdaQueryWrapper<EmotionAnalysis> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(EmotionAnalysis::getPrimaryEmotion, primaryEmotion)
.eq(EmotionAnalysis::getIsDeleted, 0)
.orderByDesc(EmotionAnalysis::getCreateTime);
return this.list(wrapper);
}
@Override
public List<EmotionAnalysis> getByPolarity(String polarity) {
LambdaQueryWrapper<EmotionAnalysis> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(EmotionAnalysis::getPolarity, polarity)
.eq(EmotionAnalysis::getIsDeleted, 0)
.orderByDesc(EmotionAnalysis::getCreateTime);
return this.list(wrapper);
}
@Override
public List<EmotionAnalysis> getByUserIdAndEmotion(String userId, String primaryEmotion) {
LambdaQueryWrapper<EmotionAnalysis> 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<EmotionAnalysis> getByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime) {
LambdaQueryWrapper<EmotionAnalysis> 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<EmotionAnalysis> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(EmotionAnalysis::getCreateBy, userId)
.eq(EmotionAnalysis::getIsDeleted, 0);
return this.count(wrapper);
}
@Override
public Long countByPrimaryEmotion(String primaryEmotion) {
LambdaQueryWrapper<EmotionAnalysis> 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<EmotionAnalysis> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(EmotionAnalysis::getCreateBy, userId)
.eq(EmotionAnalysis::getPrimaryEmotion, primaryEmotion)
.eq(EmotionAnalysis::getIsDeleted, 0);
return this.count(wrapper);
}
@Override
public List<EmotionAnalysis> getRecentByUserId(String userId, Integer limit) {
LambdaQueryWrapper<EmotionAnalysis> 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<EmotionAnalysis> getByMinConfidence(Double minConfidence) {
LambdaQueryWrapper<EmotionAnalysis> 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 @Override
public EmotionAnalysis createEmotionAnalysis(String messageId, String userId, String primaryEmotion, public EmotionAnalysis createEmotionAnalysis(String messageId, String userId, String primaryEmotion,
String polarity, Double intensity, Double confidence) { String polarity, Double intensity, Double confidence) {
@@ -287,48 +187,6 @@ public class EmotionAnalysisServiceImpl extends ServiceImpl<EmotionAnalysisMappe
return this.removeById(id); return this.removeById(id);
} }
@Override
public List<EmotionAnalysisResponse> getEmotionAnalysisResponsesByPrimaryEmotion(String primaryEmotion) {
List<EmotionAnalysis> analyses = getByPrimaryEmotion(primaryEmotion);
return analyses.stream()
.map(this::convertToResponse)
.collect(Collectors.toList());
}
@Override
public List<EmotionAnalysisResponse> getEmotionAnalysisResponsesByPolarity(String polarity) {
List<EmotionAnalysis> analyses = getByPolarity(polarity);
return analyses.stream()
.map(this::convertToResponse)
.collect(Collectors.toList());
}
@Override
public List<EmotionAnalysisResponse> getEmotionAnalysisResponsesByUserIdAndEmotion(String userId,
String primaryEmotion) {
List<EmotionAnalysis> analyses = getByUserIdAndEmotion(userId, primaryEmotion);
return analyses.stream()
.map(this::convertToResponse)
.collect(Collectors.toList());
}
@Override
public List<EmotionAnalysisResponse> getEmotionAnalysisResponsesByUserIdAndTimeRange(String userId,
LocalDateTime startTime, LocalDateTime endTime) {
List<EmotionAnalysis> analyses = getByUserIdAndTimeRange(userId, startTime, endTime);
return analyses.stream()
.map(this::convertToResponse)
.collect(Collectors.toList());
}
@Override
public List<EmotionAnalysisResponse> getEmotionAnalysisResponsesRecentByUserId(String userId, Integer limit) {
List<EmotionAnalysis> analyses = getRecentByUserId(userId, limit);
return analyses.stream()
.map(this::convertToResponse)
.collect(Collectors.toList());
}
/** /**
* 创建PageResult对象 * 创建PageResult对象
*/ */
@@ -355,6 +213,13 @@ public class EmotionAnalysisServiceImpl extends ServiceImpl<EmotionAnalysisMappe
if (analysis.getUpdateTime() != null) { if (analysis.getUpdateTime() != null) {
response.setUpdateTime(analysis.getUpdateTime().format(DATE_TIME_FORMATTER)); response.setUpdateTime(analysis.getUpdateTime().format(DATE_TIME_FORMATTER));
} }
// 处理Double类型转换
if (analysis.getIntensity() != null) {
response.setIntensity(analysis.getIntensity().doubleValue());
}
if (analysis.getConfidence() != null) {
response.setConfidence(analysis.getConfidence().doubleValue());
}
return response; return response;
} }
} }