package nju.seec.helper.dao; import nju.seec.helper.entity.Slide; import nju.seec.helper.util.enums.ExceptionType; import nju.seec.helper.util.enums.SlideState; import nju.seec.helper.util.exception.HelperException; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import java.util.Set; /** * @author cst */ @Repository public interface SlideDAO extends JpaRepository { /** * 封装findById * * @param id * @return */ default Slide findSlideById(Long id) { return this.findById(id) .filter(slide -> slide.getDeleteAt() == 0) .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "找不到课件")); } /** * 查询教师创建的课件 * * @param teacherId * @param key * @param pageable * @return */ @Query("select s from Slide s " + "where s.teacherId=?1 " + "and s.name like concat('%',?2,'%') " + "and s.deleteAt=0") Page findByTeacherIdAndNameContains(Long teacherId, String key, Pageable pageable); /** * 查询教师创建的该课程的课件 * * @param courseId * @param teacherId * @param key * @param pageable * @return */ @Query("select s from Slide s " + "where s.courseId=?1 " + "and s.teacherId=?2 " + "and s.name like concat('%',?3,'%') " + "and s.deleteAt=0") Page findByCourseIdAndTeacherIdAndNameContains(Long courseId, Long teacherId, String key, Pageable pageable); /** * 根据课程和课件状态查询 * * @param courseId * @param exclusiveStates * @param key * @param pageable * @return */ @Query("select s from Slide s " + "where s.courseId=?1 " + "and s.state not in ?2 " + "and s.name like concat('%',?3,'%') " + "and s.deleteAt=0") Page findByCourseIdAndStateNotInAndNameContains(Long courseId, Set exclusiveStates, String key, Pageable pageable); /** * 更新所有人信息 * * @param userId * @param userName */ @Transactional(rollbackFor = Exception.class) @Modifying @Query("update Slide s " + "set s.teacherName=?2 " + "where s.teacherId=?1 ") void updateUser(Long userId, String userName); /** * 更新课程信息 * * @param courseId * @param courseName */ @Transactional(rollbackFor = Exception.class) @Modifying @Query("update Slide s " + "set s.courseName=?2 " + "where s.courseId=?1 ") void updateCourse(Long courseId, String courseName); /** * 检查同名课件 * * @param slideId * @param courseId * @param name * @return */ @Query("select case when count(s.id)>0 then true else false end " + "from Slide s " + "where s.id<>?1 " + "and s.courseId=?2 " + "and s.name=?3 " + "and s.deleteAt=0 ") boolean existsByCourseIdAndName(Long slideId, Long courseId, String name); }