| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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<Long> 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<NoticeVO> 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<Notice> findByCourseIdAndKey(Long courseId, String key, Pageable pageable) {
- String keyPattern = StringUtils.keyPattern(key);
- return noticeDAO.findAll(
- (Specification<Notice>) (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);
- }
- }
|