NoticeServiceImpl.java 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package nju.seec.helper.service.impl;
  2. import nju.seec.helper.dao.ChooseDAO;
  3. import nju.seec.helper.dao.CourseDAO;
  4. import nju.seec.helper.dao.NoticeDAO;
  5. import nju.seec.helper.dto.notice.NoticeDTO;
  6. import nju.seec.helper.dto.user.LoginUser;
  7. import nju.seec.helper.entity.Course;
  8. import nju.seec.helper.entity.Notice;
  9. import nju.seec.helper.service.MessageService;
  10. import nju.seec.helper.service.NoticeService;
  11. import nju.seec.helper.service.util.AuthUtils;
  12. import nju.seec.helper.service.util.StringUtils;
  13. import nju.seec.helper.util.enums.MessageType;
  14. import nju.seec.helper.vo.NoticeVO;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.data.domain.Pageable;
  17. import org.springframework.data.jpa.domain.Specification;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import java.util.Set;
  21. /**
  22. * @author cst
  23. */
  24. @Service
  25. public class NoticeServiceImpl implements NoticeService {
  26. private final CourseDAO courseDAO;
  27. private final ChooseDAO chooseDAO;
  28. private final NoticeDAO noticeDAO;
  29. private final MessageService messageService;
  30. public NoticeServiceImpl(CourseDAO courseDAO, ChooseDAO chooseDAO, NoticeDAO noticeDAO, MessageService messageService) {
  31. this.courseDAO = courseDAO;
  32. this.chooseDAO = chooseDAO;
  33. this.noticeDAO = noticeDAO;
  34. this.messageService = messageService;
  35. }
  36. @Transactional(rollbackFor = Exception.class)
  37. @Override
  38. public NoticeVO createNotice(LoginUser user, NoticeDTO noticeDTO) {
  39. Course course = courseDAO.findCourseById(noticeDTO.getCourseId());
  40. AuthUtils.checkDataAuth(user.getId(), course.getTeacher().getId(), "您无权创建该课程的公告");
  41. Notice notice = new Notice()
  42. .setCourseId(noticeDTO.getCourseId())
  43. .setTeacherId(user.getId())
  44. .setTitle(noticeDTO.getTitle())
  45. .setContent(noticeDTO.getContent());
  46. Set<Long> studentIds = chooseDAO.findStudentIdsByCourseId(noticeDTO.getCourseId());
  47. messageService.createMessage(studentIds, MessageType.NOTICE_NEW, String.format("[%s] 发布了新公告", course.getName()), null);
  48. return new NoticeVO(noticeDAO.save(notice));
  49. }
  50. @Transactional(rollbackFor = Exception.class)
  51. @Override
  52. public NoticeVO modifyNotice(LoginUser user, Long noticeId, NoticeDTO noticeDTO) {
  53. Notice notice = noticeDAO.findNoticeById(noticeId);
  54. AuthUtils.checkDataAuth(user.getId(), notice.getTeacherId(), "您无权修改该公告");
  55. notice.setTitle(noticeDTO.getTitle())
  56. .setContent(noticeDTO.getContent());
  57. return new NoticeVO(noticeDAO.save(notice));
  58. }
  59. @Transactional(readOnly = true)
  60. @Override
  61. public Page<NoticeVO> getNoticesByCourse(LoginUser user, Long courseId, String key, Pageable pageable) {
  62. return findByCourseIdAndKey(courseId, key, pageable).map(NoticeVO::new);
  63. }
  64. @Transactional(rollbackFor = Exception.class)
  65. @Override
  66. public void deleteNotice(LoginUser user, Long noticeId) {
  67. Notice notice = noticeDAO.findNoticeById(noticeId);
  68. AuthUtils.checkDataAuth(user.getId(), notice.getTeacherId(), "您无权删除该公告");
  69. noticeDAO.deleteById(noticeId);
  70. }
  71. @Transactional(readOnly = true)
  72. @Override
  73. public NoticeVO getOneNotice(LoginUser user, Long noticeId) {
  74. return new NoticeVO(noticeDAO.findNoticeById(noticeId));
  75. }
  76. private Page<Notice> findByCourseIdAndKey(Long courseId, String key, Pageable pageable) {
  77. String keyPattern = StringUtils.keyPattern(key);
  78. return noticeDAO.findAll(
  79. (Specification<Notice>) (root, query, cb) -> cb.and(
  80. cb.equal(root.get("courseId"), courseId)
  81. // , cb.or(cb.like(root.get("title"), keyPattern), cb.like(root.get("content"), keyPattern))
  82. ),
  83. pageable);
  84. }
  85. }