教育项目--课程模块--增删查改【29】

课程模块



前言


提示:以下是本篇文章正文内容,下面案例可供参考

一、课程列表

进行分类展示

Service

package com.djr.eduservice.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.djr.eduservice.entity.EduSubject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.djr.eduservice.entity.vo.EduSubjectVo;
import com.djr.eduservice.entity.vo.SubjectNestedVo;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 服务类
 * </p>
 *
 * @author taotao
 * @since 2021-01-29
 */
public interface EduSubjectService extends IService<EduSubject> {

    /**
     * 通过读取excel的方式进行添加课程
     *
     * @param file              数据文件
     * @param eduSubjectService service
     */
    void saveSubject(MultipartFile file, EduSubjectService eduSubjectService);

    /**
     * 嵌套返回课程的数据
     *
     * @return 课程集合
     */
    List<SubjectNestedVo> nestedList();

    /**
     * 条件查询
     *
     * @return 课程集合
     */
    IPage<EduSubject> getPageWapper(Long page, Long limit, EduSubjectVo eduSubjectVo) ;

    /**
     * 递归方式进行查询全部
     * @return
     */
    List<SubjectNestedVo> getSubjectList();
}

service Impl

package com.djr.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.djr.eduservice.config.excel.ExcelSubjectData;
import com.djr.eduservice.config.excel.SubjectExcelListener;
import com.djr.eduservice.entity.EduSubject;
import com.djr.eduservice.entity.vo.EduSubjectVo;
import com.djr.eduservice.entity.vo.SubjectNestedVo;
import com.djr.eduservice.entity.vo.SubjectVo;
import com.djr.eduservice.mapper.EduSubjectMapper;
import com.djr.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.djr.servicebase.exceptionhandler.MyselfException;
import io.swagger.annotations.ApiParam;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.Subject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author taotao
 * @since 2021-01-29
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 通过读取excel的方式进行添加课程
     * 添加课程分类
     * poi读取excel内容
     *
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            //1 获取文件输入流
            InputStream inputStream = file.getInputStream();

            // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream,
                    ExcelSubjectData.class,
                    new SubjectExcelListener(eduSubjectService))
                    .sheet()
                    .doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyselfException(50001, "添加课程分类失败");
        }
    }

    /**
     * 嵌套返回课程的数据
     *
     * @return 课程集合
     */
    @Override
    public List<SubjectNestedVo> nestedList() {

        //最终要的到的数据列表
        ArrayList<SubjectNestedVo> subjectNestedVoArrayList = new ArrayList<>();


        //获取父级菜单的集合
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        queryWrapper.orderByAsc("sort", "id");

        //拿到所有的顶级父类的集合
        List<EduSubject> eduSubjects = baseMapper.selectList(queryWrapper);

        //开始遍历拿到的父类的集合
        for (EduSubject eduSubject : eduSubjects) {
            //创建一个父类的返回对象
            SubjectNestedVo subjectNestedVo = new SubjectNestedVo();

            //根据父类的id分别拿取其子类的集合
            List<EduSubject> eduSubjectSunList = this.baseMapper.selectList(new QueryWrapper<EduSubject>().eq("parent_id", eduSubject.getId()));
            //如果其子类为空则直接跳出,继续下一次循环
            if (eduSubjectSunList.isEmpty()) {
                break;
            }

            //创建一个子类返回对象的集合  ---一会放到父类返回对象的集合属性中
            List<SubjectVo> subjectVos = new ArrayList<>();
            //遍历子类集合
            for (EduSubject subject : eduSubjectSunList) {
                //创建一个子类返回对象
                SubjectVo subjectVo = new SubjectVo();
                //对象的置换
                BeanUtils.copyProperties(subject, subjectVo);

                //添加到子类返回对象的集合中
                subjectVos.add(subjectVo);
            }

            subjectNestedVo.setId(eduSubject.getId());
            subjectNestedVo.setTitle(eduSubject.getTitle());
            subjectNestedVo.setChildren(subjectVos);
            //父类的放入需要返回的集合中
            subjectNestedVoArrayList.add(subjectNestedVo);

        }


        return subjectNestedVoArrayList;
    }


    /**
     * 封装QueryWrapper的方法
     *
     * @param eduSubjectVo
     * @return
     */
    public QueryWrapper<EduSubject> getQueryWapper(EduSubjectVo eduSubjectVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (eduSubjectVo == null) {
            return new QueryWrapper<EduSubject>();
        }
        String begin = eduSubjectVo.getBegin();
        String end = eduSubjectVo.getEnd();
        String title = eduSubjectVo.getTitle();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_modified", end);
        }
        return queryWrapper;
    }

    /**
     * 返回分页对象
     * @param page
     * @param limit
     * @param eduSubjectVo
     * @return
     */
    @Override
    public IPage<EduSubject> getPageWapper(Long page, Long limit, EduSubjectVo eduSubjectVo) {

        Page<EduSubject> eduSubjectPage = new Page<>(page, limit);

        QueryWrapper<EduSubject> queryWapper = this.getQueryWapper(eduSubjectVo);

        IPage<EduSubject> eduSubjectIPage = this.page(eduSubjectPage, queryWapper);

        return eduSubjectIPage;
    }

    /**
     * 获取顶级分类
     * @return
     */
    @Override
    public List<SubjectNestedVo> getSubjectList() {
        List<EduSubject> eduSubjects = getParentId("0");
        return getTreeList(eduSubjects);
    }

    /**
     * 获取子分类
     * @param parentId 其父类的ID
     * @return 参数parentId下的所有子类的集合
     */
    List<EduSubject> getParentId(String parentId){
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("parent_id",parentId);
        List<EduSubject> eduSubjects = baseMapper.selectList(queryWrapper);
        return eduSubjects;
    }

    /**
     * 获取数据
     * @param eduSubjects  子类的集合
     * @return
     */
    List<SubjectNestedVo> getTreeList(List<EduSubject> eduSubjects){

        //创建返回的父类的集合【包括其子类的集合属性】
        List<SubjectNestedVo> subjectNestedVoList = new ArrayList<>();
        //遍历拿到的子类的集合
        for (EduSubject eduSubject : eduSubjects) {
            //创建一个结果父类
            SubjectNestedVo subjectNestedVo = new SubjectNestedVo();

            subjectNestedVo.setId(eduSubject.getId());
            subjectNestedVo.setTitle(eduSubject.getTitle());
            subjectNestedVo.setChildren(getTreeList(getParentId(eduSubject.getId())));
            subjectNestedVoList.add(subjectNestedVo);
        }
        return subjectNestedVoList;
    }
}

