SlideDAO.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package nju.seec.helper.dao;
  2. import nju.seec.helper.entity.Slide;
  3. import nju.seec.helper.util.enums.ExceptionType;
  4. import nju.seec.helper.util.enums.SlideState;
  5. import nju.seec.helper.util.exception.HelperException;
  6. import org.springframework.data.domain.Page;
  7. import org.springframework.data.domain.Pageable;
  8. import org.springframework.data.jpa.repository.JpaRepository;
  9. import org.springframework.data.jpa.repository.Modifying;
  10. import org.springframework.data.jpa.repository.Query;
  11. import org.springframework.stereotype.Repository;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import java.util.Set;
  14. /**
  15. * @author cst
  16. */
  17. @Repository
  18. public interface SlideDAO extends JpaRepository<Slide, Long> {
  19. /**
  20. * 封装findById
  21. *
  22. * @param id
  23. * @return
  24. */
  25. default Slide findSlideById(Long id) {
  26. return this.findById(id)
  27. .filter(slide -> slide.getDeleteAt() == 0)
  28. .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "找不到课件"));
  29. }
  30. /**
  31. * 查询教师创建的课件
  32. *
  33. * @param teacherId
  34. * @param key
  35. * @param pageable
  36. * @return
  37. */
  38. @Query("select s from Slide s " +
  39. "where s.teacherId=?1 " +
  40. "and s.name like concat('%',?2,'%') " +
  41. "and s.deleteAt=0")
  42. Page<Slide> findByTeacherIdAndNameContains(Long teacherId, String key, Pageable pageable);
  43. /**
  44. * 查询教师创建的该课程的课件
  45. *
  46. * @param courseId
  47. * @param teacherId
  48. * @param key
  49. * @param pageable
  50. * @return
  51. */
  52. @Query("select s from Slide s " +
  53. "where s.courseId=?1 " +
  54. "and s.teacherId=?2 " +
  55. "and s.name like concat('%',?3,'%') " +
  56. "and s.deleteAt=0")
  57. Page<Slide> findByCourseIdAndTeacherIdAndNameContains(Long courseId, Long teacherId, String key, Pageable pageable);
  58. /**
  59. * 根据课程和课件状态查询
  60. *
  61. * @param courseId
  62. * @param exclusiveStates
  63. * @param key
  64. * @param pageable
  65. * @return
  66. */
  67. @Query("select s from Slide s " +
  68. "where s.courseId=?1 " +
  69. "and s.state not in ?2 " +
  70. "and s.name like concat('%',?3,'%') " +
  71. "and s.deleteAt=0")
  72. Page<Slide> findByCourseIdAndStateNotInAndNameContains(Long courseId, Set<SlideState> exclusiveStates, String key, Pageable pageable);
  73. /**
  74. * 更新所有人信息
  75. *
  76. * @param userId
  77. * @param userName
  78. */
  79. @Transactional(rollbackFor = Exception.class)
  80. @Modifying
  81. @Query("update Slide s " +
  82. "set s.teacherName=?2 " +
  83. "where s.teacherId=?1 ")
  84. void updateUser(Long userId, String userName);
  85. /**
  86. * 更新课程信息
  87. *
  88. * @param courseId
  89. * @param courseName
  90. */
  91. @Transactional(rollbackFor = Exception.class)
  92. @Modifying
  93. @Query("update Slide s " +
  94. "set s.courseName=?2 " +
  95. "where s.courseId=?1 ")
  96. void updateCourse(Long courseId, String courseName);
  97. /**
  98. * 检查同名课件
  99. *
  100. * @param slideId
  101. * @param courseId
  102. * @param name
  103. * @return
  104. */
  105. @Query("select case when count(s.id)>0 then true else false end " +
  106. "from Slide s " +
  107. "where s.id<>?1 " +
  108. "and s.courseId=?2 " +
  109. "and s.name=?3 " +
  110. "and s.deleteAt=0 ")
  111. boolean existsByCourseIdAndName(Long slideId, Long courseId, String name);
  112. }