优化课程列表查询速度

master
Alan 4 months ago
parent 806e7b365f
commit c1da086cb0
  1. 2
      src/main/java/com/teaching/backend/controller/courses/CoursesController.java
  2. 4
      src/main/java/com/teaching/backend/model/query/CourseQuery.java
  3. 33
      src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
  4. 6
      src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java

@ -66,7 +66,7 @@ public class CoursesController {
@ApiOperation("查询课程列表")
@ValidateParams({"username"})
@ValidateParams({"userId"})
@GetMapping("/page")
// @PostMapping("/page")
public BaseResponse<PageDTO<CoursesVO>> getCourses(CourseQuery courseQuery){

@ -15,8 +15,8 @@ public class CourseQuery extends PageQuery {
@ApiModelProperty("课程名称关键字")
private String name;
@ApiModelProperty(value = "用户的username",required = true)
private String username;
@ApiModelProperty(value = "用户的userId",required = true)
private String userId;
// @ApiModelProperty(value = "用户的角色id",required = true)
// private List<String> roleId;

@ -140,10 +140,14 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
@Override
public PageDTO<CoursesVO> queryCourses(CourseQuery courseQuery) {
int roleId = Integer.parseInt(umsUserMapper.selectOne(new LambdaQueryWrapper<UmsUser>()
.eq(UmsUser::getUsername, courseQuery.getUsername())).getRoleId());
// int roleId = Integer.parseInt(umsUserMapper.selectOne(new LambdaQueryWrapper<UmsUser>()
// .eq(UmsUser::getUsername, courseQuery.getUsername())).getRoleId());
UmsUser umsUser = umsUserMapper.selectById(courseQuery.getUserId());
if (umsUser == null){
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
int roleId = Integer.parseInt(umsUser.getRoleId());
PageDTO<CoursesVO> coursesVOPageDTO;
switch (roleId) {
//教师--也是管理员--但是只能看自己的课程,有搜索功能
case 1:
@ -168,7 +172,7 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
Page<Courses> page = courseQuery.toMpPageDefaultSortByCreateTime();
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null, Courses::getName, courseQuery.getName())
.apply("FIND_IN_SET({0}, teacher)", courseQuery.getUsername())
.apply("FIND_IN_SET({0}, teacher)", courseQuery.getUserId())
.eq(courseQuery.getCategory() != null && !courseQuery.getCategory().isEmpty(), Courses::getCategory, courseQuery.getCategory())
.eq(courseQuery.getNature() != null && !courseQuery.getNature().isEmpty(), Courses::getNature, courseQuery.getNature())
.eq(courseQuery.getAssessmenttype() != null &&!courseQuery.getAssessmenttype().isEmpty(), Courses::getAssessmenttype, courseQuery.getAssessmenttype())
@ -178,11 +182,10 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
private PageDTO<CoursesVO> queryForStudent(CourseQuery courseQuery) {
List<StudentCourses> studentCourses = studentCoursesMapper.selectList(new LambdaQueryWrapper<StudentCourses>()
.eq(StudentCourses::getStudent, courseQuery.getUsername()));
.eq(StudentCourses::getStudent, courseQuery.getUserId()));
List<String> coursesList = studentCourses.stream()
.map(StudentCourses::getCourse)
.collect(Collectors.toList());
Page<Courses> page = courseQuery.toMpPageDefaultSortByCreateTime();
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null, Courses::getName, courseQuery.getName())
@ -190,7 +193,6 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
.page(page);
return PageDTO.of(p, CoursesVO.class);
}
private PageDTO<CoursesVO> queryForAdmin(CourseQuery courseQuery) {
Page<Courses> page = courseQuery.toMpPageDefaultSortByCreateTime();
Page<Courses> p = lambdaQuery()
@ -206,17 +208,16 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
private void populateTeacherNames(List<CoursesVO> courseList) {
for (CoursesVO coursesVO : courseList) {
List<String> teacherNameList = new ArrayList<>();
String[] teacherIds = coursesVO.getTeacher().split(",");
for (String teacherId : teacherIds) {
UmsUser umsUserTea = umsUserMapper.selectOne(new LambdaQueryWrapper<UmsUser>()
.eq(UmsUser::getUsername, teacherId));
if (umsUserTea == null) {
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
String[] teacherUserIds = coursesVO.getTeacher().split(",");
for (String teacherId : teacherUserIds) {
// UmsUser umsUserTea = umsUserMapper.selectOne(new LambdaQueryWrapper<UmsUser>()
// .eq(UmsUser::getUsername, teacherId));
// if (umsUserTea == null) {
// throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
// }
// System.out.println(umsAdminTea);
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, umsUserTea.getId()));
// System.out.println(umsTeacher);
.eq(UmsTeacher::getUserId,teacherId));
if (umsTeacher == null){
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}

@ -84,12 +84,12 @@ public class StudentCoursesServiceImpl extends ServiceImpl<StudentCoursesMapper,
//TODO:学生这个课程要不要整合到教师那边???
@Override
public PageDTO<CoursesVO> queryCourses(CourseQuery courseQuery) {
String username = courseQuery.getUsername();
if (username == null || username.equals("")) {
String userId = courseQuery.getUserId();
if (userId == null || userId.equals("")) {
throw new BusinessException(ErrorCode.STUDENT_NOT_EXIT);
}
List<StudentCourses> studentCourses = studentCoursesMapper.selectList(new LambdaQueryWrapper<StudentCourses>()
.eq(StudentCourses::getStudent, username));
.eq(StudentCourses::getStudent, userId));
List<String> coursesList = studentCourses.stream()
.map(StudentCourses::getCourse)
.collect(Collectors.toList());

Loading…
Cancel
Save