需求
删除课程的同时删除云端视频

一、vod服务

1、业务

业务接口:VideoService.java

  1. void removeVideoList(List<String> videoIdList);

业务实现:VideoServiceImpl.java

  1. @Override
  2. public void removeVideoList(List<String> videoIdList) {
  3. try {
  4. //初始化
  5. DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
  6. ConstantPropertiesUtil.ACCESS_KEY_ID,
  7. ConstantPropertiesUtil.ACCESS_KEY_SECRET);
  8. //创建请求对象
  9. //一次只能批量删20个
  10. String str = org.apache.commons.lang.StringUtils.join(videoIdList.toArray(), ",");
  11. DeleteVideoRequest request = new DeleteVideoRequest();
  12. request.setVideoIds(str);
  13. //获取响应
  14. DeleteVideoResponse response = client.getAcsResponse(request);
  15. System.out.print("RequestId = " + response.getRequestId() + "\n");
  16. } catch (ClientException e) {
  17. throw new GuliException(20001, "视频删除失败");
  18. }
  19. }

2、web层接口

controller:VideoAdminController.java

  1. /**
  2. * 批量删除视频
  3. * @param videoIdList
  4. * @return
  5. */
  6. @DeleteMapping("delete-batch")
  7. public R removeVideoList(
  8. @ApiParam(name = "videoIdList", value = "云端视频id", required = true)
  9. @RequestParam("videoIdList") List videoIdList){
  10. videoService.removeVideoList(videoIdList);
  11. return R.ok().message("视频删除成功");
  12. }

3、Swagger测试

输入多个id,每个一行
image.png

二、edu服务

1、client

VodClient.java

  1. @DeleteMapping(value = "/admin/vod/video/delete-batch")
  2. public R removeVideoList(@RequestParam("videoIdList") List<String> videoIdList);

2、业务

VideoServiceImpl.java

  1. @Override
  2. public boolean removeByCourseId(String courseId) {
  3. //根据课程id查询所有视频列表
  4. QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
  5. queryWrapper.eq("course_id", courseId);
  6. queryWrapper.select("video_source_id");
  7. List<Video> videoList = baseMapper.selectList(queryWrapper);
  8. //得到所有视频列表的云端原始视频id
  9. List<String> videoSourceIdList = new ArrayList<>();
  10. for (int i = 0; i < videoList.size(); i++) {
  11. Video video = videoList.get(i);
  12. String videoSourceId = video.getVideoSourceId();
  13. if(!StringUtils.isEmpty(videoSourceId)){
  14. videoSourceIdList.add(videoSourceId);
  15. }
  16. }
  17. //调用vod服务删除远程视频
  18. if(videoSourceIdList.size() > 0){
  19. vodClient.removeVideoList(videoSourceIdList);
  20. }
  21. //删除video表示的记录
  22. QueryWrapper<Video> queryWrapper2 = new QueryWrapper<>();
  23. queryWrapper2.eq("course_id", courseId);
  24. Integer count = baseMapper.delete(queryWrapper2);
  25. return null != count && count > 0;
  26. }

CourseServiceImpl.java

  1. @Override
  2. public boolean removeCourseById(String id) {
  3. //根据id删除所有视频
  4. videoService.removeByCourseId(id);
  5. //根据id删除所有章节
  6. chapterService.removeByCourseId(id);
  7. //根据id删除所有课程详情
  8. courseDescriptionService.removeById(id);
  9. //删除封面 TODO 独立完成
  10. Integer result = baseMapper.deleteById(id);
  11. return null != result && result > 0;
  12. }