1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
@Service @Transactional public class CommentServiceImpl implements CommentService {
@Autowired private CommentRepository commentRepository;
@Autowired private CommentLikeRepository commentLikeRepository;
@Autowired private RedisTemplate<String, Object> redisTemplate;
@Autowired private RabbitTemplate rabbitTemplate;
@Autowired private UserService userService;
@Autowired private ContentService contentService;
private final String COMMENT_CACHE_PREFIX = "comment:"; private final String COMMENT_LIKE_CACHE_PREFIX = "comment_like:"; private final long COMMENT_CACHE_EXPIRE = 1800;
@Override public CommentResult publishComment(CommentRequest request) { try { validateCommentRequest(request);
checkUserPermission(request.getUserId());
checkContentExists(request.getContentId());
Comment comment = createCommentEntity(request);
comment = commentRepository.save(comment);
updateCommentCache(comment);
sendCommentNotification(comment);
CommentResult result = new CommentResult(); result.setSuccess(true); result.setCommentId(comment.getId()); result.setMessage("评论发布成功");
return result;
} catch (Exception e) { log.error("发布评论失败", e); CommentResult result = new CommentResult(); result.setSuccess(false); result.setMessage("评论发布失败: " + e.getMessage()); return result; } }
private void validateCommentRequest(CommentRequest request) { if (request.getContentId() == null) { throw new IllegalArgumentException("内容ID不能为空"); }
if (request.getUserId() == null) { throw new IllegalArgumentException("用户ID不能为空"); }
if (request.getContent() == null || request.getContent().trim().isEmpty()) { throw new IllegalArgumentException("评论内容不能为空"); }
if (request.getContent().length() > 2000) { throw new IllegalArgumentException("评论内容不能超过2000字符"); } }
private void checkUserPermission(Long userId) { try { UserInfo userInfo = userService.getUserInfo(userId); if (userInfo == null) { throw new IllegalArgumentException("用户不存在"); }
if (userInfo.getStatus() != UserStatus.ACTIVE) { throw new IllegalArgumentException("用户状态异常"); }
} catch (Exception e) { log.error("检查用户权限失败", e); throw new IllegalArgumentException("用户权限验证失败"); } }
private void checkContentExists(Long contentId) { try { ContentInfo contentInfo = contentService.getContentInfo(contentId); if (contentInfo == null) { throw new IllegalArgumentException("内容不存在"); }
if (contentInfo.getStatus() != ContentStatus.PUBLISHED) { throw new IllegalArgumentException("内容状态异常"); }
} catch (Exception e) { log.error("检查内容是否存在失败", e); throw new IllegalArgumentException("内容验证失败"); } }
private Comment createCommentEntity(CommentRequest request) { Comment comment = new Comment(); comment.setContentId(request.getContentId()); comment.setUserId(request.getUserId()); comment.setParentId(request.getParentId()); comment.setContent(request.getContent()); comment.setStatus(CommentStatus.PENDING); comment.setIpAddress(request.getIpAddress()); comment.setUserAgent(request.getUserAgent());
return comment; }
private void updateCommentCache(Comment comment) { try { String cacheKey = COMMENT_CACHE_PREFIX + comment.getId(); redisTemplate.opsForValue().set(cacheKey, comment, Duration.ofSeconds(COMMENT_CACHE_EXPIRE));
} catch (Exception e) { log.error("更新评论缓存失败", e); } }
private void sendCommentNotification(Comment comment) { try { CommentNotificationEvent event = new CommentNotificationEvent(); event.setCommentId(comment.getId()); event.setContentId(comment.getContentId()); event.setUserId(comment.getUserId()); event.setEventType("COMMENT_PUBLISHED"); event.setTimestamp(new Date());
rabbitTemplate.convertAndSend("comment.notification.queue", event);
} catch (Exception e) { log.error("发送评论通知失败", e); } }
@Override public CommentListResult getCommentList(CommentListRequest request) { try { validateCommentListRequest(request);
String cacheKey = COMMENT_CACHE_PREFIX + "list:" + request.getContentId() + ":" + request.getPage() + ":" + request.getSize(); CommentListResult cachedResult = (CommentListResult) redisTemplate.opsForValue().get(cacheKey);
if (cachedResult != null) { return cachedResult; }
Page<Comment> commentPage = commentRepository.findByContentIdAndStatusAndIsDeleted( request.getContentId(), CommentStatus.APPROVED, false, PageRequest.of(request.getPage(), request.getSize(), Sort.by(Sort.Direction.DESC, "createdTime")));
CommentListResult result = new CommentListResult(); result.setComments(commentPage.getContent()); result.setTotalCount(commentPage.getTotalElements()); result.setPage(request.getPage()); result.setSize(request.getSize()); result.setTotalPages(commentPage.getTotalPages());
redisTemplate.opsForValue().set(cacheKey, result, Duration.ofSeconds(COMMENT_CACHE_EXPIRE));
return result;
} catch (Exception e) { log.error("查询评论列表失败", e); CommentListResult result = new CommentListResult(); result.setComments(new ArrayList<>()); result.setTotalCount(0L); result.setPage(request.getPage()); result.setSize(request.getSize()); result.setTotalPages(0); return result; } }
private void validateCommentListRequest(CommentListRequest request) { if (request.getContentId() == null) { throw new IllegalArgumentException("内容ID不能为空"); }
if (request.getPage() < 0) { throw new IllegalArgumentException("页码不能小于0"); }
if (request.getSize() <= 0 || request.getSize() > 100) { throw new IllegalArgumentException("每页大小必须在1-100之间"); } }
@Override public CommentDetailResult getCommentDetail(Long commentId) { try { String cacheKey = COMMENT_CACHE_PREFIX + commentId; Comment comment = (Comment) redisTemplate.opsForValue().get(cacheKey);
if (comment == null) { comment = commentRepository.findById(commentId).orElse(null); if (comment == null) { throw new IllegalArgumentException("评论不存在"); }
redisTemplate.opsForValue().set(cacheKey, comment, Duration.ofSeconds(COMMENT_CACHE_EXPIRE)); }
CommentDetailResult result = new CommentDetailResult(); result.setComment(comment); result.setSuccess(true);
return result;
} catch (Exception e) { log.error("查询评论详情失败", e); CommentDetailResult result = new CommentDetailResult(); result.setSuccess(false); result.setMessage("查询评论详情失败: " + e.getMessage()); return result; } }
@Override public CommentResult deleteComment(Long commentId, Long userId) { try { Comment comment = commentRepository.findById(commentId).orElse(null); if (comment == null) { throw new IllegalArgumentException("评论不存在"); }
if (!comment.getUserId().equals(userId)) { throw new IllegalArgumentException("无权限删除此评论"); }
comment.setIsDeleted(true); comment.setUpdatedTime(LocalDateTime.now()); commentRepository.save(comment);
clearCommentCache(commentId);
CommentResult result = new CommentResult(); result.setSuccess(true); result.setMessage("评论删除成功");
return result;
} catch (Exception e) { log.error("删除评论失败", e); CommentResult result = new CommentResult(); result.setSuccess(false); result.setMessage("删除评论失败: " + e.getMessage()); return result; } }
@Override public CommentResult likeComment(Long commentId, Long userId) { try { Comment comment = commentRepository.findById(commentId).orElse(null); if (comment == null) { throw new IllegalArgumentException("评论不存在"); }
String likeKey = COMMENT_LIKE_CACHE_PREFIX + commentId + ":" + userId; Boolean isLiked = (Boolean) redisTemplate.opsForValue().get(likeKey);
if (isLiked != null && isLiked) { throw new IllegalArgumentException("已经点赞过此评论"); }
CommentLike commentLike = new CommentLike(); commentLike.setCommentId(commentId); commentLike.setUserId(userId); commentLike.setIsLiked(true); commentLikeRepository.save(commentLike);
comment.setLikeCount(comment.getLikeCount() + 1); comment.setUpdatedTime(LocalDateTime.now()); commentRepository.save(comment);
redisTemplate.opsForValue().set(likeKey, true, Duration.ofHours(24)); updateCommentCache(comment);
CommentResult result = new CommentResult(); result.setSuccess(true); result.setMessage("点赞成功");
return result;
} catch (Exception e) { log.error("点赞评论失败", e); CommentResult result = new CommentResult(); result.setSuccess(false); result.setMessage("点赞失败: " + e.getMessage()); return result; } }
@Override public CommentResult unlikeComment(Long commentId, Long userId) { try { Comment comment = commentRepository.findById(commentId).orElse(null); if (comment == null) { throw new IllegalArgumentException("评论不存在"); }
CommentLike commentLike = commentLikeRepository.findByCommentIdAndUserId(commentId, userId); if (commentLike == null || !commentLike.getIsLiked()) { throw new IllegalArgumentException("未点赞此评论"); }
commentLikeRepository.delete(commentLike);
comment.setLikeCount(Math.max(0, comment.getLikeCount() - 1)); comment.setUpdatedTime(LocalDateTime.now()); commentRepository.save(comment);
String likeKey = COMMENT_LIKE_CACHE_PREFIX + commentId + ":" + userId; redisTemplate.delete(likeKey); updateCommentCache(comment);
CommentResult result = new CommentResult(); result.setSuccess(true); result.setMessage("取消点赞成功");
return result;
} catch (Exception e) { log.error("取消点赞失败", e); CommentResult result = new CommentResult(); result.setSuccess(false); result.setMessage("取消点赞失败: " + e.getMessage()); return result; } }
@Override public CommentResult reportComment(Long commentId, Long userId, String reason) { try { Comment comment = commentRepository.findById(commentId).orElse(null); if (comment == null) { throw new IllegalArgumentException("评论不存在"); }
comment.setReportCount(comment.getReportCount() + 1); comment.setUpdatedTime(LocalDateTime.now()); commentRepository.save(comment);
updateCommentCache(comment);
sendReportNotification(commentId, userId, reason);
CommentResult result = new CommentResult(); result.setSuccess(true); result.setMessage("举报成功");
return result;
} catch (Exception e) { log.error("举报评论失败", e); CommentResult result = new CommentResult(); result.setSuccess(false); result.setMessage("举报失败: " + e.getMessage()); return result; } }
private void sendReportNotification(Long commentId, Long userId, String reason) { try { CommentReportEvent event = new CommentReportEvent(); event.setCommentId(commentId); event.setUserId(userId); event.setReason(reason); event.setEventType("COMMENT_REPORTED"); event.setTimestamp(new Date());
rabbitTemplate.convertAndSend("comment.report.queue", event);
} catch (Exception e) { log.error("发送举报通知失败", e); } }
@Override public CommentResult auditComment(Long commentId, CommentStatus status, String reason) { try { Comment comment = commentRepository.findById(commentId).orElse(null); if (comment == null) { throw new IllegalArgumentException("评论不存在"); }
comment.setStatus(status); comment.setUpdatedTime(LocalDateTime.now()); commentRepository.save(comment);
updateCommentCache(comment);
sendAuditNotification(commentId, status, reason);
CommentResult result = new CommentResult(); result.setSuccess(true); result.setMessage("审核成功");
return result;
} catch (Exception e) { log.error("审核评论失败", e); CommentResult result = new CommentResult(); result.setSuccess(false); result.setMessage("审核失败: " + e.getMessage()); return result; } }
private void sendAuditNotification(Long commentId, CommentStatus status, String reason) { try { CommentAuditEvent event = new CommentAuditEvent(); event.setCommentId(commentId); event.setStatus(status); event.setReason(reason); event.setEventType("COMMENT_AUDITED"); event.setTimestamp(new Date());
rabbitTemplate.convertAndSend("comment.audit.queue", event);
} catch (Exception e) { log.error("发送审核通知失败", e); } }
@Override public CommentStatisticsResult getCommentStatistics(Long contentId) { try { String cacheKey = COMMENT_CACHE_PREFIX + "statistics:" + contentId; CommentStatisticsResult cachedResult = (CommentStatisticsResult) redisTemplate.opsForValue().get(cacheKey);
if (cachedResult != null) { return cachedResult; }
Long totalComments = commentRepository.countByContentIdAndStatusAndIsDeleted(contentId, CommentStatus.APPROVED, false); Long totalLikes = commentRepository.sumLikeCountByContentIdAndStatusAndIsDeleted(contentId, CommentStatus.APPROVED, false); Long totalReplies = commentRepository.sumReplyCountByContentIdAndStatusAndIsDeleted(contentId, CommentStatus.APPROVED, false);
CommentStatisticsResult result = new CommentStatisticsResult(); result.setContentId(contentId); result.setTotalComments(totalComments); result.setTotalLikes(totalLikes); result.setTotalReplies(totalReplies);
redisTemplate.opsForValue().set(cacheKey, result, Duration.ofSeconds(COMMENT_CACHE_EXPIRE));
return result;
} catch (Exception e) { log.error("获取评论统计失败", e); CommentStatisticsResult result = new CommentStatisticsResult(); result.setContentId(contentId); result.setTotalComments(0L); result.setTotalLikes(0L); result.setTotalReplies(0L); return result; } }
private void clearCommentCache(Long commentId) { try { String cacheKey = COMMENT_CACHE_PREFIX + commentId; redisTemplate.delete(cacheKey);
} catch (Exception e) { log.error("清除评论缓存失败", e); } } }
|