后台服务
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
7.6 KiB

  1. package com.iformall.schedule;
  2. import com.alibaba.fastjson.JSON;
  3. import com.iformall.domain.po.sm.UserMouldVideo;
  4. import com.iformall.domain.po.sm.VideoTrans;
  5. import com.iformall.enums.EnumProject;
  6. import com.iformall.enums.EnumVideoStatus;
  7. import com.iformall.enums.EnumYesOrNo;
  8. import com.iformall.enums.EnumaVideoTransStatus;
  9. import com.iformall.mapper.VideoTransMapper;
  10. import com.iformall.service.project.ProjectFactory;
  11. import com.iformall.service.project.entity.CreateBilling;
  12. import com.iformall.service.sm.UserMouldVideoService;
  13. import com.iformall.utils.DateUtils;
  14. import com.iformall.video.VideoFactory;
  15. import com.iformall.video.entity.VideTransResult;
  16. import com.iformall.video.entity.VideUploadResult;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.context.annotation.Configuration;
  22. import org.springframework.scheduling.annotation.EnableScheduling;
  23. import org.springframework.scheduling.annotation.Scheduled;
  24. import java.util.Date;
  25. import java.util.List;
  26. import java.util.concurrent.Future;
  27. @Configuration
  28. @EnableScheduling
  29. public class VideoSchedule {
  30. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  31. @Autowired
  32. private UserMouldVideoService userMouldVideoService;
  33. @Autowired
  34. private VideoTransMapper videoTransMapper;
  35. @Autowired
  36. VideoFactory videoFactory;
  37. @Autowired
  38. String videoType;
  39. @Autowired
  40. ProjectFactory projectFactory;
  41. /**
  42. * 生成视频
  43. */
  44. @Scheduled(cron = "0 20/30 * * * *?") // 每半小时检查一次
  45. public void userVideoCreateSchedule() {
  46. UserMouldVideo userVideo = new UserMouldVideo();
  47. userVideo.setVideoStartDate(DateUtils.getHourDateBefore(2,new Date()));
  48. userVideo.setVideoStatus(EnumVideoStatus.fail.getCode());
  49. userVideo.setIsDel(EnumYesOrNo.NO.getCode());
  50. List<UserMouldVideo> list = userMouldVideoService.findList(userVideo);
  51. if (list != null && list.size() > 0) {
  52. for (UserMouldVideo video : list) {
  53. try {
  54. Future future = userMouldVideoService.createVideo(video);
  55. future.get();
  56. } catch (Exception e) {
  57. logger.error("TtCouponVideoSchedule error.couponVideoSchedule:"+video.getId(),e);
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * 上传阿里云
  64. */
  65. @Scheduled(cron = "0 */30 * * * *?") // 每半小时检查一次
  66. public void userVideoUploadSchedule() {
  67. List<UserMouldVideo> videos = userMouldVideoService.getNotUploadList();
  68. if (videos != null && videos.size() > 0) {
  69. for (UserMouldVideo video : videos) {
  70. try {
  71. userMouldVideoService.uploadVideo(video);
  72. } catch (Exception e) {
  73. logger.error("TtCouponVideoSchedule error.couponVideoSchedule:"+video.getId(),e);
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * 获取时长和大小
  80. */
  81. @Scheduled(cron = "0 1/5 * * * *?") // 每五分钟检查一次
  82. public void userVideoDetailSchedule() {
  83. List<UserMouldVideo> videos = userMouldVideoService.getNotHaveUrl();
  84. if (videos != null && videos.size() > 0) {
  85. for (UserMouldVideo video : videos) {
  86. try {
  87. VideUploadResult videoDetail = videoFactory.getExcutor(videoType).getVideoDetailWithCache(video.getVideoId(),false);
  88. if (videoDetail.isSuccess()
  89. && StringUtils.isNotBlank(videoDetail.getDuration())
  90. && !"0.0".equals(videoDetail.getDuration())) {
  91. video.setCoverImg(videoDetail.getCoverURL());
  92. video.setVideoPlayUrl(videoDetail.getVideoUrl());
  93. video.setVideoTime(videoDetail.getDuration());
  94. video.setVideoSize(videoDetail.getSize());
  95. video.setVideoStatus(EnumVideoStatus.upload_success.getCode());
  96. video.setVideoMsg("视频上传成功");
  97. video.setUpdateDate(new Date());
  98. //设置扣费,当前方法是慧影项目专用的
  99. CreateBilling cb = projectFactory.getProjectService(EnumProject.PROJECT_2.getCode())
  100. .handleCreateVideoBilling(video.getUserId(), video.getFinalTenantId(), video.getVideoTime(), video.getVideoSize());
  101. if (null != cb) {
  102. logger.info("createBilling result:"+JSON.toJSONString(cb));
  103. video.setCostPoints(cb.getTotalCostPoins());
  104. video.setCostPointsDetail(cb.getDetail());
  105. }
  106. userMouldVideoService.updateById(video);
  107. }
  108. } catch (Exception e) {
  109. logger.error("TtCouponVideoSchedule error.couponVideoSchedule:"+video.getId(),e);
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * 转码
  116. */
  117. @Scheduled(cron = "0 10/30 * * * *?") // 每半小时检查一次
  118. public void videoTransSchedule() {
  119. VideoTrans videoTransQ = new VideoTrans();
  120. videoTransQ.setStatus(EnumaVideoTransStatus.draft.getCode());
  121. List<VideoTrans> videos = videoTransMapper.findList(videoTransQ);
  122. if (videos != null && videos.size() > 0) {
  123. for (VideoTrans video : videos) {
  124. try {
  125. VideTransResult videTransResult = videoFactory.getExcutor(videoType).transVideo(video.getVideoId(), video.getTransTemplateId());
  126. if(videTransResult.isSuccess()){
  127. VideoTrans upd = new VideoTrans();
  128. upd.setId(video.getId());
  129. upd.setTaskId(videTransResult.getTaskId());
  130. upd.setStatus(EnumaVideoTransStatus.ing.getCode());
  131. upd.setMsg("ing");
  132. upd.setUpdateDate(new Date());
  133. videoTransMapper.updateById(upd);
  134. }
  135. } catch (Exception e) {
  136. logger.error("TtCouponVideoSchedule error.couponVideoSchedule:"+video.getId(),e);
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * 转码结果
  143. */
  144. @Scheduled(cron = "0 20/30 * * * *?") // 每半小时检查一次
  145. public void videoTransResultSchedule() {
  146. VideoTrans videoTransQ = new VideoTrans();
  147. videoTransQ.setStatus(EnumaVideoTransStatus.ing.getCode());
  148. List<VideoTrans> videos = videoTransMapper.findList(videoTransQ);
  149. if (videos != null && videos.size() > 0) {
  150. for (VideoTrans video : videos) {
  151. try {
  152. VideTransResult transVideoResult = videoFactory.getExcutor(videoType).getTransVideoResult(video.getVideoId(), video.getTaskId());
  153. VideoTrans upd = new VideoTrans();
  154. upd.setId(video.getId());
  155. if (transVideoResult.isSuccess()){
  156. upd.setStatus(EnumaVideoTransStatus.success.getCode());
  157. upd.setMsg("success");
  158. }else{
  159. upd.setStatus(EnumaVideoTransStatus.fail.getCode());
  160. upd.setMsg(transVideoResult.getMsg());
  161. }
  162. upd.setUpdateDate(new Date());
  163. videoTransMapper.updateById(upd);
  164. } catch (Exception e) {
  165. logger.error("TtCouponVideoSchedule error.couponVideoSchedule:"+video.getId(),e);
  166. }
  167. }
  168. }
  169. }
  170. }