接口优化
This commit is contained in:
+64
-160
@@ -1,24 +1,22 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotion.common.PageResult;
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.PageRequest;
|
||||
import com.emotion.dto.response.BaseResponse;
|
||||
import com.emotion.entity.EmotionAnalysis;
|
||||
import com.emotion.dto.request.EmotionAnalysisCreateRequest;
|
||||
import com.emotion.dto.request.EmotionAnalysisPageRequest;
|
||||
import com.emotion.dto.request.EmotionAnalysisUpdateRequest;
|
||||
import com.emotion.dto.response.EmotionAnalysisResponse;
|
||||
import com.emotion.service.EmotionAnalysisService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
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.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 情绪分析控制器
|
||||
@@ -33,108 +31,74 @@ public class EmotionAnalysisController {
|
||||
@Autowired
|
||||
private EmotionAnalysisService emotionAnalysisService;
|
||||
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/**
|
||||
* 分页查询情绪分析记录
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public Result<PageResult<EmotionAnalysisResponse>> getPage(@Validated PageRequest request) {
|
||||
IPage<EmotionAnalysis> page = emotionAnalysisService.getPage(request);
|
||||
List<EmotionAnalysisResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<EmotionAnalysisResponse> 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);
|
||||
public Result<PageResult<EmotionAnalysisResponse>> getPage(@Validated EmotionAnalysisPageRequest request) {
|
||||
return Result.success(emotionAnalysisService.getPageWithResponse(request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID分页查询情绪分析记录
|
||||
*/
|
||||
@GetMapping("/user/{userId}/page")
|
||||
public Result<PageResult<EmotionAnalysisResponse>> getPageByUserId(@PathVariable String userId, @Validated PageRequest request) {
|
||||
IPage<EmotionAnalysis> page = emotionAnalysisService.getPageByUserId(request, userId);
|
||||
List<EmotionAnalysisResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<EmotionAnalysisResponse> 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);
|
||||
@GetMapping("/user/page")
|
||||
public Result<PageResult<EmotionAnalysisResponse>> getPageByUserId(
|
||||
@RequestParam String userId,
|
||||
@Validated EmotionAnalysisPageRequest request) {
|
||||
return Result.success(emotionAnalysisService.getPageByUserIdWithResponse(userId, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取情绪分析记录
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<EmotionAnalysisResponse> getById(@PathVariable String id) {
|
||||
EmotionAnalysis analysis = emotionAnalysisService.getById(id);
|
||||
if (analysis == null) {
|
||||
@GetMapping
|
||||
public Result<EmotionAnalysisResponse> getById(@RequestParam String id) {
|
||||
EmotionAnalysisResponse response = emotionAnalysisService.getEmotionAnalysisResponseById(id);
|
||||
if (response == null) {
|
||||
return Result.notFound("情绪分析记录不存在");
|
||||
}
|
||||
return Result.success(convertToResponse(analysis));
|
||||
return Result.success(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据消息ID获取情绪分析记录
|
||||
*/
|
||||
@GetMapping("/message/{messageId}")
|
||||
public Result<EmotionAnalysisResponse> getByMessageId(@PathVariable String messageId) {
|
||||
EmotionAnalysis analysis = emotionAnalysisService.getByMessageId(messageId);
|
||||
if (analysis == null) {
|
||||
@GetMapping("/message")
|
||||
public Result<EmotionAnalysisResponse> getByMessageId(@RequestParam String messageId) {
|
||||
EmotionAnalysisResponse response = emotionAnalysisService.getEmotionAnalysisResponseByMessageId(messageId);
|
||||
if (response == null) {
|
||||
return Result.notFound("情绪分析记录不存在");
|
||||
}
|
||||
return Result.success(convertToResponse(analysis));
|
||||
return Result.success(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建情绪分析记录
|
||||
*/
|
||||
@PostMapping
|
||||
public Result<EmotionAnalysisResponse> create(@RequestBody @Validated EmotionAnalysisCreateRequest request) {
|
||||
EmotionAnalysis analysis = emotionAnalysisService.createEmotionAnalysis(
|
||||
request.getMessageId(),
|
||||
request.getUserId(),
|
||||
request.getPrimaryEmotion(),
|
||||
request.getPolarity(),
|
||||
request.getIntensity(),
|
||||
request.getConfidence()
|
||||
);
|
||||
return Result.success(convertToResponse(analysis));
|
||||
public Result<EmotionAnalysisResponse> create(@RequestBody @Valid EmotionAnalysisCreateRequest request) {
|
||||
return Result.success(emotionAnalysisService.createEmotionAnalysisWithResponse(request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新情绪分析记录
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
public Result<EmotionAnalysisResponse> update(@PathVariable String id, @RequestBody EmotionAnalysis analysis) {
|
||||
analysis.setId(id);
|
||||
boolean updated = emotionAnalysisService.updateById(analysis);
|
||||
if (!updated) {
|
||||
@PutMapping
|
||||
public Result<EmotionAnalysisResponse> update(@RequestBody @Valid EmotionAnalysisUpdateRequest request) {
|
||||
EmotionAnalysisResponse response = emotionAnalysisService.updateEmotionAnalysisWithResponse(request);
|
||||
if (response == null) {
|
||||
return Result.error("更新失败");
|
||||
}
|
||||
EmotionAnalysis updatedAnalysis = emotionAnalysisService.getById(id);
|
||||
return Result.success(convertToResponse(updatedAnalysis));
|
||||
return Result.success(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除情绪分析记录
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Void> delete(@PathVariable String id) {
|
||||
boolean deleted = emotionAnalysisService.removeById(id);
|
||||
@DeleteMapping
|
||||
public Result<Void> delete(@RequestParam String id) {
|
||||
boolean deleted = emotionAnalysisService.deleteEmotionAnalysis(id);
|
||||
if (!deleted) {
|
||||
return Result.error("删除失败");
|
||||
}
|
||||
@@ -144,59 +108,53 @@ public class EmotionAnalysisController {
|
||||
/**
|
||||
* 根据主要情绪查询分析记录
|
||||
*/
|
||||
@GetMapping("/emotion/{primaryEmotion}")
|
||||
public Result<List<EmotionAnalysisResponse>> getByPrimaryEmotion(@PathVariable String primaryEmotion) {
|
||||
List<EmotionAnalysis> analyses = emotionAnalysisService.getByPrimaryEmotion(primaryEmotion);
|
||||
List<EmotionAnalysisResponse> responses = analyses.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
@GetMapping("/emotion")
|
||||
public Result<List<EmotionAnalysisResponse>> getByPrimaryEmotion(@RequestParam String primaryEmotion) {
|
||||
List<EmotionAnalysisResponse> responses = emotionAnalysisService
|
||||
.getEmotionAnalysisResponsesByPrimaryEmotion(primaryEmotion);
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据情绪极性查询分析记录
|
||||
*/
|
||||
@GetMapping("/polarity/{polarity}")
|
||||
public Result<List<EmotionAnalysisResponse>> getByPolarity(@PathVariable String polarity) {
|
||||
List<EmotionAnalysis> analyses = emotionAnalysisService.getByPolarity(polarity);
|
||||
List<EmotionAnalysisResponse> responses = analyses.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
@GetMapping("/polarity")
|
||||
public Result<List<EmotionAnalysisResponse>> getByPolarity(@RequestParam String polarity) {
|
||||
List<EmotionAnalysisResponse> responses = emotionAnalysisService
|
||||
.getEmotionAnalysisResponsesByPolarity(polarity);
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID和情绪类型查询分析记录
|
||||
*/
|
||||
@GetMapping("/user/{userId}/emotion/{primaryEmotion}")
|
||||
public Result<List<EmotionAnalysisResponse>> getByUserIdAndEmotion(@PathVariable String userId, @PathVariable String primaryEmotion) {
|
||||
List<EmotionAnalysis> analyses = emotionAnalysisService.getByUserIdAndEmotion(userId, primaryEmotion);
|
||||
List<EmotionAnalysisResponse> responses = analyses.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
@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/{userId}/time-range")
|
||||
@GetMapping("/user/time-range")
|
||||
public Result<List<EmotionAnalysisResponse>> getByUserIdAndTimeRange(
|
||||
@PathVariable String userId,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
|
||||
@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<EmotionAnalysis> analyses = emotionAnalysisService.getByUserIdAndTimeRange(userId, startTime, endTime);
|
||||
List<EmotionAnalysisResponse> responses = analyses.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
List<EmotionAnalysisResponse> responses = emotionAnalysisService
|
||||
.getEmotionAnalysisResponsesByUserIdAndTimeRange(userId, startTime, endTime);
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计用户的情绪分析记录数量
|
||||
*/
|
||||
@GetMapping("/user/{userId}/count")
|
||||
public Result<Long> countByUserId(@PathVariable String userId) {
|
||||
@GetMapping("/user/count")
|
||||
public Result<Long> countByUserId(@RequestParam String userId) {
|
||||
Long count = emotionAnalysisService.countByUserId(userId);
|
||||
return Result.success(count);
|
||||
}
|
||||
@@ -204,20 +162,20 @@ public class EmotionAnalysisController {
|
||||
/**
|
||||
* 查询用户最近的情绪分析记录
|
||||
*/
|
||||
@GetMapping("/user/{userId}/recent")
|
||||
public Result<List<EmotionAnalysisResponse>> getRecentByUserId(@PathVariable String userId, @RequestParam(defaultValue = "10") Integer limit) {
|
||||
List<EmotionAnalysis> analyses = emotionAnalysisService.getRecentByUserId(userId, limit);
|
||||
List<EmotionAnalysisResponse> responses = analyses.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
@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/{userId}/avg-intensity")
|
||||
public Result<Double> getAvgIntensityByUserId(@PathVariable String userId) {
|
||||
@GetMapping("/user/avg-intensity")
|
||||
public Result<Double> getAvgIntensityByUserId(@RequestParam String userId) {
|
||||
Double avgIntensity = emotionAnalysisService.getAvgIntensityByUserId(userId);
|
||||
return Result.success(avgIntensity);
|
||||
}
|
||||
@@ -225,63 +183,9 @@ public class EmotionAnalysisController {
|
||||
/**
|
||||
* 查询用户最常见的情绪类型
|
||||
*/
|
||||
@GetMapping("/user/{userId}/most-frequent-emotion")
|
||||
public Result<String> getMostFrequentEmotionByUserId(@PathVariable String userId) {
|
||||
@GetMapping("/user/most-frequent-emotion")
|
||||
public Result<String> getMostFrequentEmotionByUserId(@RequestParam String userId) {
|
||||
String emotion = emotionAnalysisService.getMostFrequentEmotionByUserId(userId);
|
||||
return Result.success(emotion);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为响应对象
|
||||
*/
|
||||
private EmotionAnalysisResponse convertToResponse(EmotionAnalysis analysis) {
|
||||
EmotionAnalysisResponse response = new EmotionAnalysisResponse();
|
||||
BeanUtils.copyProperties(analysis, response);
|
||||
response.setId(analysis.getId());
|
||||
if (analysis.getCreateTime() != null) {
|
||||
response.setCreateTime(analysis.getCreateTime().format(DATE_TIME_FORMATTER));
|
||||
}
|
||||
if (analysis.getUpdateTime() != null) {
|
||||
response.setUpdateTime(analysis.getUpdateTime().format(DATE_TIME_FORMATTER));
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 情绪分析创建请求
|
||||
*/
|
||||
@lombok.Data
|
||||
public static class EmotionAnalysisCreateRequest {
|
||||
@NotBlank(message = "消息ID不能为空")
|
||||
private String messageId;
|
||||
|
||||
@NotBlank(message = "用户ID不能为空")
|
||||
private String userId;
|
||||
|
||||
@NotBlank(message = "主要情绪不能为空")
|
||||
private String primaryEmotion;
|
||||
|
||||
private String polarity;
|
||||
|
||||
@NotNull(message = "情绪强度不能为空")
|
||||
private Double intensity;
|
||||
|
||||
@NotNull(message = "置信度不能为空")
|
||||
private Double confidence;
|
||||
}
|
||||
|
||||
/**
|
||||
* 情绪分析响应类
|
||||
*/
|
||||
@lombok.Data
|
||||
@lombok.EqualsAndHashCode(callSuper = true)
|
||||
public static class EmotionAnalysisResponse extends BaseResponse {
|
||||
private String messageId;
|
||||
private String userId;
|
||||
private String primaryEmotion;
|
||||
private String polarity;
|
||||
private Double intensity;
|
||||
private Double confidence;
|
||||
private String emotionDetails;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user