Explorar o código

发布 VideoPost 时同步 VideoCategoryPost

reghao hai 1 ano
pai
achega
dcc4c93ce9

+ 4 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/db/mapper/VideoCategoryPostMapper.java

@@ -5,6 +5,7 @@ import cn.reghao.jutil.jdk.db.Page;
 import cn.reghao.tnb.content.app.vod.model.po.VideoCategoryPost;
 import cn.reghao.tnb.content.app.vod.model.query.VideoQuery;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
@@ -14,6 +15,9 @@ import java.util.List;
  */
 @Mapper
 public interface VideoCategoryPostMapper extends BaseMapper<VideoCategoryPost> {
+    void updateVideoCategoryPost(VideoCategoryPost videoCategoryPost);
+
     int countByCriteria(VideoQuery videoQuery);
     List<VideoCategoryPost> findVideoCategoryPostByPage(Page page, VideoQuery videoQuery);
+    VideoCategoryPost findByCategoryIdAndUserId(@Param("categoryId") int categoryId, @Param("userId") long userId);
 }

+ 32 - 1
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/impl/VideoPostServiceImpl.java

@@ -45,16 +45,18 @@ public class VideoPostServiceImpl implements VideoPostService {
     private final VideoPostTagMapper videoPostTagMapper;
     private final IdGenerator idGenerator;
     private final VideoRepository videoRepository;
+    private final VideoCategoryPostMapper videoCategoryPostMapper;
 
     public VideoPostServiceImpl(VideoPostMapper videoPostMapper, VideoStatisticMapper videoStatisticMapper,
                                 VideoTagMapper videoTagMapper, VideoPostTagMapper videoPostTagMapper,
-                                VideoRepository videoRepository) {
+                                VideoRepository videoRepository, VideoCategoryPostMapper videoCategoryPostMapper) {
         this.videoPostMapper = videoPostMapper;
         this.videoStatisticMapper = videoStatisticMapper;
         this.videoTagMapper = videoTagMapper;
         this.videoPostTagMapper = videoPostTagMapper;
         this.idGenerator = new IdGenerator("video-id");
         this.videoRepository = videoRepository;
+        this.videoCategoryPostMapper = videoCategoryPostMapper;
     }
 
     public Result publishVideo(VideoPublishSbt videoPublishSbt) {
@@ -141,6 +143,7 @@ public class VideoPostServiceImpl implements VideoPostService {
     public void saveVideo(VideoFile videoFile, VideoPost videoPost, String tags) {
         String videoId = videoPost.getVideoId();
         VideoStatistic videoStatistic = new VideoStatistic(videoId);
+        setVideoCategoryPost(videoPost);
         videoRepository.savePostVideo(videoFile, videoPost, videoStatistic);
         setVideoTags(videoId, tags);
     }
@@ -148,12 +151,40 @@ public class VideoPostServiceImpl implements VideoPostService {
     @Deprecated
     @Transactional(rollbackFor = Exception.class)
     public void saveVideo(VideoPost videoPost, VideoStatistic videoStatistic, String tags) {
+        int categoryId = videoPost.getCategoryId();
+        long publishBy = videoPost.getPublishBy();
+        VideoCategoryPost videoCategoryPost = videoCategoryPostMapper.findByCategoryIdAndUserId(categoryId, publishBy);
+        if (videoCategoryPost == null) {
+            videoCategoryPost = new VideoCategoryPost(videoPost);
+            videoCategoryPostMapper.save(videoCategoryPost);
+        } else {
+            videoCategoryPost.setVideoId(videoPost.getVideoId());
+            videoCategoryPost.setScope(videoPost.getScope());
+            videoCategoryPost.setPublishAt(videoPost.getPublishAt());
+            videoCategoryPostMapper.updateVideoCategoryPost(videoCategoryPost);
+        }
+
         String videoId = videoPost.getVideoId();
         videoPostMapper.save(videoPost);
         videoStatisticMapper.save(videoStatistic);
         setVideoTags(videoId, tags);
     }
 
+    private void setVideoCategoryPost(VideoPost videoPost) {
+        int categoryId = videoPost.getCategoryId();
+        long publishBy = videoPost.getPublishBy();
+        VideoCategoryPost videoCategoryPost = videoCategoryPostMapper.findByCategoryIdAndUserId(categoryId, publishBy);
+        if (videoCategoryPost == null) {
+            videoCategoryPost = new VideoCategoryPost(videoPost);
+            videoCategoryPostMapper.save(videoCategoryPost);
+        } else {
+            videoCategoryPost.setVideoId(videoPost.getVideoId());
+            videoCategoryPost.setScope(videoPost.getScope());
+            videoCategoryPost.setPublishAt(videoPost.getPublishAt());
+            videoCategoryPostMapper.updateVideoCategoryPost(videoCategoryPost);
+        }
+    }
+
     private void setVideoTags(String videoId, String tags) {
         if (tags.isBlank()) {
             return;

+ 8 - 3
content/content-service/src/main/resources/mapper/vod/VideoPostCategoryMapper.xml

@@ -17,10 +17,10 @@
         </foreach>
     </insert>
 
-    <update id="updateVideoScope">
+    <update id="updateVideoCategoryPost">
         update vod_video_category_post
-        set update_time=now(),scope=#{scope}
-        where video_id=#{videoId}
+        set update_time=now(),video_id=#{videoId},scope=#{scope},publish_at=#{publishAt}
+        where category_id=#{categoryId} and publish_by=#{publishBy}
     </update>
 
     <delete id="deleteByVideoId">
@@ -76,4 +76,9 @@
         </where>
         order by post.publish_at desc
     </select>
+    <select id="findByCategoryIdAndUserId" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoCategoryPost">
+        select *
+        from vod_video_category_post post
+        where category_id=#{categoryId} and publish_by=#{userId}
+    </select>
 </mapper>