课程目标管理代码优化

master
Alan 4 months ago
parent f2086ebf35
commit bb95bd444e
  1. 2
      src/main/java/com/teaching/backend/common/ErrorCode.java
  2. 17
      src/main/java/com/teaching/backend/controller/courses/CoursesController.java
  3. 1
      src/main/java/com/teaching/backend/controller/courses/ObjectiveContentsController.java
  4. 40
      src/main/java/com/teaching/backend/service/impl/courses/CourseObjectivesServiceImpl.java
  5. 98
      src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
  6. 29
      src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentKnowServiceImpl.java
  7. 2
      src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentsServiceImpl.java
  8. 4
      src/main/java/com/teaching/backend/utils/CourseCode.java

@ -18,7 +18,7 @@ public enum ErrorCode {
STUDENT_NOT_EXIT(40006, "缺少学生信息"),
COURSES_NOT_EXIT(40007, "缺少课程信息"),
CONTENT_NOT_EXIT(40008, "内容id不存在"),
KNOWS_EXIT(40009, "该项下面存在关联的知识点"),
KNOWS_EXIT(40009, "该项下面存在关联的知识点,请在删除关联的知识点后再来操作!"),
INVALID_ROLE(400010, "角色不存在"),
NOT_LOGIN_ERROR(40100, "未登录"),

@ -3,19 +3,14 @@ package com.teaching.backend.controller.courses;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.CommonResult;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.dto.courses.CoursesDTO;
import com.teaching.backend.model.dto.courses.PageDTO;
import com.teaching.backend.model.entity.courses.Courses;
import com.teaching.backend.model.entity.umsAdmin.UmsStudent;
import com.teaching.backend.model.query.CourseQuery;
import com.teaching.backend.model.vo.courses.CoursesVO;
import com.teaching.backend.model.vo.umsAdmin.UmsStudentVO;
import com.teaching.backend.service.courses.ICoursesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -24,7 +19,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
/**
* <p>
@ -87,7 +84,6 @@ public class CoursesController {
@ApiOperation("查询课程列表")
@ValidateParams({"userId"})
@GetMapping("/page")
// @PostMapping("/page")
public BaseResponse<PageDTO<CoursesVO>> getCourses(CourseQuery courseQuery){
PageDTO<CoursesVO> coursesList = coursesService.queryCourses(courseQuery);
return ResultUtils.success(coursesList);
@ -97,9 +93,6 @@ public class CoursesController {
@ValidateParams({"id"})
@GetMapping("/{id}")
public BaseResponse<CoursesDTO> getByIdCourse(@PathVariable String id){
if(id==null){
throw new BusinessException(ErrorCode.PARAMS_ERROR,"课程id为空");
}
Courses course = coursesService.getById(id);
CoursesDTO coursesDTO = new CoursesDTO();
BeanUtils.copyProperties(course,coursesDTO);
@ -114,16 +107,12 @@ public class CoursesController {
return ResultUtils.success("编辑成功");
}
//TODO:删除功能暂未完善,数据缺失
// 暂时发现有个漏洞 就是目标关联的有知识点的时候还是可以直接删除
// (当有内容的时候本来就是得先删除内容--关联也有知识点 所以内容下面的不用校验 )
@ApiOperation("根据id删除课程")
@ValidateParams({"id"})
@DeleteMapping("/{id}")
public BaseResponse deleteCourses(@PathVariable String id){
coursesService.deleteBatchByIds(id);
// coursesService.removeBatchByIds(ids);
return ResultUtils.success("删除成功");
}

@ -39,7 +39,6 @@ public class ObjectiveContentsController {
@ValidateParams({"id"})
@DeleteMapping("/{id}")
public BaseResponse<String> deleteContent(@PathVariable String id){
// objectiveContentsService.removeById(id);
String data = objectiveContentsService.deleteById(id);
return ResultUtils.success(data);
}

@ -25,6 +25,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.teaching.backend.utils.CourseCode.TOTAL_OBJECTIVE_HAS_NO_CHILD;
/**
* <p>
* 服务实现类
@ -54,33 +56,33 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
CourseObjectives courseObjectivesNew = new CourseObjectives();
BeanUtil.copyProperties(courseObjectivesDTO,courseObjectivesNew);
String pid = courseObjectivesDTO.getPid();
// if(pid.equals(null)){
// throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"pid父节点不能为空");
// }
Integer type = courseObjectivesDTO.getType();
//判断是否已经添加过该类型的目标
//每个类型的目标只能有一个
//判断是否已经添加过该类型的目标(每个类型的目标只能有一个)
Long count = courseObjectivesMapper.selectCount(new LambdaQueryWrapper<CourseObjectives>()
.eq(CourseObjectives::getPid, pid)
.eq(CourseObjectives::getType, type));
if (count==CourseCode.OBJECTIVE_EXIT.getValue()){
throw new BusinessException(ErrorCode.CONTENT_EXISTS,"该类型的目标已存在,禁止重复添加!");
}
if (type!=CourseCode.SI_ZHENG_TYPE.getValue()){
if (!type.equals(CourseCode.SI_ZHENG_TYPE.getValue())){
Long sz = SZ_EXIT(courseObjectivesDTO);
if (sz!=CourseCode.SI_ZHENG_EXIT.getValue()){
throw new BusinessException(ErrorCode.PARAMS_ILLEGAL,"请在添加完思政目标以后再添加此类型目标!");
}
}
courseObjectivesMapper.insert(courseObjectivesNew);
//插入新的课程目标
int insert = courseObjectivesMapper.insert(courseObjectivesNew);
//插入数据以后要把总体目标那边的haschild改成1
if (insert>0){
CourseObjectives courseObjectivesOld = courseObjectivesMapper.selectById(pid);
if (courseObjectivesOld.getHasChild() == TOTAL_OBJECTIVE_HAS_NO_CHILD.getValue()) {
courseObjectivesOld.setHasChild(1);
courseObjectivesMapper.updateById(courseObjectivesOld);
}
}
return "添加成功";
//往表里面写分项目标
// courseObjectivesMapper.insert(courseObjectivesNew);
// //插入数据以后要把总体目标那边的haschild改成1
// CourseObjectives courseObjectivesOld = courseObjectivesMapper.selectById(pid);
// courseObjectivesOld.setHasChild(1);
// courseObjectivesMapper.updateById(courseObjectivesOld);
}
private Long SZ_EXIT(CourseObjectivesDTO courseObjectivesDTO) {
@ -110,7 +112,7 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
if (type == CourseCode.TOTAL_OBJECTIVE_TYPE.getValue()){
throw new BusinessException(ErrorCode.OPERATION_ERROR,"课程总目标只需清除所列内容即可!");
}
//TODO:等到删完内容后判断还是上来就判断?
//等到删完内容后判断还是上来就判断?
Long count = objectiveContentKnowMapper.selectCount(new LambdaQueryWrapper<ObjectiveContentKnow>()
.eq(ObjectiveContentKnow::getObjectiveOrContent, id));
if(count > CourseCode.KNOWS_EXIT.getValue()){
@ -121,9 +123,9 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
LambdaQueryWrapper<CourseObjectives> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CourseObjectives::getPid, pid);
Long countSZ = courseObjectivesMapper.selectCount(queryWrapper);
if (countSZ==CourseCode.SI_ZHENG_EXIT.getValue()){
if (countSZ.equals(CourseCode.SI_ZHENG_EXIT.getValue())){
Long countContentsSZ = getCount(id);
if (countContentsSZ<(CourseCode.CONTENT_EXIT.getValue()))
if (countContentsSZ == (CourseCode.CONTENT_EXIT.getValue()))
{
// 删除操作和更新父目标状态
deleteObjectiveAndUpdateParent(id, pid);
@ -140,7 +142,7 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
}
else {
Long countContentsF = getCount(id);
if (countContentsF<CourseCode.CONTENT_EXIT.getValue()){
if (countContentsF == CourseCode.CONTENT_EXIT.getValue()){
courseObjectivesMapper.deleteById(id);
return "删除成功";
}
@ -160,7 +162,7 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
private void deleteObjectiveAndUpdateParent(String id, String pid) {
courseObjectivesMapper.deleteById(id);
CourseObjectives parentObjective = courseObjectivesMapper.selectById(pid);
parentObjective.setHasChild(0);
parentObjective.setHasChild(TOTAL_OBJECTIVE_HAS_NO_CHILD.getValue());
courseObjectivesMapper.updateById(parentObjective);
}

@ -39,6 +39,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
@ -99,63 +100,68 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
@Override
@Transactional
public String saveCourseWithObjective(CoursesDTO coursesDTO) {
// String teacher = coursesDTO.getTeacher();
// if (teacher == null || teacher.equals("")) {
// throw new BusinessException(ErrorCode.USERNAME_NOT_EXIT);
// }
// 初始化并生成新的课程ID
Courses courses = new Courses();
String courseId = UUID.randomUUID().toString().replace("-", "");
CourseObjectives courseObjectives = new CourseObjectives();
// 拷贝属性并设置创建时间和ID
BeanUtils.copyProperties(coursesDTO, courses);
courses.setCreateTime(LocalDateTime.now());
courses.setId(courseId);
// 检查课程代码是否已存在
String code = coursesDTO.getCode();
QueryWrapper<Courses> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("code", code);
Long count = coursesMapper.selectCount(queryWrapper);
if (count == 0) {
int insert = coursesMapper.insert(courses);
if (insert > 0) {
Courses coursesNew = coursesMapper.selectById(courseId);
courseObjectives.setCourseId(courseId);
courseObjectives.setType(CourseCode.TOTAL_OBJECTIVE_TYPE.getValue());
// courseObjectives.setName(coursesNew.getName() + "课程总体目标");
int insertTotal = courseObjectivesMapper.insert(courseObjectives);
if (insertTotal>0){
CourseObjectives courseTotalObjectives = courseObjectivesMapper.selectOne
(new LambdaQueryWrapper<CourseObjectives>().eq(CourseObjectives::getCourseId, courseId));
String courseTotalObjectivesId = courseTotalObjectives.getId();
List<ObjectivesType> objectivesTypes = objectivesTypeMapper.selectList(new LambdaQueryWrapper<ObjectivesType>());
ArrayList<CourseObjectives> courseObjectivesFList = new ArrayList<>();
CourseObjectives courseObjectivesF = null;
for (ObjectivesType objectivesType : objectivesTypes) {
Integer typeId = objectivesType.getId();
if (!typeId.equals(CourseCode.TOTAL_OBJECTIVE_TYPE.getValue())){
// String typeName = objectivesType.getTypeName();
courseObjectivesF = new CourseObjectives();
courseObjectivesF.setType(typeId);
// courseObjectivesF.setName(typeName);
courseObjectivesF.setPid(courseTotalObjectivesId);
courseObjectivesFList.add(courseObjectivesF);
}
}
courseObjectivesMapper.insertBatch(courseObjectivesFList);
courseTotalObjectives.setHasChild(1);
courseObjectivesMapper.updateById(courseTotalObjectives);
}
}
return "添加成功";
} else {
Long count = query().eq("code", code).count();
if (count > 0) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "这个课程已经存在了!请联系系统相关人员为您导入课程数据!");
}
// 插入课程信息
int insert = coursesMapper.insert(courses);
if (insert <= 0) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "课程插入失败!");
}
// 插入课程目标信息
CourseObjectives courseObjectives = new CourseObjectives();
courseObjectives.setCourseId(courseId);
courseObjectives.setType(CourseCode.TOTAL_OBJECTIVE_TYPE.getValue());
int insertTotal = courseObjectivesMapper.insert(courseObjectives);
if (insertTotal <= 0) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "课程目标插入失败!");
}
// 获取插入后的课程目标信息
CourseObjectives courseTotalObjectives = courseObjectivesMapper.selectOne(
new LambdaQueryWrapper<CourseObjectives>().eq(CourseObjectives::getCourseId, courseId)
);
String courseTotalObjectivesId = courseTotalObjectives.getId();
// 获取所有目标类型并插入子目标
List<ObjectivesType> objectivesTypes = objectivesTypeMapper.selectList(new LambdaQueryWrapper<>());
ArrayList<CourseObjectives> courseObjectivesFList = new ArrayList<>();
for (ObjectivesType objectivesType : objectivesTypes) {
if (!objectivesType.getId().equals(CourseCode.TOTAL_OBJECTIVE_TYPE.getValue())) {
CourseObjectives courseObjectivesF = new CourseObjectives();
courseObjectivesF.setType(objectivesType.getId());
courseObjectivesF.setPid(courseTotalObjectivesId);
courseObjectivesFList.add(courseObjectivesF);
}
}
// 批量插入子目标并更新父目标的hasChild字段
if (!courseObjectivesFList.isEmpty()) {
courseObjectivesMapper.insertBatch(courseObjectivesFList);
courseTotalObjectives.setHasChild(1);
courseObjectivesMapper.updateById(courseTotalObjectives);
}
return "添加成功";
}
@Override
public PageDTO<CoursesVO> queryCourses(CourseQuery courseQuery) {
// 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);
@ -187,9 +193,11 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null, Courses::getName, courseQuery.getName())
.apply("FIND_IN_SET({0}, teacher)", courseQuery.getUserId())
.eq(courseQuery.getCategory() != null && !courseQuery.getCategory().isEmpty(), Courses::getCategory, courseQuery.getCategory())
.eq(StringUtils.hasText(courseQuery.getCategory()), Courses::getCategory, courseQuery.getCategory())
// .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())
.select(Courses::getId,Courses::getTeacher,Courses::getImg,Courses::getName,Courses::getCredit,Courses::getClasshours)
.page(page);
return PageDTO.of(p,CoursesVO.class);
}
@ -204,6 +212,7 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null, Courses::getName, courseQuery.getName())
.in(Courses::getId, coursesList)
.select(Courses::getId,Courses::getTeacher,Courses::getImg,Courses::getName,Courses::getCredit,Courses::getClasshours)
.page(page);
return PageDTO.of(p, CoursesVO.class);
}
@ -215,6 +224,7 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
.eq(courseQuery.getNature() != null && !courseQuery.getNature().isEmpty(), Courses::getNature, courseQuery.getNature())
.eq(courseQuery.getAssessmenttype() != null &&!courseQuery.getAssessmenttype().isEmpty(), Courses::getAssessmenttype, courseQuery.getAssessmenttype())
.apply(courseQuery.getTeacher() != null && !courseQuery.getTeacher().isEmpty(), "FIND_IN_SET({0}, teacher)", courseQuery.getTeacher())
.select(Courses::getId,Courses::getTeacher,Courses::getImg,Courses::getName,Courses::getCredit,Courses::getClasshours)
.page(page);
return PageDTO.of(p, CoursesVO.class);
}

@ -18,6 +18,7 @@ import com.teaching.backend.service.courses.IObjectiveContentKnowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
@ -71,7 +72,6 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
objectiveContentKnow.setKnow(knowId);
objectiveContentKnowList.add(objectiveContentKnow);
}
try {
objectiveContentKnowMapper.insertBatchSomeColumn(objectiveContentKnowList);
} catch (Exception e) {
@ -84,12 +84,7 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
@Override
public ObjectiveContentKnowVO getCountData(String objectiveOrContentId) {
// 检查objectiveOrContentId是否为空,如果为空则抛出异常
if (objectiveOrContentId == null) {
throw new BusinessException(ErrorCode.OBJECTIVE_OR_CONTENT_NOT_EXIT);
}
// 查询分项目标关联的知识点数据
// 查询分项目标或者是下面的内容关联的知识点数据(按设计要求,这个传的就是分项目标的id)
List<ObjectiveContentKnow> objectiveContentKnows = objectiveContentKnowMapper.selectList(
new LambdaQueryWrapper<ObjectiveContentKnow>()
.eq(ObjectiveContentKnow::getObjectiveOrContent, objectiveOrContentId));
@ -105,16 +100,16 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
Integer classhours = coursesMapper.selectById(courseId).getClasshours();
// 计算并设置比例(百分比)
BigDecimal percentage = calculatePercentage(objectiveContentKnowVO.getKnowsTime(), classhours);
objectiveContentKnowVO.setPersent(percentage + "%");
// BigDecimal percentage = calculatePercentage(objectiveContentKnowVO.getKnowsTime(), classhours);
// objectiveContentKnowVO.setPersent(percentage + "%");
// 查找分项目标下的内容
List<ObjectiveContents> objectiveContents = objectiveContentsMapper.selectList(
new LambdaQueryWrapper<ObjectiveContents>()
List<ObjectiveContents> objectiveContents = objectiveContentsMapper.selectList(new LambdaQueryWrapper<ObjectiveContents>()
.eq(ObjectiveContents::getObjectiveId, objectiveOrContentId));
// 如果存在分项目标内容,处理并计算每个内容的相关数据
if (objectiveContents != null && !objectiveContents.isEmpty()) {
if (!CollectionUtils.isEmpty(objectiveContents)) {
List<ObjectiveContentKnowVO> contentKnowVOs = objectiveContents.stream()
.map(content -> createObjectiveContentKnowVO(content.getId(), objectiveContentKnowMapper.selectList(
new LambdaQueryWrapper<ObjectiveContentKnow>()
@ -123,10 +118,10 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
.collect(Collectors.toList());
// 计算每个内容的知识点总学时的比例(百分比)
contentKnowVOs.forEach(contentKnowVO -> {
BigDecimal contentPercentage = calculatePercentage(contentKnowVO.getKnowsTime(), classhours);
contentKnowVO.setPersent(contentPercentage + "%");
});
// contentKnowVOs.forEach(contentKnowVO -> {
// BigDecimal contentPercentage = calculatePercentage(contentKnowVO.getKnowsTime(), classhours);
// contentKnowVO.setPersent(contentPercentage + "%");
// });
// 设置内容的知识点数据
objectiveContentKnowVO.setContentKnowsData(contentKnowVOs);
@ -168,6 +163,4 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
// 计算knowsTime占classhours的比例,先除以classhours再乘以100,保留两位小数,并四舍五入
return knowsTime.divide(classHoursBigDecimal, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP);
}
}

@ -41,6 +41,6 @@ public class ObjectiveContentsServiceImpl extends ServiceImpl<ObjectiveContentsM
throw new BusinessException(ErrorCode.KNOWS_EXIT);
}
int delete = objectiveContentsMapper.deleteById(id);
return delete>0 ? "删除成功!" : "删除失败!";
return delete > 0 ? "删除成功!" : "删除失败!";
}
}

@ -4,14 +4,14 @@ public enum CourseCode {
HAS_CHILD(1, "存在子节点"),
OBJECTIVE_EXIT(1,"目标存在"),
CONTENT_EXIT(1, "存在内容"),
CONTENT_EXIT(0, "存在内容"),
SI_ZHENG_TYPE(1, "思政目标类型"),
SI_ZHENG_EXIT(1, "思政目标存在"),
KNOWS_EXIT(0, "存在关联的知识点"),
TEACHER_ROLE_ID(1, "教师的角色id是1"),
TOTAL_OBJECTIVE_TYPE(0, "课程总目标"),
TOTAL_OBJECTIVE_HAS_NO_CHILD(0, "课程总目标下面没有分项目标"),
Total_EXIT(0, "课程总目标"),
COURSE_UPDATING(1, "课程正在修改中");

Loading…
Cancel
Save