|
|
@@ -7,6 +7,7 @@ import com.zhentao.entity.CommentLikes;
|
|
|
import com.zhentao.entity.DynamicComments;
|
|
|
import com.zhentao.mapper.CommentLikesMapper;
|
|
|
import com.zhentao.mapper.DynamicCommentsMapper;
|
|
|
+import com.zhentao.mapper.UserDynamicsMapper;
|
|
|
import com.zhentao.service.CommentService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
|
@@ -27,6 +28,8 @@ public class CommentServiceImpl implements CommentService {
|
|
|
private DynamicCommentsMapper commentsMapper;
|
|
|
@Autowired
|
|
|
private CommentLikesMapper commentLikesMapper;
|
|
|
+ @Autowired
|
|
|
+ private UserDynamicsMapper userDynamicsMapper;
|
|
|
|
|
|
private static final Set<String> SENSITIVE = new HashSet<>(Arrays.asList("傻", "坏词", "违规"));
|
|
|
|
|
|
@@ -53,6 +56,12 @@ public class CommentServiceImpl implements CommentService {
|
|
|
c.setCreatedAt(LocalDateTime.now());
|
|
|
commentsMapper.insert(c);
|
|
|
|
|
|
+ // 更新动态的评论数
|
|
|
+ userDynamicsMapper.update(null,
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<com.zhentao.entity.UserDynamics>()
|
|
|
+ .eq("dynamic_id", dynamicId)
|
|
|
+ .setSql("comment_count = comment_count + 1"));
|
|
|
+
|
|
|
// 如果是回复,给父评论回复数+1
|
|
|
if (c.getParentCommentId() != null && c.getParentCommentId() > 0) {
|
|
|
commentsMapper.update(null,
|
|
|
@@ -69,6 +78,21 @@ public class CommentServiceImpl implements CommentService {
|
|
|
.eq("dynamic_id", dynamicId)
|
|
|
.eq("status", 1)
|
|
|
.orderByDesc("created_at"));
|
|
|
+
|
|
|
+ // 同步更新动态的评论数(统计实际评论总数,确保数据一致性)
|
|
|
+ // 统计所有评论(包括回复)
|
|
|
+ Long actualCommentCount = commentsMapper.selectCount(new QueryWrapper<DynamicComments>()
|
|
|
+ .eq("dynamic_id", dynamicId)
|
|
|
+ .eq("status", 1));
|
|
|
+
|
|
|
+ // 更新动态表的评论数(如果实际数量与数据库不一致)
|
|
|
+ if (actualCommentCount != null) {
|
|
|
+ userDynamicsMapper.update(null,
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<com.zhentao.entity.UserDynamics>()
|
|
|
+ .eq("dynamic_id", dynamicId)
|
|
|
+ .set("comment_count", actualCommentCount.intValue()));
|
|
|
+ }
|
|
|
+
|
|
|
return new PageResult<>(pg.getRecords(), pg.getTotal(), pg.getCurrent(), pg.getSize());
|
|
|
}
|
|
|
|