From c1125c45cc9171a1848ef4caa976e77eceea3215 Mon Sep 17 00:00:00 2001
From: peng <peng.com>
Date: 星期三, 02 七月 2025 10:39:34 +0800
Subject: [PATCH] insert 覆盖模式添加购物车
---
framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 108 insertions(+), 3 deletions(-)
diff --git a/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java b/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java
index ca75179..116f4f6 100644
--- a/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java
+++ b/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java
@@ -1,6 +1,9 @@
package cn.lili.modules.lmk.service.impl;
+import cn.lili.cache.Cache;
+import cn.lili.cache.CachePrefix;
import cn.lili.common.security.context.UserContext;
+import cn.lili.modules.lmk.constant.RedisKeyExpireConstant;
import cn.lili.modules.lmk.domain.entity.*;
import cn.lili.modules.lmk.domain.form.*;
import cn.lili.modules.lmk.domain.query.*;
@@ -57,6 +60,7 @@
private final KitchenVideoTypeRefService kitchenVideoTypeRefService;
private final VideoGoodsService videoGoodsService;
private final KitchenTypeService kitchenTypeService;
+ private final Cache cache;
/**
* 娣诲姞
@@ -135,6 +139,11 @@
vo.setVideoUrl(cosUtil.getPreviewUrl(vo.getVideoFileKey()));
} else if (VideoContentTypeEnum.IMG.getValue().equals(vo.getVideoContentType()) && StringUtils.isNotBlank(vo.getVideoImgs())) {
vo.setImgs(JSON.parseArray(vo.getVideoImgs(), String.class).stream().map(fileKey -> cosUtil.getPreviewUrl(fileKey)).collect(Collectors.toList()));
+ }
+ if (CollectionUtils.isNotEmpty(vo.getGoodsList())) {
+ vo.getGoodsList().stream().forEach(goods -> {
+ goods.setThumbnail(cosUtil.getPreviewUrl(goods.getThumbnail()));
+ });
}
return Result.ok().data(vo);
}
@@ -389,17 +398,57 @@
page.getRecords().forEach(v -> {
v.setTagList(tagMap.get(v.getId()));
v.setCollected(CollectionUtils.isNotEmpty(collectMap.get(v.getId())));
+ v.setCommentNum(this.getCommentNum(v.getId(), v.getCommentNum()));
+ v.setCollectNum(this.getCollectNum(v.getId(), v.getCollectNum()));
if (VideoContentTypeEnum.VIDEO.getValue().equals(v.getVideoContentType())) {
v.setVideoUrl(cosUtil.getPreviewUrl(v.getVideoFileKey()));
v.setCoverUrl(cosUtil.getPreviewUrl(v.getCoverFileKey()));
} else if (VideoContentTypeEnum.IMG.getValue().equals(v.getVideoContentType()) && StringUtils.isNotBlank(v.getVideoImgs())) {
v.setImgs(JSON.parseArray(v.getVideoImgs(), String.class).stream().map(fileKey -> cosUtil.getPreviewUrl(fileKey)).collect(Collectors.toList()));
}
-
+ if (CollectionUtils.isNotEmpty(v.getGoodsList())) {
+ v.getGoodsList().stream().forEach(goods -> {
+ goods.setThumbnail(cosUtil.getPreviewUrl(goods.getThumbnail()));
+ });
+ }
v.setSubscribeThisAuthor(subscribes.contains(v.getAuthorId()));
});
}
return Result.ok().data(page.getRecords());
+ }
+
+ /**
+ * 浠巖edis涓幏鍙栬瘎璁烘暟閲忥紝濡傛灉redis涓病鏈夊垯灏唌ysql涓殑鏁伴噺鍐欏叆鍒皉edis
+ *
+ * @param videoId
+ * @param mysqlNum
+ * @return
+ */
+ private Integer getCommentNum(String videoId, Integer mysqlNum) {
+ Object redisNum = cache.get(CachePrefix.VIDEO_COMMENT_NUM.getPrefixWithId(videoId));
+ if (Objects.isNull(redisNum)) {
+ // redis涓病鏈夊氨鎶婃暟鎹簱鐨勫啓鍒皉edis涓�
+ cache.put(CachePrefix.VIDEO_COMMENT_NUM.getPrefixWithId(videoId), mysqlNum, RedisKeyExpireConstant.COMMENT_NUM_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY);
+ return mysqlNum;
+ }
+ return (Integer) redisNum;
+ }
+
+ /**
+ * 浠巖edis涓幏鍙栨敹钘忔暟閲忥紝濡傛灉redis涓病鏈夊垯灏唌ysql涓殑鏁伴噺鍐欏叆鍒皉edis
+ *
+ * @param videoId
+ * @param mysqlNum
+ * @return
+ */
+ private Integer getCollectNum(String videoId, Integer mysqlNum) {
+ Object redisNum = cache.get(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(videoId));
+ if (Objects.isNull(redisNum)) {
+ // redis涓病鏈夊氨鎶婃暟鎹簱鐨勫啓鍒皉edis涓�
+ cache.put(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(videoId), mysqlNum, RedisKeyExpireConstant.COLLECT_NUM_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY);
+ return mysqlNum;
+ }
+ return (Integer) redisNum;
}
@Override
@@ -446,6 +495,10 @@
List<List<CollectTypeNumVO>> chunks = ListUtils.partition(numList, 500);
for (List<CollectTypeNumVO> chunk : chunks) {
baseMapper.updateCollectNumBatch(chunk);
+ new LambdaUpdateChainWrapper<>(baseMapper)
+ .in(Video::getId, chunk.stream().map(CollectTypeNumVO::getId).collect(Collectors.toList()))
+ .set(Video::getCollectNumJob, Boolean.FALSE)
+ .update();
}
}
@@ -456,6 +509,10 @@
List<List<CollectTypeNumVO>> chunks = ListUtils.partition(numList, 500);
for (List<CollectTypeNumVO> chunk : chunks) {
baseMapper.updateCommentNumBatch(chunk);
+ new LambdaUpdateChainWrapper<>(baseMapper)
+ .in(Video::getId, chunk.stream().map(CollectTypeNumVO::getId).collect(Collectors.toList()))
+ .set(Video::getCommentNumJob, Boolean.FALSE)
+ .update();
}
}
@@ -523,6 +580,9 @@
@Override
public Result getGoodsDetail(String videoId) {
List<VideoGoodsDetailVO> goodsList = baseMapper.getVideoGoods(videoId);
+ goodsList.stream().forEach(goods -> {
+ goods.setThumbnail(cosUtil.getPreviewUrl(goods.getThumbnail()));
+ });
return Result.ok().data(goodsList);
}
@@ -573,7 +633,7 @@
video.setAuthorId(UserContext.getCurrentUserId());
video.setVideoType(VideoTypeEnum.HEALTH.getValue());
//璁剧疆濉厖妯″紡 淇濇寔姣斾緥锛屽畬鏁存樉绀�
- video.setVideoFit("contain");
+ video.setVideoFit(form.getVideoFit());
video.setVideoContentType(VideoContentTypeEnum.VIDEO.getValue());
video.setStatus(VideoStatusEnum.PUBLISHED.getValue());
baseMapper.insert(video);
@@ -630,7 +690,7 @@
video.setAuthorId(UserContext.getCurrentUserId());
video.setVideoType(VideoTypeEnum.COOK.getValue());
//璁剧疆濉厖妯″紡 淇濇寔姣斾緥锛屽畬鏁存樉绀�
- video.setVideoFit("contain");
+ video.setVideoFit(form.getVideoFit());
video.setVideoContentType(VideoContentTypeEnum.VIDEO.getValue());
video.setStatus(VideoStatusEnum.PUBLISHED.getValue());
baseMapper.insert(video);
@@ -709,4 +769,49 @@
.eq(KitchenVideoTypeRef::getVideoId, id));
return Result.ok("鍒犻櫎鎴愬姛");
}
+
+ /**
+ * mq鎵ц瑙嗛鐨勬敹钘�/鍙栨秷鏀惰棌
+ *
+ * @param collect
+ */
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void mqCollectChange(MyCollect collect) {
+ MyCollect myCollect = new LambdaQueryChainWrapper<>(myCollectService.getBaseMapper())
+ .eq(MyCollect::getCollectType, collect.getCollectType())
+ .eq(MyCollect::getRefId, collect.getRefId())
+ .eq(MyCollect::getUserId, collect.getUserId())
+ .one();
+ boolean add = false;
+ if (Objects.nonNull(myCollect)) {
+ myCollectService.removeById(myCollect.getId());
+ } else {
+ myCollectService.save(collect);
+ add = true;
+ }
+ // 澶勭悊缂撳瓨
+ Video video = baseMapper.selectById(collect.getRefId());
+ if (cache.exist(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(collect.getRefId()))) {
+ if (add) {
+ cache.incr(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(collect.getRefId()));
+ } else {
+ cache.decr(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(collect.getRefId()));
+ }
+ } else {
+ if (Objects.nonNull(video)) {
+ cache.put(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(video.getId()),
+ video.getCollectNum() + (add ? 1 : -1),
+ RedisKeyExpireConstant.COLLECT_NUM_EXPIRE,
+ RedisKeyExpireConstant.EXPIRE_DAY);
+ }
+ }
+ // 鏍囪瘑璇ヨ棰戦渶瑕侀�氳繃瀹氭椂浠诲姟缁熻鏀惰棌鏁�
+ if (Objects.nonNull(video) && ! video.getCollectNumJob()) {
+ new LambdaUpdateChainWrapper<>(baseMapper)
+ .eq(Video::getId, video.getId())
+ .set(Video::getCollectNumJob, Boolean.TRUE)
+ .update();
+ }
+ }
}
--
Gitblit v1.8.0