From 654f4eebf519f015506b90d71637e6aad75e5b5c Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期三, 25 六月 2025 14:33:26 +0800
Subject: [PATCH] 视频评论点赞通过mq异步添加数据库,点赞数缓存redis

---
 framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoCommentServiceImpl.java |   99 +++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 78 insertions(+), 21 deletions(-)

diff --git a/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoCommentServiceImpl.java b/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoCommentServiceImpl.java
index fecee7f..6d4047b 100644
--- a/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoCommentServiceImpl.java
+++ b/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoCommentServiceImpl.java
@@ -6,11 +6,16 @@
 import cn.lili.common.security.AuthUser;
 import cn.lili.common.security.context.UserContext;
 import cn.lili.common.sensitive.SensitiveWordsFilter;
+import cn.lili.modules.lmk.constant.RedisKeyExpireConstant;
 import cn.lili.modules.lmk.domain.entity.ThumbsUpRecord;
 import cn.lili.modules.lmk.domain.form.ThumbsUpRecordForm;
 import cn.lili.modules.lmk.domain.vo.CollectTypeNumVO;
 import cn.lili.modules.lmk.enums.general.VideoCommentStatusEnum;
 import cn.lili.modules.lmk.service.ThumbsUpRecordService;
+import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
+import cn.lili.rocketmq.tags.CommentTagsEnum;
+import cn.lili.rocketmq.tags.OrderTagsEnum;
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import cn.lili.modules.lmk.domain.entity.VideoComment;
 import cn.lili.modules.lmk.mapper.VideoCommentMapper;
@@ -22,7 +27,10 @@
 import cn.lili.modules.lmk.domain.form.VideoCommentForm;
 import cn.lili.modules.lmk.domain.vo.VideoCommentVO;
 import cn.lili.modules.lmk.domain.query.VideoCommentQuery;
+import org.apache.commons.collections4.ListUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.rocketmq.client.producer.DefaultMQProducer;
+import org.apache.rocketmq.common.MixAll;
 import org.apache.rocketmq.spring.core.RocketMQTemplate;
 import org.springframework.stereotype.Service;
 import lombok.RequiredArgsConstructor;
