欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

学成在线day06(课程管理)

程序员文章站 2022-03-14 10:15:42
...

①. 需求分析

1>. 需求分析

  • 在线教育平台的课程信息相当于电商平台的商品。课程管理是后台管理功能中最重要的模块。本项目为教学机构提 供课程管理功能,教学机构可以添加属于自己的课程,供学生在线学习

  • 课程管理包括如下功能需求:(1).分类管理 (2).新增课程 (3).修改课程 (4).预览课程 (5).发布课程
    学成在线day06(课程管理)

②. 数据库表分析

2>. 数据库表分析

  • ①. 课程信息内容繁多,将课程信息分类保存在如下表中
    学成在线day06(课程管理)

  • ②.数据表结构如下:
    学成在线day06(课程管理)
    学成在线day06(课程管理)学成在线day06(课程管理)学成在线day06(课程管理)学成在线day06(课程管理)

③. 课程计划

3>. 课程计划需求分析

  • ①. 课程计划定义了课程的章节内容,学生通过课程计划进行在线学习,下图中右侧显示的就是课程计划。 课程计划包括两级,第一级是课程的大章节、第二级是大章节下属的小章节,每个小章节通常是一段视频,学生点 击小章节在线学习

  • ②. 课程计划包括两级,第一级是课程的大章节、第二级是大章节下属的小章节,每个小章节通常是一段视频,学生点 击小章节在线学习

  • ③. 教学管理人员对课程计划如何管理?
    功能包括:添加课程计划、删除课程计划、修改课程计划等。
    学成在线day06(课程管理)

④. 课程计划查询

4>. 课程计划查询

1. 需求分析

  • 课程计划查询是将某个课程的课程计划内容完整的显示出来,如下图所示:
    学成在线day06(课程管理)

2. 后台接口实现

  • ①. 前台定义数据格式
    学成在线day06(课程管理)

  • ②. 实体类环境准备

	//1.实体类要用@Entity标识
	//2.指定表名@Table(name="")
	//3.主键生成策略
	@Data
	@ToString
	@Entity
	@Table(name="teachplan")
	@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
	public class Teachplan implements Serializable {
	    private static final long serialVersionUID = -916357110051689485L;
	    @Id
	    @GeneratedValue(generator = "jpa-uuid")
	    @Column(length = 32)
	    private String id;
	    private String pname;
	    private String parentid;
	    private String grade;
	    private String ptype;
	    private String description;
	    private String courseid;
	    private String status;
	    private Integer orderby;
	    private Double timelength;
	    private String trylearn;
	}
	@Data
	@ToString
	public class TeachplanNode extends Teachplan {
	    List<TeachplanNode> children;
	}
  • ③. Api接口定义
	@Api(value="课程的管理接口",description = "课程的管理接口,课程的增删改查")
	public interface CourseControllerApi {
	    @ApiOperation("课程计划查询")
	    public TeachplanNode findTeachplanList(String courseId);
	}
  • ④. TeachplanMapper.xml
    说明:针对输入参数为简单类型#{}中可以是任意类型,判断参数是否为空要用 _parameter(它属于mybatis的内 置参数)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xuecheng.manage_course.dao.TeachplanMapper">


    <resultMap id="teachplanMap" type="com.xuecheng.framework.domain.course.ext.TeachplanNode">
        <!--一级节点-->
        <id column="one_id" property="id"></id>
        <result column="one_panme" property="pname"></result>
        <!--二级节点-->
        <collection ofType="com.xuecheng.framework.domain.course.ext.TeachplanNode" 
        property="children">
            <id column="two_id" property="id"></id>
            <result column="two_panme" property="pname"></result>
            <!--三级节点-->
            <collection ofType="com.xuecheng.framework.domain.course.Teachplan" 
            property="children">
                <id column="three_id" property="id"></id>
                <result column="three_panme" property="pname"></result>
            </collection>
        </collection>
    </resultMap>

    <select id="selectList" parameterType="string" resultMap="teachplanMap">
      select
        a.id one_id,
        a.pname one_panme,
        b.id two_id,
        b.pname two_panme,
        c.id three_id,
        c.pname three_panme
      from

        teachplan a
        left join teachplan b
        on a.id=b.parentid
        left join teachplan c
        on b.id=c.parentid

      where
        a.parentid='0'
        <if test="_parameter!=null and _parameter=''">
            and a.courseid=#{courseid}
        </if>
      order by
        a.orderby,
        b.orderby,
        c.orderby
    </select>