controller

package com.djr.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.djr.commonutils.R;
import com.djr.eduservice.entity.EduSubject;
import com.djr.eduservice.entity.vo.EduSubjectVo;
import com.djr.eduservice.entity.vo.SubjectNestedVo;
import com.djr.eduservice.service.EduSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author taotao
 * @since 2021-01-29
 */
@Api(description = "课程模块")
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu-subject")
public class EduSubjectController {
    @Autowired
    EduSubjectService eduSubjectService;

    /**
     * 添加课程分类
     * 获取上传文件,文件内容进行读取
     */
    @ApiOperation(value = "Excel批量导入")
    @PostMapping("/addSubject")
    public R addSubject(MultipartFile file) {

        eduSubjectService.saveSubject(file, eduSubjectService);

        return R.ok();
    }

    /**
     * 嵌套数据课程列表
     * @return
     */
    @ApiOperation(value = "嵌套数据课程列表")
    @GetMapping()
    public R getSubject() {

        List<SubjectNestedVo> subjectNestedVoList = eduSubjectService.nestedList();
        return R.ok().data("items", subjectNestedVoList);
    }

    /**
     *  条件查询课程
     * @param page
     * @param limit
     * @param eduSubjectVo
     * @return
     */
    @ApiOperation(value = "条件查询课程")
    @PostMapping("/PaginationCondition/{page}/{limit}")
    public R getSubject(@ApiParam(name = "page", value = "当前页码", required = true)
                        @PathVariable Long page,

                        @ApiParam(name = "limit", value = "每页记录数", required = true)
                        @PathVariable Long limit,

                        @ApiParam(name = "eduTeacherVO", value = "查询对象")
                        @RequestBody(required = false) EduSubjectVo eduSubjectVo) {

        IPage<EduSubject> pageWapper = eduSubjectService.getPageWapper(page, limit, eduSubjectVo);
        return R
                .ok()
                .data("items", pageWapper.getRecords())
                .data("total",pageWapper.getTotal());
    }


    /**
     * 递归方式查全部
     * @return
     */
    @ApiOperation(value = "递归方式查询课程")
    @PostMapping("/getSubjectList")
    public R getSubjectList() {

        List<SubjectNestedVo> subjectList = eduSubjectService.getSubjectList();
        return R
                .ok()
                .data("items", subjectList);

    }
}

 

二、添加课程

1.进行分析

edu_teacher 讲师表

edu_subject 课程表

edu_chapter 章节表

edu_course 课程表 包含的是某一个课程中的详细信息

edu_course_description 课程表中的简介信息【以文件的形式进行存储】

edu_video 章节表中某一个章节的具体信息

在这里插入图片描述

以上是这六个表之间的关系。

2.自动生成

"edu_course","edu_course_description","edu_chapter","edu_video"

