课程查询相关代码优化

master
Alan 5 months ago
parent b3e8d5e6f7
commit 7c0c549928
  1. 2
      src/main/java/com/teaching/backend/common/ErrorCode.java
  2. 2
      src/main/java/com/teaching/backend/controller/courses/CoursesController.java
  3. 6
      src/main/java/com/teaching/backend/controller/courses/StudentCoursesController.java
  4. 1
      src/main/java/com/teaching/backend/service/courses/ICoursesService.java
  5. 73
      src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
  6. 1
      src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentKnowServiceImpl.java
  7. 3
      src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java

@ -19,6 +19,8 @@ public enum ErrorCode {
COURSES_NOT_EXIT(40007, "缺少课程信息"),
CONTENT_NOT_EXIT(40008, "内容id不存在"),
KNOWS_EXIT(40009, "该项下面存在关联的知识点"),
INVALID_ROLE(400010, "角色不存在"),
NOT_LOGIN_ERROR(40100, "未登录"),
NO_AUTH_ERROR(40101, "无权限"),
NOT_FOUND_ERROR(40400, "请求数据不存在"),

@ -30,7 +30,7 @@ import java.util.*;
* @author zjh
* @since 2024-05-30
*/
@Api(tags = "教师管理课程接口")
@Api(tags = "课程管理接口")
@RestController
@RequestMapping("/coursesteacher")
public class CoursesController {

@ -39,11 +39,5 @@ public class StudentCoursesController {
return ResultUtils.success(data);
}
@ApiOperation("查询课程列表")
@GetMapping("/page")
public BaseResponse<PageDTO<CoursesVO>> getCourses(CourseQuery courseQuery){
PageDTO<CoursesVO> coursesList = studentCoursesService.queryCourses(courseQuery);
return ResultUtils.success(coursesList);
}
}

@ -37,5 +37,4 @@ public interface ICoursesService extends IService<Courses> {
void down(HttpServletResponse response, String id) throws Exception;
void populateTeacherNames(List<CoursesVO> courseList);
}

@ -10,19 +10,13 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.courses.CourseObjectivesMapper;
import com.teaching.backend.mapper.courses.CoursesMapper;
import com.teaching.backend.mapper.courses.ObjectiveContentsMapper;
import com.teaching.backend.mapper.courses.ObjectivesTypeMapper;
import com.teaching.backend.mapper.courses.*;
import com.teaching.backend.mapper.umsAdmin.UmsAdminMapper;
import com.teaching.backend.mapper.umsAdmin.UmsAdminRoleRelationMapper;
import com.teaching.backend.mapper.umsAdmin.UmsTeacherMapper;
import com.teaching.backend.model.dto.courses.CoursesDTO;
import com.teaching.backend.model.dto.courses.PageDTO;
import com.teaching.backend.model.entity.courses.CourseObjectives;
import com.teaching.backend.model.entity.courses.Courses;
import com.teaching.backend.model.entity.courses.ObjectiveContents;
import com.teaching.backend.model.entity.courses.ObjectivesType;
import com.teaching.backend.model.entity.courses.*;
import com.teaching.backend.model.entity.umsAdmin.UmsAdmin;
import com.teaching.backend.model.entity.umsAdmin.UmsAdminRoleRelation;
import com.teaching.backend.model.entity.umsAdmin.UmsTeacher;
@ -75,6 +69,8 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
UmsTeacherMapper umsTeacherMapper;
@Autowired
UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;
@Autowired
StudentCoursesMapper studentCoursesMapper;
@Override
@ -142,26 +138,64 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
UmsAdminRoleRelation umsAdminRoleRelation = umsAdminRoleRelationMapper.selectOne(new LambdaQueryWrapper<UmsAdminRoleRelation>()
.eq(UmsAdminRoleRelation::getAdminId, umsAdmin.getId()));
// Determine if the user is a teacher
boolean isTeacher = umsAdminRoleRelation.getRoleId() == CourseCode.TEACHER_ROLE_ID.getValue();
int roleId = Math.toIntExact(umsAdminRoleRelation.getRoleId());
PageDTO<CoursesVO> coursesVOPageDTO;
switch (roleId) {
case 1:
coursesVOPageDTO = queryForTeacher(courseQuery, username);
break;
case 2:
coursesVOPageDTO = queryForStudent(courseQuery, username);
break;
case 3:
coursesVOPageDTO = queryForAdmin(courseQuery);
break;
default:
throw new BusinessException(ErrorCode.INVALID_ROLE);
}
populateTeacherNames(coursesVOPageDTO.getList());
return coursesVOPageDTO;
}
private PageDTO<CoursesVO> queryForTeacher(CourseQuery courseQuery, String username) {
Page<Courses> page = courseQuery.toMpPageDefaultSortByCreateTime();
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null, Courses::getName, courseQuery.getName())
.apply(isTeacher, "FIND_IN_SET({0}, teacher)", username)
.eq(!isTeacher && courseQuery.getCategory() != null, Courses::getCategory, courseQuery.getCategory())
.eq(!isTeacher && courseQuery.getNature() != null, Courses::getNature, courseQuery.getNature())
.eq(!isTeacher && courseQuery.getAssessmenttype() != null, Courses::getAssessmenttype, courseQuery.getAssessmenttype())
.apply(!isTeacher && courseQuery.getTeacher() != null && !courseQuery.getTeacher().isEmpty(), "FIND_IN_SET({0}, teacher)", courseQuery.getTeacher())
.apply("FIND_IN_SET({0}, teacher)", username)
.page(page);
return PageDTO.of(p, CoursesVO.class);
}
PageDTO<CoursesVO> coursesVOPageDTO = PageDTO.of(p, CoursesVO.class);
populateTeacherNames(coursesVOPageDTO.getList());
private PageDTO<CoursesVO> queryForStudent(CourseQuery courseQuery, String username) {
List<StudentCourses> studentCourses = studentCoursesMapper.selectList(new LambdaQueryWrapper<StudentCourses>()
.eq(StudentCourses::getStudent, username));
List<String> coursesList = studentCourses.stream()
.map(StudentCourses::getCourse)
.collect(Collectors.toList());
return coursesVOPageDTO;
Page<Courses> page = courseQuery.toMpPageDefaultSortByCreateTime();
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null, Courses::getName, courseQuery.getName())
.in(Courses::getId, coursesList)
.page(page);
return PageDTO.of(p, CoursesVO.class);
}
private PageDTO<CoursesVO> queryForAdmin(CourseQuery courseQuery) {
Page<Courses> page = courseQuery.toMpPageDefaultSortByCreateTime();
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null, Courses::getName, courseQuery.getName())
.eq(courseQuery.getCategory() != null, Courses::getCategory, courseQuery.getCategory())
.eq(courseQuery.getNature() != null, Courses::getNature, courseQuery.getNature())
.eq(courseQuery.getAssessmenttype() != null, Courses::getAssessmenttype, courseQuery.getAssessmenttype())
.apply(courseQuery.getTeacher() != null && !courseQuery.getTeacher().isEmpty(), "FIND_IN_SET({0}, teacher)", courseQuery.getTeacher())
.page(page);
return PageDTO.of(p, CoursesVO.class);
}
public void populateTeacherNames(List<CoursesVO> courseList) {
private void populateTeacherNames(List<CoursesVO> courseList) {
for (CoursesVO coursesVO : courseList) {
List<String> teacherNameList = new ArrayList<>();
String[] teacherIds = coursesVO.getTeacher().split(",");
@ -179,6 +213,7 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
}
}
@Override
public void deleteBatchByIds(String id) {
if(id==null){

@ -54,7 +54,6 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
@Autowired
SnowflakeGenerator snowflakeGenerator;
//TODO:优化插入
@Override
@Transactional
public String saveKnowsWithObjectiveOrContent(ObjectiveContentKnowDTO objectiveContentKnowDTO) {

@ -86,6 +86,7 @@ public class StudentCoursesServiceImpl extends ServiceImpl<StudentCoursesMapper,
return "添加成功";
}
//TODO:学生这个课程要不要整合到教师那边???
@Override
public PageDTO<CoursesVO> queryCourses(CourseQuery courseQuery) {
String username = courseQuery.getUsername();
@ -110,7 +111,7 @@ public class StudentCoursesServiceImpl extends ServiceImpl<StudentCoursesMapper,
PageDTO<CoursesVO> coursesVOPageDTO = PageDTO.of(p, CoursesVO.class);
coursesService.populateTeacherNames(coursesVOPageDTO.getList());
// coursesService.populateTeacherNames(coursesVOPageDTO.getList());
return coursesVOPageDTO;
}

Loading…
Cancel
Save