xiangpei
6 小时以前 1cdb060a8aa59b0979f7609db1781805528e76e7
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;
    /**
     * 添加
@@ -389,6 +393,8 @@
            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()));
@@ -400,6 +406,40 @@
            });
        }
        return Result.ok().data(page.getRecords());
    }
    /**
     * 从redis中获取评论数量,如果redis中没有则将mysql中的数量写入到redis
     *
     * @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中没有就把数据库的写到redis中
            cache.put(CachePrefix.VIDEO_COMMENT_NUM.getPrefixWithId(videoId), mysqlNum, RedisKeyExpireConstant.COMMENT_NUM_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY);
            return mysqlNum;
        }
        return (Integer) redisNum;
    }
    /**
     * 从redis中获取收藏数量,如果redis中没有则将mysql中的数量写入到redis
     *
     * @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中没有就把数据库的写到redis中
            cache.put(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(videoId), mysqlNum, RedisKeyExpireConstant.COLLECT_NUM_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY);
            return mysqlNum;
        }
        return (Integer) redisNum;
    }
    @Override
@@ -446,6 +486,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 +500,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();
        }
    }
@@ -709,4 +757,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();
        }
    }
}