生成之后的edu_course_description的controller不需要可以删掉,因为这个简介表我们直接在课程内容中就可以处理了。

3.创建Vo实体类

这个VO类用来封装表单数据

package com.djr.eduservice.entity.vo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @Program: science_source_education
 * @Description
 * @Author: 涛涛 * ^ *
 * @Create: 2021-02-01 16:20
 **/
@Data
@ApiModel(value = "课程基本信息", description = "编辑课程基本信息的表单对象")
public class CourseInfoVo implements Serializable {


    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "课程ID")
    private String id;

    @ApiModelProperty(value = "课程讲师ID")
    private String teacherId;

    @ApiModelProperty(value = "课程专业ID")
    private String subjectId;

    @ApiModelProperty(value = "课程标题")
    private String title;

    /**
     * 价格0.01的问题 使用BigDecimal类型
     */
    @ApiModelProperty(value = "课程销售价格,设置为0则可免费观看")
    private BigDecimal price;

    @ApiModelProperty(value = "总课时")
    private Integer lessonNum;

    @ApiModelProperty(value = "课程封面图片路径")
    private String cover;

    @ApiModelProperty(value = "课程简介")
    private String description;


}

4.简介表–设置简介表ID的生成

首先我们传入的Vo类中有一个属性是在这里插入图片描述

但是呢如果生成的话呢 这个description类中的ID也是自动生成的,做不到与新增的课程数据的ID保持一直,所以进行两个ID之间的一直

4.1 修改description类的ID生成

在这里插入图片描述
修改为ID手动设置而不自动生成

4.2 实现类中进行ID的赋值

        //添加课程简介-->课程简介表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //将入参的这个简介对象传给这个对象
        eduCourseDescription.setDescription(courseInfoVo.getDescription());

        String id = eduCourse.getId();
        //解决一一对象的ID一致的问题
        eduCourseDescription.setId(id);

        boolean save = eduCourseDescriptionService.save(eduCourseDescription);

        if (!save) {
            throw new MyselfException(50003, "课程添加失败");
        }




    }
}

5.简介表–设置新增课程的父分类

    //通过传入的课程专业ID获取他的父ID
        QueryWrapper<EduSubject> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",courseInfoVo.getSubjectId());
        EduSubject one = eduSubjectService.getOne(queryWrapper);
        eduCourse.setSubjectParentId(one.getParentId());

6.简介表–实现类完整代码

package com.djr.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.djr.eduservice.entity.EduCourse;
import com.djr.eduservice.entity.EduCourseDescription;
import com.djr.eduservice.entity.EduSubject;
import com.djr.eduservice.entity.vo.CourseInfoVo;
import com.djr.eduservice.mapper.EduCourseMapper;
import com.djr.eduservice.service.EduCourseDescriptionService;
import com.djr.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.djr.eduservice.service.EduSubjectService;
import com.djr.servicebase.exceptionhandler.MyselfException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author taotao
 * @since 2021-02-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    @Autowired
    EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    EduSubjectService eduSubjectService;

    /**
     * 添加课程信息
     *
     * @param courseInfoVo
     */
    @Override
    public void insertCorse(CourseInfoVo courseInfoVo) {
        //添加课程信息-->课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);

        //通过传入的课程专业ID获取他的父ID
        QueryWrapper<EduSubject> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",courseInfoVo.getSubjectId());
        EduSubject one = eduSubjectService.getOne(queryWrapper);
        eduCourse.setSubjectParentId(one.getParentId());

        //返回sql中影响的行数
        int insert = baseMapper.insert(eduCourse);

        if (insert <= 0) {
            throw new MyselfException(50003, "课程添加失败");
        }
        //添加课程简介-->课程简介表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //将入参的这个简介对象传给这个对象
        eduCourseDescription.setDescription(courseInfoVo.getDescription());

        String id = eduCourse.getId();
        //解决一一对象的ID一致的问题
        eduCourseDescription.setId(id);

        boolean save = eduCourseDescriptionService.save(eduCourseDescription);

        if (!save) {
            throw new MyselfException(50003, "课程添加失败");
        }
    }
}

7.简介表–测试

在这里插入图片描述
在这里插入图片描述

8.课程封面–文件上传

/**
	 * 文件上传
	 *
	 * @param file
	 */
@ApiOperation(value = "文件上传")
@PostMapping("upload")
public R upload(
    @ApiParam(name = "file", value = "文件", required = true)
    @RequestParam("file") MultipartFile file,

    @ApiParam(name = "host", value = "文件上传路径", required = false)) {

    String uploadUrl = fileService.upload(file);
    //返回r对象
    return R.ok().message("文件上传成功").data("url", uploadUrl);

}

这里调用的是之前项目中的oss模块的内容文件上传

9.课程封面–测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小七会喷火

小七想要bi

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值