|
|
|
|
@@ -1,9 +1,12 @@
|
|
|
|
|
package com.yolo.keyborad.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
|
|
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.yolo.keyborad.common.ErrorCode;
|
|
|
|
|
import com.yolo.keyborad.exception.BusinessException;
|
|
|
|
|
import com.yolo.keyborad.mapper.KeyboardAiCompanionCommentMapper;
|
|
|
|
|
import com.yolo.keyborad.model.entity.KeyboardAiCompanionComment;
|
|
|
|
|
import com.yolo.keyborad.model.entity.KeyboardUser;
|
|
|
|
|
@@ -13,10 +16,11 @@ import com.yolo.keyborad.service.KeyboardAiCompanionCommentLikeService;
|
|
|
|
|
import com.yolo.keyborad.service.UserService;
|
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
@@ -29,6 +33,12 @@ import java.util.stream.Collectors;
|
|
|
|
|
@Service
|
|
|
|
|
public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardAiCompanionCommentMapper, KeyboardAiCompanionComment> implements KeyboardAiCompanionCommentService {
|
|
|
|
|
|
|
|
|
|
private static final short ACTIVE_STATUS = 1;
|
|
|
|
|
|
|
|
|
|
private static final short DELETED_STATUS = -1;
|
|
|
|
|
|
|
|
|
|
private static final long MAX_REPLY_PREVIEW_COUNT = 999L;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private UserService userService;
|
|
|
|
|
|
|
|
|
|
@@ -43,19 +53,27 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|
|
|
|
comment.setContent(content);
|
|
|
|
|
comment.setParentId(parentId);
|
|
|
|
|
comment.setRootId(rootId);
|
|
|
|
|
comment.setStatus((short) 1);
|
|
|
|
|
comment.setStatus(ACTIVE_STATUS);
|
|
|
|
|
comment.setLikeCount(0);
|
|
|
|
|
comment.setCreatedAt(new Date());
|
|
|
|
|
this.save(comment);
|
|
|
|
|
return comment.getId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void deleteComment(Long userId, Long commentId) {
|
|
|
|
|
KeyboardAiCompanionComment comment = getActiveComment(commentId);
|
|
|
|
|
validateDeletePermission(userId, comment.getUserId());
|
|
|
|
|
softDeleteComment(commentId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IPage<CommentVO> pageComments(Long companionId, Integer pageNum, Integer pageSize) {
|
|
|
|
|
Page<KeyboardAiCompanionComment> page = new Page<>(pageNum, pageSize);
|
|
|
|
|
LambdaQueryWrapper<KeyboardAiCompanionComment> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
queryWrapper.eq(KeyboardAiCompanionComment::getCompanionId, companionId)
|
|
|
|
|
.eq(KeyboardAiCompanionComment::getStatus, 1)
|
|
|
|
|
.eq(KeyboardAiCompanionComment::getStatus, ACTIVE_STATUS)
|
|
|
|
|
.isNull(KeyboardAiCompanionComment::getParentId)
|
|
|
|
|
.orderByDesc(KeyboardAiCompanionComment::getCreatedAt);
|
|
|
|
|
IPage<KeyboardAiCompanionComment> entityPage = this.page(page, queryWrapper);
|
|
|
|
|
@@ -87,7 +105,7 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|
|
|
|
Page<KeyboardAiCompanionComment> page = new Page<>(pageNum, pageSize);
|
|
|
|
|
LambdaQueryWrapper<KeyboardAiCompanionComment> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
queryWrapper.eq(KeyboardAiCompanionComment::getCompanionId, companionId)
|
|
|
|
|
.eq(KeyboardAiCompanionComment::getStatus, 1)
|
|
|
|
|
.eq(KeyboardAiCompanionComment::getStatus, ACTIVE_STATUS)
|
|
|
|
|
.isNull(KeyboardAiCompanionComment::getParentId)
|
|
|
|
|
.orderByDesc(KeyboardAiCompanionComment::getCreatedAt);
|
|
|
|
|
IPage<KeyboardAiCompanionComment> entityPage = this.page(page, queryWrapper);
|
|
|
|
|
@@ -105,7 +123,7 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|
|
|
|
// 查询所有回复
|
|
|
|
|
LambdaQueryWrapper<KeyboardAiCompanionComment> replyWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
replyWrapper.in(KeyboardAiCompanionComment::getRootId, topCommentIds)
|
|
|
|
|
.eq(KeyboardAiCompanionComment::getStatus, 1)
|
|
|
|
|
.eq(KeyboardAiCompanionComment::getStatus, ACTIVE_STATUS)
|
|
|
|
|
.orderByAsc(KeyboardAiCompanionComment::getCreatedAt);
|
|
|
|
|
allReplies = this.list(replyWrapper);
|
|
|
|
|
|
|
|
|
|
@@ -121,7 +139,7 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|
|
|
|
repliesMap = repliesMap.entrySet().stream()
|
|
|
|
|
.collect(Collectors.toMap(
|
|
|
|
|
Map.Entry::getKey,
|
|
|
|
|
e -> e.getValue().stream().limit(999).collect(Collectors.toList())
|
|
|
|
|
e -> e.getValue().stream().limit(MAX_REPLY_PREVIEW_COUNT).collect(Collectors.toList())
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -178,6 +196,35 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private KeyboardAiCompanionComment getActiveComment(Long commentId) {
|
|
|
|
|
KeyboardAiCompanionComment comment = this.getById(commentId);
|
|
|
|
|
if (comment == null || comment.getStatus() == null || comment.getStatus() != ACTIVE_STATUS) {
|
|
|
|
|
throw new BusinessException(ErrorCode.COMMENT_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
return comment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateDeletePermission(Long userId, Long ownerId) {
|
|
|
|
|
if (!userId.equals(ownerId)) {
|
|
|
|
|
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void softDeleteComment(Long commentId) {
|
|
|
|
|
KeyboardAiCompanionComment updateEntity = new KeyboardAiCompanionComment();
|
|
|
|
|
updateEntity.setStatus(DELETED_STATUS);
|
|
|
|
|
updateEntity.setUpdatedAt(new Date());
|
|
|
|
|
|
|
|
|
|
LambdaUpdateWrapper<KeyboardAiCompanionComment> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
|
|
updateWrapper.eq(KeyboardAiCompanionComment::getId, commentId)
|
|
|
|
|
.eq(KeyboardAiCompanionComment::getStatus, ACTIVE_STATUS);
|
|
|
|
|
|
|
|
|
|
boolean updated = this.update(updateEntity, updateWrapper);
|
|
|
|
|
if (!updated) {
|
|
|
|
|
throw new BusinessException(ErrorCode.OPERATION_ERROR, "删除评论失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将评论实体转换为VO
|
|
|
|
|
*/
|
|
|
|
|
@@ -201,6 +248,7 @@ public class KeyboardAiCompanionCommentServiceImpl extends ServiceImpl<KeyboardA
|
|
|
|
|
// 填充用户信息
|
|
|
|
|
KeyboardUser user = userMap.get(entity.getUserId());
|
|
|
|
|
if (user != null) {
|
|
|
|
|
vo.setUserUid(user.getUid());
|
|
|
|
|
vo.setUserName(user.getNickName());
|
|
|
|
|
vo.setUserAvatar(user.getAvatarUrl());
|
|
|
|
|
}
|
|
|
|
|
|