CourseDAO.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package nju.seec.helper.dao;
  2. import nju.seec.helper.entity.Course;
  3. import nju.seec.helper.util.enums.ExceptionType;
  4. import nju.seec.helper.util.exception.HelperException;
  5. import org.springframework.data.domain.Page;
  6. import org.springframework.data.domain.Pageable;
  7. import org.springframework.data.jpa.repository.JpaRepository;
  8. import org.springframework.data.jpa.repository.Modifying;
  9. import org.springframework.data.jpa.repository.Query;
  10. import org.springframework.stereotype.Repository;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import java.util.Set;
  13. /**
  14. * @author cst
  15. */
  16. @Repository
  17. public interface CourseDAO extends JpaRepository<Course, Long> {
  18. /**
  19. * 封装findById
  20. *
  21. * @param id
  22. * @return
  23. */
  24. default Course findCourseById(Long id) {
  25. return this.findById(id)
  26. .filter(course -> course.getDeleteAt() == 0)
  27. .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "找不到课程"));
  28. }
  29. /**
  30. * 检查老师是否已创建同名课程
  31. *
  32. * @param teacherId
  33. * @param name
  34. * @param courseId
  35. * @return
  36. */
  37. @Query("select case when count(c.id)>0 then true else false end " +
  38. "from Course c " +
  39. "where c.id<>?1 " +
  40. "and c.teacherId=?2 " +
  41. "and c.name=?3 " +
  42. "and c.deleteAt=0 ")
  43. boolean existsByTeacherIdAndName(Long courseId, Long teacherId, String name);
  44. /**
  45. * 查询老师创建的课程
  46. *
  47. * @param teacherId
  48. * @param key
  49. * @param pageable
  50. * @return
  51. */
  52. @Query("select c from Course c " +
  53. "where c.teacherId=?1 " +
  54. "and c.name like concat('%',?2,'%') " +
  55. "and c.deleteAt=0 ")
  56. Page<Course> findByTeacherIdAndKey(Long teacherId, String key, Pageable pageable);
  57. /**
  58. * 查询课程
  59. *
  60. * @param key
  61. * @param pageable
  62. * @return
  63. */
  64. @Query("select c from Course c " +
  65. "where (c.name like concat('%',?1,'%') or c.teacherName like concat('%',?1,'%')) " +
  66. "and c.deleteAt=0 ")
  67. Page<Course> findByKey(String key, Pageable pageable);
  68. /**
  69. * 按ID列表查询
  70. *
  71. * @param courseIds
  72. * @param key
  73. * @param pageable
  74. * @return
  75. */
  76. @Query("select c from Course c " +
  77. "where c.id in ?1 " +
  78. "and (c.name like concat('%',?2,'%') or c.teacherName like concat('%',?2,'%')) " +
  79. "and c.deleteAt=0 ")
  80. Page<Course> findByIdsAndKey(Set<Long> courseIds, String key, Pageable pageable);
  81. /**
  82. * 更新所有人信息
  83. *
  84. * @param userId
  85. * @param userName
  86. */
  87. @Transactional(rollbackFor = Exception.class)
  88. @Modifying
  89. @Query("update Course c " +
  90. "set c.teacherName=?2 " +
  91. "where c.teacherId=?1 ")
  92. void updateUser(Long userId, String userName);
  93. }