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:
2025-07-24 07:38:40 +08:00
parent 880e0e3c88
commit 873b8e55da
67 changed files with 8619 additions and 850 deletions
@@ -0,0 +1,251 @@
package com.emotion.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emotion.common.BasePageRequest;
import com.emotion.entity.GrowthTopic;
import com.emotion.mapper.GrowthTopicMapper;
import com.emotion.service.GrowthTopicService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
/**
* 成长话题服务实现类
*
* @author emotion-museum
* @date 2025-07-23
*/
@Service
public class GrowthTopicServiceImpl extends ServiceImpl<GrowthTopicMapper, GrowthTopic> implements GrowthTopicService {
@Override
public IPage<GrowthTopic> getPage(BasePageRequest request) {
Page<GrowthTopic> page = new Page<>(request.getCurrent(), request.getSize());
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
// 关键词搜索
if (StringUtils.hasText(request.getKeyword())) {
wrapper.and(w -> w.like(GrowthTopic::getTitle, request.getKeyword())
.or().like(GrowthTopic::getDescription, request.getKeyword())
.or().like(GrowthTopic::getTags, request.getKeyword()));
}
wrapper.eq(GrowthTopic::getIsDeleted, 0);
// 排序
if (StringUtils.hasText(request.getOrderBy())) {
if ("asc".equalsIgnoreCase(request.getOrderDirection())) {
wrapper.orderByAsc(GrowthTopic::getCreateTime);
} else {
wrapper.orderByDesc(GrowthTopic::getCreateTime);
}
} else {
wrapper.orderByDesc(GrowthTopic::getCreateTime);
}
return this.page(page, wrapper);
}
@Override
public List<GrowthTopic> getByCategory(String category) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getCategory, category)
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getCreateTime);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> getByDifficultyLevel(String difficultyLevel) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getDifficultyLevel, difficultyLevel)
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getCreateTime);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> getByStatus(String status) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getStatus, status)
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getCreateTime);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> getRecommendedTopics(Integer limit) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getStatus, "active")
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getParticipantCount)
.last("LIMIT " + limit);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> getPopularTopics(Integer limit) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getParticipantCount)
.last("LIMIT " + limit);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> getLatestTopics(Integer limit) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getCreateTime)
.last("LIMIT " + limit);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> getByParticipantRange(Integer minParticipants, Integer maxParticipants) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.between(GrowthTopic::getParticipantCount, minParticipants, maxParticipants)
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getParticipantCount);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> getByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.between(GrowthTopic::getCreateTime, startTime, endTime)
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getCreateTime);
return this.list(wrapper);
}
@Override
public Long countByCategory(String category) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getCategory, category)
.eq(GrowthTopic::getIsDeleted, 0);
return this.count(wrapper);
}
@Override
public Long countByStatus(String status) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getStatus, status)
.eq(GrowthTopic::getIsDeleted, 0);
return this.count(wrapper);
}
@Override
public Long countByDifficultyLevel(String difficultyLevel) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GrowthTopic::getDifficultyLevel, difficultyLevel)
.eq(GrowthTopic::getIsDeleted, 0);
return this.count(wrapper);
}
@Override
public Double getAvgParticipantCount() {
List<GrowthTopic> topics = this.list(new LambdaQueryWrapper<GrowthTopic>()
.eq(GrowthTopic::getIsDeleted, 0)
.isNotNull(GrowthTopic::getParticipantCount));
return topics.stream()
.mapToDouble(t -> t.getParticipantCount() != null ? t.getParticipantCount().doubleValue() : 0.0)
.average()
.orElse(0.0);
}
@Override
public Double getAvgParticipantCountByCategory(String category) {
List<GrowthTopic> topics = this.list(new LambdaQueryWrapper<GrowthTopic>()
.eq(GrowthTopic::getCategory, category)
.eq(GrowthTopic::getIsDeleted, 0)
.isNotNull(GrowthTopic::getParticipantCount));
return topics.stream()
.mapToDouble(t -> t.getParticipantCount() != null ? t.getParticipantCount().doubleValue() : 0.0)
.average()
.orElse(0.0);
}
@Override
public List<GrowthTopic> searchByTags(String tags) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.like(GrowthTopic::getTags, tags)
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getCreateTime);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> searchByKeyword(String keyword) {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.and(w -> w.like(GrowthTopic::getTitle, keyword)
.or().like(GrowthTopic::getDescription, keyword)
.or().like(GrowthTopic::getTags, keyword))
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getCreateTime);
return this.list(wrapper);
}
@Override
public boolean updateParticipantCount(String id, Integer increment) {
LambdaUpdateWrapper<GrowthTopic> wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(GrowthTopic::getId, id)
.setSql("participant_count = participant_count + " + increment)
.set(GrowthTopic::getUpdateTime, LocalDateTime.now());
return this.update(wrapper);
}
@Override
public boolean updateStatus(String id, String status) {
LambdaUpdateWrapper<GrowthTopic> wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(GrowthTopic::getId, id)
.set(GrowthTopic::getStatus, status)
.set(GrowthTopic::getUpdateTime, LocalDateTime.now());
return this.update(wrapper);
}
@Override
public List<GrowthTopic> getEndingSoonTopics(Integer days) {
LocalDateTime endTime = LocalDateTime.now().plusDays(days);
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.le(GrowthTopic::getEndTime, endTime)
.eq(GrowthTopic::getStatus, "active")
.eq(GrowthTopic::getIsDeleted, 0)
.orderByAsc(GrowthTopic::getEndTime);
return this.list(wrapper);
}
@Override
public List<GrowthTopic> getLongTermTopics() {
LambdaQueryWrapper<GrowthTopic> wrapper = new LambdaQueryWrapper<>();
wrapper.isNull(GrowthTopic::getEndTime)
.eq(GrowthTopic::getIsDeleted, 0)
.orderByDesc(GrowthTopic::getCreateTime);
return this.list(wrapper);
}
@Override
public GrowthTopic createGrowthTopic(String title, String description, String category,
String difficultyLevel, String tags, LocalDateTime endTime) {
GrowthTopic topic = GrowthTopic.builder()
.id(UUID.randomUUID().toString())
.title(title)
.description(description)
.category(category)
.difficultyLevel(difficultyLevel)
.tags(tags)
.endTime(endTime)
.status("active")
.participantCount(0)
.build();
this.save(topic);
return topic;
}
}