添加Course的utils

优化课程删除逻辑
编辑课程加锁
master
Alan 5 months ago
parent 03758c7d3b
commit 2e9ea8cf6c
  1. 4
      src/main/java/com/teaching/backend/controller/courses/CourseObjectivesController.java
  2. 11
      src/main/java/com/teaching/backend/controller/courses/CoursesController.java
  3. 2
      src/main/java/com/teaching/backend/model/dto/courses/CourseObjectivesDTO.java
  4. 11
      src/main/java/com/teaching/backend/model/entity/courses/CourseObjectives.java
  5. 5
      src/main/java/com/teaching/backend/model/entity/courses/Courses.java
  6. 6
      src/main/java/com/teaching/backend/model/entity/courses/ObjectivesType.java
  7. 4
      src/main/java/com/teaching/backend/service/courses/ICoursesService.java
  8. 70
      src/main/java/com/teaching/backend/service/impl/courses/CourseObjectivesServiceImpl.java
  9. 137
      src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
  10. 37
      src/main/java/com/teaching/backend/utils/CourseCode.java
  11. 1
      src/main/resources/mapper/CourseObjectivesMapper.xml

@ -14,7 +14,6 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
/**
@ -25,7 +24,7 @@ import java.util.List;
* @author zjh
* @since 2024-05-31
*/
@Api(tags = "项目目标管理接口")
@Api(tags = "课程目标管理接口")
@RestController
@RequestMapping("/course_objectives")
public class CourseObjectivesController {
@ -62,7 +61,6 @@ public class CourseObjectivesController {
String pid = courseObjectivesDTO.getPid();
CourseObjectives courseObjectives = courseObjectivesMapper.selectById(pid);
BeanUtil.copyProperties(courseObjectivesDTO, courseObjectives);
courseObjectives.setUpdateTime(LocalDateTime.now());
courseObjectivesService.updateById(courseObjectives);
return ResultUtils.success("编辑成功");
}

@ -49,10 +49,10 @@ public class CoursesController {
return ResultUtils.success(objectMap);
}
@ApiOperation("新增课程--同步新增课程的总体目标")
@ApiOperation("新增课程--同步新增课程的总体目标并同时添加所有的分项目标")
@PostMapping("/addcourse")
public BaseResponse<String> saveCourse(@RequestBody CoursesDTO coursesDTO, @RequestParam String teacherId){
String data = coursesService.saveCourseWithObjective(coursesDTO, teacherId);
public BaseResponse<String> saveCourse(@RequestBody CoursesDTO coursesDTO){
String data = coursesService.saveCourseWithObjective(coursesDTO);
return ResultUtils.success(data);
}
@ -76,10 +76,7 @@ public class CoursesController {
@ApiOperation("根据id修改课程")
@PutMapping
public BaseResponse<String> editCourse(@RequestBody CoursesDTO coursesDTO){
String courseId = coursesDTO.getId();
Courses course = coursesService.getById(courseId);
BeanUtil.copyProperties(coursesDTO, course);
coursesService.updateById(course);
coursesService.updateCourse(coursesDTO);
return ResultUtils.success("编辑成功");
}

@ -26,7 +26,7 @@ public class CourseObjectivesDTO implements Serializable {
* 分项目标类型 思政 知识素质价值
*/
@ApiModelProperty(value = "分项目标类型",required = true)
private String type;
private Integer type;
/**
* 父节点
*/

@ -33,24 +33,17 @@ public class CourseObjectives implements Serializable {
@TableId(value = "id", type = IdType.ASSIGN_UUID)
private String id;
@ApiModelProperty(value = "创建日期")
private LocalDateTime createTime;
@ApiModelProperty(value = "更新日期")
private LocalDateTime updateTime;
@ApiModelProperty(value = "父级节点")
private String pid;
@ApiModelProperty(value = "是否有子节点")
private String hasChild;
private Integer hasChild;
@ApiModelProperty(value = "目标名称")
private String name;
@ApiModelProperty(value = "目标类型")
private String type;
private Integer type;
@ApiModelProperty(value = "课程id")

@ -139,5 +139,10 @@ public class Courses implements Serializable {
*/
private String description;
/**
*课程状态
*/
private int status;
}

@ -27,9 +27,9 @@ public class ObjectivesType implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.ASSIGN_UUID)
@ApiModelProperty(value = "类型编码,不用输入,使用雪花算法生成")
private String id;
@TableId(value = "id", type = IdType.AUTO)
@ApiModelProperty(value = "类型编码,不用输入,使用自动生成")
private Integer id;
@ApiModelProperty(value = "类型名称",required = true)
private String typeName;

@ -21,7 +21,7 @@ import java.util.List;
*/
public interface ICoursesService extends IService<Courses> {
String saveCourseWithObjective(CoursesDTO coursesDTO, String teacherId);
String saveCourseWithObjective(CoursesDTO coursesDTO);
PageDTO<CoursesVO> queryCourses(CourseQuery courseQuery, String teacherId);
@ -31,4 +31,6 @@ public interface ICoursesService extends IService<Courses> {
int countHours(String id);
List<CoursesVO> getPagePageSize(int page, int pageSize);
void updateCourse(CoursesDTO coursesDTO);
}

@ -12,8 +12,12 @@ import com.teaching.backend.model.entity.courses.CourseObjectives;
import com.teaching.backend.model.entity.courses.ObjectiveContents;
import com.teaching.backend.model.vo.CourseObjectivesTreeVO;
import com.teaching.backend.service.courses.ICourseObjectivesService;
import com.teaching.backend.utils.CourseCode;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
@ -39,37 +43,46 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
ObjectiveContentsMapper objectiveContentsMapper;
@Override
@Transactional
public String addObjectives(CourseObjectivesDTO courseObjectivesDTO) {
CourseObjectives courseObjectivesNew = new CourseObjectives();
BeanUtil.copyProperties(courseObjectivesDTO,courseObjectivesNew);
String pid = courseObjectivesDTO.getPid();
if(pid.equals(null)){
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"pid父节点不能为空");
}
String hasChild = courseObjectivesMapper.selectById(pid).getHasChild();
String type = courseObjectivesDTO.getType();
if (hasChild.equals("0") && !type.equals("71f73bd999f678bcbbdb101e23277feb")){
throw new BusinessException(ErrorCode.PARAMS_ILLEGAL,"请在添加完思政目标以后再添加此类型目标!");
}
Integer type = courseObjectivesDTO.getType();
//判断是否已经添加过该类型的目标
//每个类型的目标只能有一个
LambdaQueryWrapper<CourseObjectives> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper
Long count = courseObjectivesMapper.selectCount(new LambdaQueryWrapper<CourseObjectives>()
.eq(CourseObjectives::getPid, pid)
.eq(CourseObjectives::getType, type);
Long count = courseObjectivesMapper.selectCount(queryWrapper);
if (count>0){
.eq(CourseObjectives::getType, type));
if (count==CourseCode.OBJECTIVE_EXIT.getValue()){
throw new BusinessException(ErrorCode.CONTENT_EXISTS,"该类型的目标已存在,禁止重复添加!");
}
//往表里面写分项目标
CourseObjectives courseObjectivesNew = new CourseObjectives();
BeanUtil.copyProperties(courseObjectivesDTO,courseObjectivesNew);
courseObjectivesNew.setCreateTime(LocalDateTime.now());
if (type!=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);
//插入数据以后要把总体目标那边的haschild改成1
CourseObjectives courseObjectivesOld = courseObjectivesMapper.selectById(pid);
courseObjectivesOld.setUpdateTime(LocalDateTime.now());
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) {
Long count = courseObjectivesMapper.selectCount(new LambdaQueryWrapper<CourseObjectives>()
.eq(CourseObjectives::getPid, courseObjectivesDTO.getPid())
.eq(CourseObjectives::getType, CourseCode.SI_ZHENG_TYPE.getValue()));
return count;
}
/**
@ -82,21 +95,20 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
*/
@Override
public String deleteObjectives(String id) {
final String SI_ZHENG_TYPE="71f73bd999f678bcbbdb101e23277feb";
CourseObjectives courseObjectives = courseObjectivesMapper.selectById(id);
if (courseObjectives == null) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
}
String type = courseObjectives.getType();
Integer type = courseObjectives.getType();
String pid = courseObjectives.getPid();
if (SI_ZHENG_TYPE.equals(type)){
if (type.equals(CourseCode.SI_ZHENG_TYPE.getValue())){
LambdaQueryWrapper<CourseObjectives> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CourseObjectives::getPid, pid);
Long count1 = courseObjectivesMapper.selectCount(queryWrapper);
if (count1==1){
Long count2 = getCount(id);
if (count2==0)
Long countSZ = courseObjectivesMapper.selectCount(queryWrapper);
if (countSZ==CourseCode.SI_ZHENG_EXIT.getValue()){
Long countContentsSZ = getCount(id);
if (countContentsSZ<(CourseCode.CONTENT_EXIT.getValue()))
{
// 删除操作和更新父目标状态
deleteObjectiveAndUpdateParent(id, pid);
@ -112,8 +124,8 @@ public class CourseObjectivesServiceImpl extends ServiceImpl<CourseObjectivesMap
}
}
else {
Long count3 = getCount(id);
if (count3==0){
Long countContentsF = getCount(id);
if (countContentsF<CourseCode.CONTENT_EXIT.getValue()){
courseObjectivesMapper.deleteById(id);
return "删除成功";
}
@ -133,7 +145,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(0);
courseObjectivesMapper.updateById(parentObjective);
}

@ -11,22 +11,26 @@ 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.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.query.CourseQuery;
import com.teaching.backend.model.vo.CourseObjectivesTreeVO;
import com.teaching.backend.model.vo.CoursesVO;
import com.teaching.backend.service.courses.ICoursesService;
import com.teaching.backend.utils.CourseCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* <p>
@ -46,17 +50,16 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
CourseObjectivesMapper courseObjectivesMapper;
@Autowired
ObjectiveContentsMapper objectiveContentsMapper;
@Autowired
ObjectivesTypeMapper objectivesTypeMapper;
@Override
@Transactional
public String saveCourseWithObjective(CoursesDTO coursesDTO, String teacherId) {
public String saveCourseWithObjective(CoursesDTO coursesDTO) {
Courses courses = new Courses();
CourseObjectives courseObjectives = new CourseObjectives();
BeanUtils.copyProperties(coursesDTO, courses);
// List<String> teacherIds = new ArrayList<>();
// teacherIds.add(teacherId);
// courses.setTeacher(String.valueOf(teacherIds));
courses.setTeacher(teacherId);
courses.setCreateTime(LocalDateTime.now());
String code = coursesDTO.getCode();
QueryWrapper<Courses> queryWrapper = new QueryWrapper<>();
@ -65,11 +68,28 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
if (count == 0) {
int insert = coursesMapper.insert(courses);
if (insert > 0) {
Courses courses1 = coursesMapper.selectOne(queryWrapper);
courseObjectives.setCourseId(courses1.getId());
courseObjectives.setCreateTime(courses1.getCreateTime());
courseObjectives.setName(courses1.getName() + "课程总体目标");
courseObjectivesMapper.insert(courseObjectives);
Courses coursesNew = coursesMapper.selectOne(queryWrapper);
courseObjectives.setCourseId(coursesNew.getId());
courseObjectives.setName(coursesNew.getName() + "课程总体目标");
// courseObjectives.setHasChild(1);
int insertTotal = courseObjectivesMapper.insert(courseObjectives);
if (insertTotal>0){
CourseObjectives courseTotalObjectives = courseObjectivesMapper.selectOne
(new LambdaQueryWrapper<CourseObjectives>().eq(CourseObjectives::getCourseId, coursesNew.getId()));
String courseTotalObjectivesId = courseTotalObjectives.getId();
List<ObjectivesType> objectivesTypes = objectivesTypeMapper.selectList(new LambdaQueryWrapper<ObjectivesType>());
for (ObjectivesType objectivesType : objectivesTypes) {
Integer typeId = objectivesType.getId();
String typeName = objectivesType.getTypeName();
CourseObjectives courseObjectivesF = new CourseObjectives();
courseObjectivesF.setType(typeId);
courseObjectivesF.setName(typeName);
courseObjectivesF.setPid(courseTotalObjectivesId);
courseObjectivesMapper.insert(courseObjectivesF);
}
courseTotalObjectives.setHasChild(1);
courseObjectivesMapper.updateById(courseTotalObjectives);
}
}
return "添加成功";
} else {
@ -97,79 +117,54 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
}
@Override
// @Transactional
public void deleteBatchByIds(String id) {
LambdaQueryWrapper<CourseObjectives> queryWrapper = new LambdaQueryWrapper<CourseObjectives>().eq(CourseObjectives::getCourseId, id);
CourseObjectives courseObjectives = courseObjectivesMapper.selectOne(queryWrapper);
CourseObjectives courseObjectives= courseObjectivesMapper.selectOne
(new LambdaQueryWrapper<CourseObjectives>().eq(CourseObjectives::getCourseId, id));
String objectivesId = courseObjectives.getId();
LambdaQueryWrapper<ObjectiveContents> queryWrapper2 = new LambdaQueryWrapper<ObjectiveContents>().eq(ObjectiveContents::getObjectiveId, objectivesId);
Long count = objectiveContentsMapper.selectCount(queryWrapper2);
if (courseObjectives.getHasChild().equals("1")) {
throw new BusinessException(ErrorCode.CONTENT_EXISTS, "该课程已有分项目标,禁止删除");
}
if (!count.equals(0)){
throw new BusinessException(ErrorCode.CONTENT_EXISTS, "总目标下面还有内容,禁止删除");
}
else {
List<CourseObjectivesTreeVO> courseObjectivesTreeVOList = courseObjectivesMapper.selectTreeNodes(objectivesId);
int contents = 0;
List<String> objectiveIds = new ArrayList<>();
for (CourseObjectivesTreeVO courseObjectivesTreeVO : courseObjectivesTreeVOList) {
String objectiveId = courseObjectivesTreeVO.getId();
objectiveIds.add(objectiveId);
Long content = objectiveContentsMapper
.selectCount(new LambdaQueryWrapper<ObjectiveContents>().eq(ObjectiveContents::getObjectiveId, objectiveId));
contents+=content;
}
if (contents==0){
coursesMapper.deleteById(id);
courseObjectivesMapper.deleteById(courseObjectives.getId());
courseObjectivesMapper.deleteBatchIds(objectiveIds);
}
else{
throw new BusinessException(ErrorCode.CONTENT_EXISTS, "该课程的目标下面还有内容,请把相关内容清空后再来删除课程");
}
@Override
public List<CoursesVO> getPagePageSize(int page, int pageSize) {
int startIndex = (page-1) * pageSize;
List<Courses> list = coursesMapper.getItemsByPage(startIndex, pageSize);
// List<Courses> list = coursesMapper.selectList(null);
List<CoursesVO> coursesVo = BeanUtil.copyToList(list, CoursesVO.class);
coursesVo = coursesVo.stream().map((item) -> {
item.setTotalHours(countHours(item.getId()));
item.setTotalchapter(100);
item.setTotalKnow(100);
// if (StringUtils.isBlank(item.getName()) ||
// StringUtils.isBlank(item.getDescription()) ){
// throw new BusinessException(ErrorCode.OPERATION_ERROR,"数据格式有问题,请修改");
// }
return item;
}).collect(Collectors.toList());
return coursesVo;
}
@Override
public int countHours(String id) {
Courses courses = coursesMapper.selectById(id);
Integer classhours = 0;
Integer theoryhours = 0;
Integer practicehours = 0;
Integer experimenthours = 0;
Integer otherhours = 0;
if (courses.getClasshours() != null) {
classhours = courses.getClasshours();
return 0;
}
if (courses.getTheoryhours() != null) {
theoryhours = courses.getTheoryhours();
}
if (courses.getPracticehours() != null) {
practicehours = courses.getPracticehours();
}
if (courses.getExperimenthours() != null) {
experimenthours = courses.getExperimenthours();
}
if (courses.getOtherhours() != null) {
otherhours = courses.getOtherhours();
@Override
public List<CoursesVO> getPagePageSize(int page, int pageSize) {
return null;
}
int total = classhours + theoryhours + practicehours + experimenthours + otherhours;
return total;
/**
* 修改课程信息
* @param coursesDTO
*/
public void updateCourse(CoursesDTO coursesDTO) {
String courseId = coursesDTO.getId();
Courses course = coursesMapper.selectById(courseId);
if (course.getStatus()==CourseCode.COURSE_UODATING.getValue()){
throw new BusinessException(ErrorCode.SYSTEM_ERROR,"课程正在修改中,请等待修改完成后再进行操作");
}
course.setStatus(CourseCode.COURSE_UODATING.getValue());
coursesMapper.updateById(course);
BeanUtil.copyProperties(coursesDTO, course);
course.setStatus(0);
coursesMapper.updateById(course);
}

@ -0,0 +1,37 @@
package com.teaching.backend.utils;
public enum CourseCode {
HAS_CHILD(1, "存在子节点"),
OBJECTIVE_EXIT(1,"目标存在"),
CONTENT_EXIT(1, "存在内容"),
SI_ZHENG_TYPE(1, "思政目标类型"),
SI_ZHENG_EXIT(1, "思政目标存在"),
COURSE_UODATING(1, "课程正在修改中");
/**
* 状态码
*/
private final int value;
/**
* 信息
*/
private final String message;
CourseCode(int value, String message) {
this.value = value;
this.message = message;
}
public int getValue() {
return value;
}
public String getMessage() {
return message;
}
}

@ -4,7 +4,6 @@
<select id="selectTreeNodes" resultType="com.teaching.backend.model.vo.CourseObjectivesTreeVO"
parameterType="string">
with recursive t1 as (
select * from course_objectives where id=#{courseObjectivesId}
union all

Loading…
Cancel
Save