新增课程节点 项目心得

    xiaoxiao2022-12-08  56

    1、上级结点 如果不设置就要从第二层结点添加,例如像测试代码-2一样 ,如果设置了就从第三层结点添加,例如为什么要选择春云

    service

    package com.xuecheng.manage_course.service; import com.xuecheng.framework.domain.course.CourseBase; import com.xuecheng.framework.domain.course.Teachplan; import com.xuecheng.framework.domain.course.ext.TeachplanNode; import com.xuecheng.framework.exception.ExceptionCast; import com.xuecheng.framework.model.response.CommonCode; import com.xuecheng.framework.model.response.ResponseResult; import com.xuecheng.manage_course.dao.CourseBaseRepository; import com.xuecheng.manage_course.dao.TeachplanMapper; import com.xuecheng.manage_course.dao.TeachplanRepository; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Optional; @Service public class CourseService { @Autowired TeachplanMapper teachplanMapper; @Autowired TeachplanRepository teachplanRepository; @Autowired CourseBaseRepository courseBaseRepository; //查询课程 public TeachplanNode findTeachplanList(String courseId){ return teachplanMapper.selectList(courseId); } //添加课程 @Transactional public ResponseResult addTeachplan(Teachplan teachplan){ //校验课程id和课程计划名称 if (teachplan ==null || StringUtils.isEmpty(teachplan.getCourseid()) ||StringUtils.isEmpty(teachplan.getPname())){ ExceptionCast.cast(CommonCode.INVALID_PARAM); } //判断课程上级节点是否为空 String parentid = teachplan.getParentid(); if (StringUtils.isEmpty(parentid)){ //空的节点,获取根节点 parentid = this.getTeachplanRoot(teachplan.getCourseid()); } //不为空,取出父节点信息 Optional<Teachplan> teachplanOptional = teachplanRepository.findById(parentid); if (!teachplanOptional.isPresent()){ ExceptionCast.cast(CommonCode.INVALID_PARAM); } //获取它的父节点信息 Teachplan teachplanParent = teachplanOptional.get(); //父节点级别 String parentGrade = teachplanParent.getGrade(); //设置父节点 teachplan.setParentid(parentid); teachplan.setStatus("0");//未发布 //设置层级 if (parentGrade.equals("1")){ teachplan.setGrade("2"); }else{ teachplan.setGrade("3"); } //设置课程id teachplan.setCourseid(teachplanParent.getCourseid()); teachplanRepository.save(teachplan); return new ResponseResult(CommonCode.SUCCESS); } //查询课程的根节点,如果查不到就要自己新建一个根节点 public String getTeachplanRoot(String courseid){ //先校验课程id Optional<CourseBase> optionalCourseBase = courseBaseRepository.findById(courseid); if (!optionalCourseBase.isPresent()){ return null; } CourseBase courseBase = optionalCourseBase.get(); //取出课程计划根节点 List<Teachplan> teachplanList = teachplanRepository.findByParentidAndCourseid("0", courseid); if (teachplanList==null){ //新增一个根结点 Teachplan teachplanRoot = new Teachplan(); teachplanRoot.setCourseid(courseid); teachplanRoot.setPname(courseBase.getName()); teachplanRoot.setParentid("0"); teachplanRoot.setGrade("1");//1级 teachplanRoot.setStatus("0");//未发布 teachplanRepository.save(teachplanRoot); return teachplanRoot.getId(); } return teachplanList.get(0).getId(); } }

    说一下getTeachplanRoot的方法 1、是判断courseBase是否有这个节点 2、是用来查询有没有根节点也就是一级节点, 如果有就返回id,也就是teachplan的courseid,

    一开始页面传来的是数据: 这个是选择了节点 这是没选的 一眼就看出来 没选的肯定没有父id,所以要自己创建 首先 你要通过课程id courseid去课程表courseBase查一下0节点,也就是第一层的所有信息, 如果第一层存在在下一步 下一步在从teachplan根据findByParentidAndCourseid(‘0’,courseid)来查询第一层的信息,直接返回id 然后你就封装参数就可以了,然后保存就可以了!

    最新回复(0)