优化
This commit is contained in:
+113
-230
@@ -1,19 +1,21 @@
|
||||
package com.emotion.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
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.common.PageResult;
|
||||
import com.emotion.dto.request.TopicInteractionCreateRequest;
|
||||
import com.emotion.dto.request.TopicInteractionPageRequest;
|
||||
import com.emotion.dto.request.TopicInteractionUpdateRequest;
|
||||
import com.emotion.dto.response.TopicInteractionResponse;
|
||||
import com.emotion.entity.TopicInteraction;
|
||||
import com.emotion.mapper.TopicInteractionMapper;
|
||||
import com.emotion.service.TopicInteractionService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 话题互动服务实现类
|
||||
@@ -24,257 +26,138 @@ import java.util.Collections;
|
||||
@Service
|
||||
public class TopicInteractionServiceImpl extends ServiceImpl<TopicInteractionMapper, TopicInteraction> implements TopicInteractionService {
|
||||
|
||||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@Override
|
||||
public IPage<TopicInteraction> getPage(BasePageRequest request) {
|
||||
public PageResult<TopicInteractionResponse> getPageWithResponse(TopicInteractionPageRequest request) {
|
||||
Page<TopicInteraction> page = new Page<>(request.getCurrent(), request.getSize());
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
// 根据关键词查询
|
||||
if (StringUtils.hasText(request.getKeyword())) {
|
||||
wrapper.and(w -> w.like(TopicInteraction::getContent, request.getKeyword())
|
||||
.or().like(TopicInteraction::getUserInput, request.getKeyword())
|
||||
.or().like(TopicInteraction::getAiResponse, request.getKeyword()));
|
||||
}
|
||||
|
||||
// 根据话题ID查询
|
||||
if (StringUtils.hasText(request.getTopicId())) {
|
||||
wrapper.eq(TopicInteraction::getTopicId, request.getTopicId());
|
||||
}
|
||||
|
||||
// 根据用户ID查询
|
||||
if (StringUtils.hasText(request.getUserId())) {
|
||||
wrapper.eq(TopicInteraction::getCreateBy, request.getUserId());
|
||||
}
|
||||
|
||||
// 根据互动类型查询
|
||||
if (StringUtils.hasText(request.getInteractionType())) {
|
||||
wrapper.eq(TopicInteraction::getType, request.getInteractionType());
|
||||
}
|
||||
|
||||
wrapper.eq(TopicInteraction::getIsDeleted, 0).orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.page(page, wrapper);
|
||||
Page<TopicInteraction> resultPage = this.page(page, wrapper);
|
||||
|
||||
// 转换为响应对象
|
||||
PageResult<TopicInteractionResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(resultPage.getCurrent());
|
||||
pageResult.setSize(resultPage.getSize());
|
||||
pageResult.setTotal(resultPage.getTotal());
|
||||
pageResult.setPages(resultPage.getPages());
|
||||
pageResult.setRecords(resultPage.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.toList());
|
||||
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPage<TopicInteraction> getPageByTopicId(BasePageRequest request, String topicId) {
|
||||
Page<TopicInteraction> page = new Page<>(request.getCurrent(), request.getSize());
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.page(page, wrapper);
|
||||
public TopicInteractionResponse getTopicInteractionResponseById(String id) {
|
||||
TopicInteraction topicInteraction = this.getById(id);
|
||||
if (topicInteraction == null || topicInteraction.getIsDeleted() == 1) {
|
||||
return null;
|
||||
}
|
||||
return convertToResponse(topicInteraction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPage<TopicInteraction> getPageByUserId(BasePageRequest request, String userId) {
|
||||
Page<TopicInteraction> page = new Page<>(request.getCurrent(), request.getSize());
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.page(page, wrapper);
|
||||
public TopicInteractionResponse createTopicInteractionWithResponse(TopicInteractionCreateRequest request) {
|
||||
TopicInteraction topicInteraction = new TopicInteraction();
|
||||
BeanUtils.copyProperties(request, topicInteraction);
|
||||
// 设置用户ID为当前登录用户
|
||||
topicInteraction.setCreateBy(getCurrentUserId());
|
||||
this.save(topicInteraction);
|
||||
return convertToResponse(topicInteraction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getByTopicId(String topicId) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
public TopicInteractionResponse updateTopicInteractionWithResponse(TopicInteractionUpdateRequest request) {
|
||||
TopicInteraction topicInteraction = this.getById(request.getId());
|
||||
if (topicInteraction == null || topicInteraction.getIsDeleted() == 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 只更新非空字段
|
||||
if (StringUtils.hasText(request.getTopicId())) {
|
||||
topicInteraction.setTopicId(request.getTopicId());
|
||||
}
|
||||
if (StringUtils.hasText(request.getInteractionType())) {
|
||||
topicInteraction.setType(request.getInteractionType());
|
||||
}
|
||||
if (StringUtils.hasText(request.getContent())) {
|
||||
topicInteraction.setContent(request.getContent());
|
||||
}
|
||||
if (StringUtils.hasText(request.getUserInput())) {
|
||||
topicInteraction.setUserInput(request.getUserInput());
|
||||
}
|
||||
if (StringUtils.hasText(request.getAiResponse())) {
|
||||
topicInteraction.setAiResponse(request.getAiResponse());
|
||||
}
|
||||
if (request.getRating() != null) {
|
||||
topicInteraction.setRating(request.getRating());
|
||||
}
|
||||
if (StringUtils.hasText(request.getFeedback())) {
|
||||
topicInteraction.setFeedback(request.getFeedback());
|
||||
}
|
||||
|
||||
this.updateById(topicInteraction);
|
||||
return convertToResponse(topicInteraction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getByUserId(String userId) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
public boolean deleteTopicInteraction(String id) {
|
||||
TopicInteraction topicInteraction = this.getById(id);
|
||||
if (topicInteraction == null || topicInteraction.getIsDeleted() == 1) {
|
||||
return false;
|
||||
}
|
||||
topicInteraction.setIsDeleted(1);
|
||||
return this.updateById(topicInteraction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getByInteractionType(String interactionType) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getType, interactionType)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
/**
|
||||
* 将实体对象转换为响应对象
|
||||
*/
|
||||
private TopicInteractionResponse convertToResponse(TopicInteraction topicInteraction) {
|
||||
if (topicInteraction == null) {
|
||||
return null;
|
||||
}
|
||||
TopicInteractionResponse response = new TopicInteractionResponse();
|
||||
BeanUtils.copyProperties(topicInteraction, response);
|
||||
response.setId(topicInteraction.getId());
|
||||
if (topicInteraction.getCreateTime() != null) {
|
||||
response.setCreateTime(topicInteraction.getCreateTime().format(FORMATTER));
|
||||
}
|
||||
if (topicInteraction.getUpdateTime() != null) {
|
||||
response.setUpdateTime(topicInteraction.getUpdateTime().format(FORMATTER));
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getByTopicIdAndInteractionType(String topicId, String interactionType) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getType, interactionType)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getByUserIdAndInteractionType(String userId, String interactionType) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getType, interactionType)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getByTopicIdAndUserId(String topicId, String userId) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.between(TopicInteraction::getCreateTime, startTime, endTime)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByTopicId(String topicId) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByUserId(String userId) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByInteractionType(String interactionType) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getType, interactionType)
|
||||
.eq(TopicInteraction::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByTopicIdAndInteractionType(String topicId, String interactionType) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getType, interactionType)
|
||||
.eq(TopicInteraction::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByUserIdAndInteractionType(String userId, String interactionType) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getType, interactionType)
|
||||
.eq(TopicInteraction::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getRecentByTopicId(String topicId, Integer limit) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime)
|
||||
.last("LIMIT " + limit);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getRecentByUserId(String userId, Integer limit) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime)
|
||||
.last("LIMIT " + limit);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getPopularInteractions(Integer limit) {
|
||||
// TopicInteraction实体中没有likes字段,暂时返回最新互动
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime)
|
||||
.last("LIMIT " + limit);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getPopularInteractionsByTopicId(String topicId, Integer limit) {
|
||||
// TopicInteraction实体中没有likes字段,暂时返回最新互动
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime)
|
||||
.last("LIMIT " + limit);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> getByLikesRange(Integer minLikes, Integer maxLikes) {
|
||||
// TopicInteraction实体中没有likes字段,暂时返回空列表
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasUserInteracted(String topicId, String userId) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getIsDeleted, 0);
|
||||
return this.count(wrapper) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopicInteraction getUserInteractionByType(String topicId, String userId, String interactionType) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.eq(TopicInteraction::getCreateBy, userId)
|
||||
.eq(TopicInteraction::getType, interactionType)
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime)
|
||||
.last("LIMIT 1");
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateLikes(String id, Integer increment) {
|
||||
// TopicInteraction实体中没有likes字段,暂时返回false
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> searchByContent(String keyword) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.and(w -> w.like(TopicInteraction::getContent, keyword)
|
||||
.or().like(TopicInteraction::getUserInput, keyword)
|
||||
.or().like(TopicInteraction::getAiResponse, keyword))
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TopicInteraction> searchByTopicIdAndContent(String topicId, String keyword) {
|
||||
LambdaQueryWrapper<TopicInteraction> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(TopicInteraction::getTopicId, topicId)
|
||||
.and(w -> w.like(TopicInteraction::getContent, keyword)
|
||||
.or().like(TopicInteraction::getUserInput, keyword)
|
||||
.or().like(TopicInteraction::getAiResponse, keyword))
|
||||
.eq(TopicInteraction::getIsDeleted, 0)
|
||||
.orderByDesc(TopicInteraction::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopicInteraction createTopicInteraction(String topicId, String userId, String interactionType,
|
||||
String content, String attachments) {
|
||||
TopicInteraction interaction = new TopicInteraction();
|
||||
interaction.setTopicId(topicId);
|
||||
interaction.setType(interactionType);
|
||||
interaction.setContent(content);
|
||||
interaction.setUserInput(content);
|
||||
interaction.setCreateBy(userId);
|
||||
|
||||
this.save(interaction);
|
||||
return interaction;
|
||||
/**
|
||||
* 获取当前登录用户ID(模拟实现)
|
||||
*/
|
||||
private String getCurrentUserId() {
|
||||
// 实际项目中应从token中获取用户ID
|
||||
return "current_user_id";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user