@@ -176,40 +184,75 @@
 
     @Override
     public Result thumbsUp(ThumbsUpRecordForm form) {
-        boolean exists = new LambdaQueryChainWrapper<>(thumbsUpRecordService.getBaseMapper())
-                .eq(ThumbsUpRecord::getRefId, form.getRefId())
-                .eq(ThumbsUpRecord::getUserId, UserContext.getCurrentUserId())
-                .exists();
-        if (exists) {
-            return Result.ok();
-        }
         ThumbsUpRecord record = ThumbsUpRecordForm.getEntityByForm(form, null);
         record.setUserId(UserContext.getCurrentUserId());
-        thumbsUpRecordService.save(record);
-        if (cache.exist(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(record.getRefId()))) {
-            cache.incr(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(record.getRefId()));
-        } else {
-            VideoComment comment = baseMapper.selectById(form.getRefId());
-            if (Objects.nonNull(comment)) {
-                cache.put(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(comment.getId()), comment.getThumbsUpNum() + 1, EXPIRE_TIME, TimeUnit.DAYS);
-            }
-        }
-        // TODO 浣跨敤rocketmq寮傛鍐欏叆鍒癿ysql涓�
-//        rocketMQTemplate.asyncSend();
+
+        // 璧癿q寮傛澶勭悊
+        String destination = rocketmqCustomProperties.getCommentTopic() + ":" + CommentTagsEnum.THUMBS_UP.name();
+        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(record), RocketmqSendCallbackBuilder.commonCallback());
         return Result.ok();
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void mqThumbsUp(ThumbsUpRecord record) {
+        boolean exists = new LambdaQueryChainWrapper<>(thumbsUpRecordService.getBaseMapper())
+                .eq(ThumbsUpRecord::getRefId, record.getRefId())
+                .eq(ThumbsUpRecord::getUserId, UserContext.getCurrentUserId())
+                .exists();
+        if (exists) {
+            return;
+        }
+        thumbsUpRecordService.save(record);
+        VideoComment comment = this.getById(record.getRefId());
+        if (cache.exist(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(record.getRefId()))) {
+            cache.incr(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(record.getRefId()));
+        } else {
+            if (Objects.nonNull(comment)) {
+                cache.put(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(comment.getId()), comment.getThumbsUpNum() + 1, RedisKeyExpireConstant.COMMENT_NUM_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY);
+            }
+        }
+        // 鏍囪瘑璇ヨ瘎璁洪渶瑕侀�氳繃瀹氭椂浠诲姟缁熻鐐硅禐鏁�
+        if (Objects.nonNull(comment) && ! comment.getThumbsUpJob()) {
+            new LambdaUpdateChainWrapper<>(baseMapper)
+                    .eq(VideoComment::getId, comment.getId())
+                    .set(VideoComment::getThumbsUpJob, Boolean.TRUE)
+                    .update();
+        }
+    }
+
+    @Override
     public Result cancelThumbsUp(ThumbsUpRecordForm form) {
+        // 璧癿q寮傛澶勭悊
+        String destination = rocketmqCustomProperties.getCommentTopic() + ":" + CommentTagsEnum.CANCEL_THUMBS_UP.name();
+        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(form), RocketmqSendCallbackBuilder.commonCallback());
+        return Result.ok();
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void mqCancelThumbsUp(ThumbsUpRecordForm form) {
         new LambdaUpdateChainWrapper<>(thumbsUpRecordService.getBaseMapper())
                 .eq(ThumbsUpRecord::getRefId, form.getRefId())
                 .eq(ThumbsUpRecord::getThumbsUpType, form.getThumbsUpType())
                 .eq(ThumbsUpRecord::getUserId, UserContext.getCurrentUserId())
                 .remove();
         // redis鏁伴噺鍑忎竴
-        cache.decr(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(form.getRefId()));
-        // TODO mq寮傛鍚屾鍒癿ysql
-        return Result.ok();
+        VideoComment comment = this.getById(form.getRefId());
+        if (cache.exist(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(form.getRefId()))) {
+            cache.decr(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(form.getRefId()));
+        } else {
+            if (Objects.nonNull(comment)) {
+                cache.put(CachePrefix.VIDEO_COMMENT_LIKE_NUM.getPrefixWithId(comment.getId()), comment.getThumbsUpNum() - 1, RedisKeyExpireConstant.COMMENT_NUM_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY);
+            }
+        }
+        // 鏍囪瘑璇ヨ瘎璁洪渶瑕侀�氳繃瀹氭椂浠诲姟缁熻鐐硅禐鏁�
+        if (Objects.nonNull(comment) && ! comment.getThumbsUpJob()) {
+            new LambdaUpdateChainWrapper<>(baseMapper)
+                    .eq(VideoComment::getId, comment.getId())
+                    .set(VideoComment::getThumbsUpJob, Boolean.TRUE)
+                    .update();
+        }
     }
 
     /**
@@ -228,4 +271,18 @@
         }
         return Long.valueOf((Integer) redisNum);
     }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void updateCommentThumbsUpNumBatch(List<CollectTypeNumVO> numList) {
+        // 鎸�500鏉℃暟鎹繘琛屾媶鍒�
+        List<List<CollectTypeNumVO>> chunks = ListUtils.partition(numList, 500);
+        for (List<CollectTypeNumVO> chunk : chunks) {
+            baseMapper.updateCommentThumbsUpNumBatch(chunk);
+            new LambdaUpdateChainWrapper<>(baseMapper)
+                    .in(VideoComment::getId, chunk.stream().map(CollectTypeNumVO::getId).collect(Collectors.toList()))
+                    .set(VideoComment::getThumbsUpJob, Boolean.FALSE)
+                    .update();
+        }
+    }
 }

--
Gitblit v1.8.0