feat: 完善后端架构 - 标准化Controller层和Service层
✨ 新功能: - 创建了完整的Service层架构,包含所有业务实体的Service接口和实现类 - 新增8个标准化的Controller类,支持完整的CRUD操作 - 实现了统一的Request/Response模式和分页查询功能 - 创建了认证服务(AuthService)和令牌服务(TokenService) - 添加了Redis配置和认证拦截器 🏗️ 架构优化: - 移除Controller层所有try-catch块,使用全局异常处理机制 - 创建了专门的异常类(AuthException, TokenException, CaptchaException) - 统一了API返回格式,完善了Result类的方法 - 实现了标准的分页查询和参数校验 📦 新增文件: - 8个Controller类: Achievement, Comment, CommunityPost, Conversation, CozeApiCall, EmotionAnalysis, Reward, UserStats - 12个Service接口和对应的实现类 - 标准化的DTO类(Request/Response) - 异常处理类和拦截器 - 测试用例 🔧 重构优化: - 重写了AuthController,移除所有业务逻辑到Service层 - 优化了MessageController,使用标准的Request/Response格式 - 更新了全局异常处理器,支持多种异常类型 - 完善了WebConfig配置,添加认证拦截器 📊 代码统计: - 新增文件: 60+个 - 新增代码行数: 8000+行 - 重构代码行数: 1000+行 - 移除过时接口: 4个
This commit is contained in:
@@ -0,0 +1,287 @@
|
||||
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.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.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;
|
||||
|
||||
/**
|
||||
* 情绪分析控制器
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/emotion-analysis")
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取情绪分析记录
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<EmotionAnalysisResponse> getById(@PathVariable String id) {
|
||||
EmotionAnalysis analysis = emotionAnalysisService.getById(id);
|
||||
if (analysis == null) {
|
||||
return Result.notFound("情绪分析记录不存在");
|
||||
}
|
||||
return Result.success(convertToResponse(analysis));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据消息ID获取情绪分析记录
|
||||
*/
|
||||
@GetMapping("/message/{messageId}")
|
||||
public Result<EmotionAnalysisResponse> getByMessageId(@PathVariable String messageId) {
|
||||
EmotionAnalysis analysis = emotionAnalysisService.getByMessageId(messageId);
|
||||
if (analysis == null) {
|
||||
return Result.notFound("情绪分析记录不存在");
|
||||
}
|
||||
return Result.success(convertToResponse(analysis));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建情绪分析记录
|
||||
*/
|
||||
@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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新情绪分析记录
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
public Result<EmotionAnalysisResponse> update(@PathVariable String id, @RequestBody EmotionAnalysis analysis) {
|
||||
analysis.setId(id);
|
||||
boolean updated = emotionAnalysisService.updateById(analysis);
|
||||
if (!updated) {
|
||||
return Result.error("更新失败");
|
||||
}
|
||||
EmotionAnalysis updatedAnalysis = emotionAnalysisService.getById(id);
|
||||
return Result.success(convertToResponse(updatedAnalysis));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除情绪分析记录
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Void> delete(@PathVariable String id) {
|
||||
boolean deleted = emotionAnalysisService.removeById(id);
|
||||
if (!deleted) {
|
||||
return Result.error("删除失败");
|
||||
}
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主要情绪查询分析记录
|
||||
*/
|
||||
@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());
|
||||
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());
|
||||
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());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间范围查询用户情绪分析记录
|
||||
*/
|
||||
@GetMapping("/user/{userId}/time-range")
|
||||
public Result<List<EmotionAnalysisResponse>> getByUserIdAndTimeRange(
|
||||
@PathVariable 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());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计用户的情绪分析记录数量
|
||||
*/
|
||||
@GetMapping("/user/{userId}/count")
|
||||
public Result<Long> countByUserId(@PathVariable String userId) {
|
||||
Long count = emotionAnalysisService.countByUserId(userId);
|
||||
return Result.success(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户最近的情绪分析记录
|
||||
*/
|
||||
@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());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户的平均情绪强度
|
||||
*/
|
||||
@GetMapping("/user/{userId}/avg-intensity")
|
||||
public Result<Double> getAvgIntensityByUserId(@PathVariable String userId) {
|
||||
Double avgIntensity = emotionAnalysisService.getAvgIntensityByUserId(userId);
|
||||
return Result.success(avgIntensity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户最常见的情绪类型
|
||||
*/
|
||||
@GetMapping("/user/{userId}/most-frequent-emotion")
|
||||
public Result<String> getMostFrequentEmotionByUserId(@PathVariable 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