package nju.seec.helper.service.impl; import nju.seec.helper.dao.ChooseDAO; import nju.seec.helper.dao.CourseDAO; import nju.seec.helper.dao.NoticeDAO; import nju.seec.helper.dto.notice.NoticeDTO; import nju.seec.helper.dto.user.LoginUser; import nju.seec.helper.entity.Course; import nju.seec.helper.entity.Notice; import nju.seec.helper.service.MessageService; import nju.seec.helper.service.NoticeService; import nju.seec.helper.service.util.AuthUtils; import nju.seec.helper.service.util.StringUtils; import nju.seec.helper.util.enums.MessageType; import nju.seec.helper.vo.NoticeVO; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Set; /** * @author cst */ @Service public class NoticeServiceImpl implements NoticeService { private final CourseDAO courseDAO; private final ChooseDAO chooseDAO; private final NoticeDAO noticeDAO; private final MessageService messageService; public NoticeServiceImpl(CourseDAO courseDAO, ChooseDAO chooseDAO, NoticeDAO noticeDAO, MessageService messageService) { this.courseDAO = courseDAO; this.chooseDAO = chooseDAO; this.noticeDAO = noticeDAO; this.messageService = messageService; } @Transactional(rollbackFor = Exception.class) @Override public NoticeVO createNotice(LoginUser user, NoticeDTO noticeDTO) { Course course = courseDAO.findCourseById(noticeDTO.getCourseId()); AuthUtils.checkDataAuth(user.getId(), course.getTeacher().getId(), "您无权创建该课程的公告"); Notice notice = new Notice() .setCourseId(noticeDTO.getCourseId()) .setTeacherId(user.getId()) .setTitle(noticeDTO.getTitle()) .setContent(noticeDTO.getContent()); Set studentIds = chooseDAO.findStudentIdsByCourseId(noticeDTO.getCourseId()); messageService.createMessage(studentIds, MessageType.NOTICE_NEW, String.format("[%s] 发布了新公告", course.getName()), null); return new NoticeVO(noticeDAO.save(notice)); } @Transactional(rollbackFor = Exception.class) @Override public NoticeVO modifyNotice(LoginUser user, Long noticeId, NoticeDTO noticeDTO) { Notice notice = noticeDAO.findNoticeById(noticeId); AuthUtils.checkDataAuth(user.getId(), notice.getTeacherId(), "您无权修改该公告"); notice.setTitle(noticeDTO.getTitle()) .setContent(noticeDTO.getContent()); return new NoticeVO(noticeDAO.save(notice)); } @Transactional(readOnly = true) @Override public Page getNoticesByCourse(LoginUser user, Long courseId, String key, Pageable pageable) { return findByCourseIdAndKey(courseId, key, pageable).map(NoticeVO::new); } @Transactional(rollbackFor = Exception.class) @Override public void deleteNotice(LoginUser user, Long noticeId) { Notice notice = noticeDAO.findNoticeById(noticeId); AuthUtils.checkDataAuth(user.getId(), notice.getTeacherId(), "您无权删除该公告"); noticeDAO.deleteById(noticeId); } @Transactional(readOnly = true) @Override public NoticeVO getOneNotice(LoginUser user, Long noticeId) { return new NoticeVO(noticeDAO.findNoticeById(noticeId)); } private Page findByCourseIdAndKey(Long courseId, String key, Pageable pageable) { String keyPattern = StringUtils.keyPattern(key); return noticeDAO.findAll( (Specification) (root, query, cb) -> cb.and( cb.equal(root.get("courseId"), courseId) // , cb.or(cb.like(root.get("title"), keyPattern), cb.like(root.get("content"), keyPattern)) ), pageable); } }