CommentServiceImpl.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package nju.seec.helper.service.impl;
  2. import nju.seec.helper.dao.CommentDAO;
  3. import nju.seec.helper.dao.SlideDAO;
  4. import nju.seec.helper.dto.CommentDTO;
  5. import nju.seec.helper.dto.LoginUser;
  6. import nju.seec.helper.entity.Comment;
  7. import nju.seec.helper.entity.Slide;
  8. import nju.seec.helper.service.CommentService;
  9. import nju.seec.helper.service.MessageService;
  10. import nju.seec.helper.service.util.AuthUtils;
  11. import nju.seec.helper.util.enums.MessageType;
  12. import nju.seec.helper.vo.CommentVO;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.data.domain.Pageable;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import java.util.Collections;
  18. /**
  19. * @author cst
  20. */
  21. @Service
  22. public class CommentServiceImpl implements CommentService {
  23. private final CommentDAO commentDAO;
  24. private final SlideDAO slideDAO;
  25. private final MessageService messageService;
  26. public CommentServiceImpl(CommentDAO commentDAO, SlideDAO slideDAO, MessageService messageService) {
  27. this.commentDAO = commentDAO;
  28. this.slideDAO = slideDAO;
  29. this.messageService = messageService;
  30. }
  31. @Transactional(rollbackFor = Exception.class)
  32. @Override
  33. public CommentVO createComment(LoginUser user, CommentDTO commentDTO) {
  34. Comment comment = new Comment()
  35. .setSlideId(commentDTO.getSlideId())
  36. .setPageNumber(commentDTO.getPageNumber())
  37. .setUserId(user.getId())
  38. .setUserName(user.getName())
  39. .setUserType(user.getType())
  40. .setTitle(commentDTO.getTitle())
  41. .setContent(commentDTO.getContent());
  42. comment = commentDAO.save(comment);
  43. // 增加通知
  44. Slide slide = slideDAO.findSlideById(comment.getSlideId());
  45. if (!user.getId().equals(slide.getTeacherId())) {
  46. messageService.createMessage(Collections.singleton(slide.getTeacherId()), MessageType.COMMENT_NEW, String.format("课程 [%s] 的课件 [%s] 在第%d页有新评论", slide.getCourseName(), slide.getName(), comment.getPageNumber()), slide.getId());
  47. }
  48. return new CommentVO(comment);
  49. }
  50. @Transactional(rollbackFor = Exception.class)
  51. @Override
  52. public void removeComment(LoginUser user, Long commentId) {
  53. Comment comment = commentDAO.findCommentById(commentId);
  54. AuthUtils.checkDataAuth(user.getId(), comment.getUserId(), "您无权删除该评论");
  55. unTopComment(comment);
  56. commentDAO.delete(comment);
  57. }
  58. @Transactional(rollbackFor = Exception.class)
  59. @Override
  60. public void topComment(LoginUser user, Long commentId) {
  61. Comment comment = commentDAO.findCommentById(commentId);
  62. AuthUtils.checkDataAuth(user.getId(), slideDAO.findSlideById(comment.getSlideId()).getTeacherId(), "您无权在该评论区置顶评论");
  63. unTopComment(comment);
  64. comment.setTopNumber(commentDAO.countTopComments(comment.getSlideId(), comment.getPageNumber()) + 1);
  65. commentDAO.save(comment);
  66. }
  67. @Transactional(rollbackFor = Exception.class)
  68. @Override
  69. public void unTopComment(LoginUser user, Long commentId) {
  70. Comment comment = commentDAO.findCommentById(commentId);
  71. AuthUtils.checkDataAuth(user.getId(), slideDAO.findSlideById(comment.getSlideId()).getTeacherId(), "您无权在该评论区取消置顶评论");
  72. unTopComment(comment);
  73. commentDAO.save(comment);
  74. }
  75. private void unTopComment(Comment comment) {
  76. Integer topNumber = comment.getTopNumber();
  77. if (topNumber != null) {
  78. comment.setTopNumber(null);
  79. commentDAO.decTopNumber(comment.getSlideId(), comment.getPageNumber(), topNumber);
  80. }
  81. }
  82. @Transactional(readOnly = true)
  83. @Override
  84. public Page<CommentVO> getCommentsBySlideAndPageNumber(Long slideId, Integer pageNumber, Pageable pageable) {
  85. return commentDAO.findBySlideIdAndPageNumber(slideId, pageNumber, pageable).map(CommentVO::new);
  86. }
  87. @Transactional(readOnly = true)
  88. @Override
  89. public CommentVO getOneComment(Long commentId) {
  90. return new CommentVO(commentDAO.findCommentById(commentId));
  91. }
  92. }