</mapper>
  • ⑤. Service
@Service
public class CourseService {
    @Autowired
    private TeachplanMapper teachplanMapper;

    public TeachplanNode findTeachplanList(String courseId) {
         return teachplanMapper.selectList(courseId);
    }
}
  • ⑥. controller
	@RestController
	@RequestMapping("/course")
	public class CourseController implements CourseControllerApi {
	
	    @Autowired
	    private CourseService courseService;
	    @GetMapping("/teachplan/list/{courseId}")
	    @Override
	    public TeachplanNode findTeachplanList(@PathVariable("courseId") String courseId) {
	        return courseService.findTeachplanList(courseId);
	    }
	}
  • ⑦. swagger测试
    学成在线day06(课程管理)
{
  "id": "1",
  "pname": "Bootstrap开发框架",
  "parentid": null,
  "grade": null,
  "ptype": null,
  "description": null,
  "courseid": null,
  "status": null,
  "orderby": null,
  "timelength": null,
  "trylearn": null,
  "children": [
    {
      "id": "2",
      "pname": "计算机原理",
      "parentid": null,
      "grade": null,
      "ptype": null,
      "description": null,
      "courseid": null,
      "status": null,
      "orderby": null,
      "timelength": null,
      "trylearn": null,
      "children": [
        {
          "id": "3",
          "pname": "计算机硬件",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        },
        {
          "id": "4",
          "pname": "计算机软件",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        }
      ]
    },
    {
      "id": "5",
      "pname": "计算机编程入门",
      "parentid": null,
      "grade": null,
      "ptype": null,
      "description": null,
      "courseid": null,
      "status": null,
      "orderby": null,
      "timelength": null,
      "trylearn": null,
      "children": [
        {
          "id": "6",
          "pname": "java语法介绍",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        },
        {
          "id": "7",
          "pname": "Hello World",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        }
      ]
    },
    {
      "id": "4028e581617ce7b601617ce801790000",
      "pname": "数据库编程",
      "parentid": null,
      "grade": null,
      "ptype": null,
      "description": null,
      "courseid": null,
      "status": null,
      "orderby": null,
      "timelength": null,
      "trylearn": null,
      "children": [
        {
          "id": "402885816347f814016348d68bad0000",
          "pname": "数据库基础知识",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        },
        {
          "id": "402885816347f814016348d6c5920001",
          "pname": "SQL查询",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        },
        {
          "id": "402885816347f814016348d7153c0002",
          "pname": "SQL优化",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        }
      ]
    },
    {
      "id": "8",
      "pname": "操作系统原理",
      "parentid": null,
      "grade": null,
      "ptype": null,
      "description": null,
      "courseid": null,
      "status": null,
      "orderby": null,
      "timelength": null,
      "trylearn": null,
      "children": [
        {
          "id": "4028e58161bbcd350161bbcefe3d0001",
          "pname": "操作系统原理",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        },
        {
          "id": "4028e581617d02e101617d070ed90000",
          "pname": "操作系统类型介绍",
          "parentid": null,
          "grade": null,
          "ptype": null,
          "description": null,
          "courseid": null,
          "status": null,
          "orderby": null,
          "timelength": null,
          "trylearn": null
        }
      ]
    }
  ]
}

⑤. 添加课程计划

5>. 添加课程计划

1. 需求分析

  • ①. 进入课程计划页面,点击“添加课程计划”

  • ②. 打开添加课程计划页面,输入课程计划信息

学成在线day06(课程管理)

2. 后台接口实现

  • ①. api
    @ApiOperation("添加课程计划")
    public ResponseResult addTeachplan(Teachplan teachplan);
  • ②.controller
    @Override
    @PostMapping("/teachplan/add")
    public ResponseResult addTeachplan(@RequestBody  Teachplan teachplan) {
        return courseService.addTeachplan(teachplan);
    }
  • ③.service
@Service
public class CourseService {
    @Autowired
    private TeachplanMapper teachplanMapper;
    @Autowired
    private TeachPlanRepository teachplanRepository;
    @Autowired
    private CourseBaseRepository courseBaseRepository;
    @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();
    }
  }
  • ④.dao
public interface TeachPlanRepository extends JpaRepository<Teachplan,String> {
    //根据courseId和parentId查询
    public List<Teachplan> findByCourseidAndParentid(String courseId, String parentId);
}