开发逻辑
course_base这个是课程列表。
teachplan这个是课程计划表
就是某课程下计划讲什么重点内容。
开发步骤
第一步:
先查询Bootstrap开发框架,id=4028e581617f945f01617f9dabc40000 这个课程的根节点,如果这个课程没有根节点就根据这个课程的名称添加为根节点,如果查询到根节点,就返回这个根节点的id。
也是就service中的 getTeachplanRoot(String courseId)方法。
第二步:
teachplan表中的grade字段代表重点内容的级别,1级代表一级节点,2:代表2级节点,3:代表3级节点。
当用户不选择父节点,就是2级节点(因为1级节点是课程名称),如果选择父节点就是3级节点。
然后将前端提交过来的内容添加到teachplan表中
添加api接口
添加controller
//因为这个方法是post方法,所以需要将json转为java对象。所以通过@RequestBody 这个参数可以转化
@RequestBody
//这个是事务处理
@Transactional
添加service
具体service代码如下
@Autowired
TeachplanRepository teachplanRepository;
@Transactional
public ResponseResult addTeachplan(Teachplan teachplan) {
if(teachplan == null ||
StringUtils.isEmpty(teachplan.getPname()) ||
StringUtils.isEmpty(teachplan.getCourseid())){
ExceptionCast.cast(CommonCode.INVALID_PARAM);
}
//课程id
String courseid = teachplan.getCourseid();
//父结点的id
String parentid = teachplan.getParentid();
if(StringUtils.isEmpty(parentid)){
//获取课程的根结点
parentid = getTeachplanRoot(courseid);
}
//查询根结点信息
Optional<Teachplan> optional = teachplanRepository.findById(parentid);
Teachplan teachplan1 = optional.get();
//父结点的级别
String parent_grade = teachplan1.getGrade();
//创建一个新结点准备添加
Teachplan teachplanNew = new Teachplan();
//将teachplan的属性拷贝到teachplanNew中
BeanUtils.copyProperties(teachplan,teachplanNew);
//要设置必要的属性
teachplanNew.setParentid(parentid);
if(parent_grade.equals("1")){
teachplanNew.setGrade("2");
}else{
teachplanNew.setGrade("3");
}
teachplanNew.setStatus("0");//未发布
teachplanRepository.save(teachplanNew);
return new ResponseResult(CommonCode.SUCCESS);
}
//获取课程的根结点
public String getTeachplanRoot(String courseId){
Optional<CourseBase> optional = courseBaseRepository.findById(courseId);
if(!optional.isPresent()){
return null;
}
CourseBase courseBase = optional.get();
//调用dao查询teachplan表得到该课程的根结点(一级结点)
List<Teachplan> teachplanList = teachplanRepository.findByCourseidAndParentid(courseId, "0");
if(teachplanList == null || teachplanList.size()<=0){
//新添加一个课程的根结点
Teachplan teachplan = new Teachplan();
teachplan.setCourseid(courseId);
teachplan.setParentid("0");
teachplan.setGrade("1");//一级结点
teachplan.setStatus("0");
teachplan.setPname(courseBase.getName());
teachplanRepository.save(teachplan);
return teachplan.getId();
}
//返回根结点的id
return teachplanList.get(0).getId();
}