zxl
4 天以前 6fb006c40ff90a615fa3c3a55a65c99b55acc03d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cn.lili.modules.lmk.service.impl;
 
 
import cn.lili.base.Result;
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.Video;
import cn.lili.modules.lmk.domain.query.AuthorVideoQuery;
import cn.lili.modules.lmk.domain.vo.SimpleMyThumbsUpVO;
import cn.lili.modules.lmk.domain.vo.SimpleVideoTagVO;
import cn.lili.modules.lmk.domain.vo.WxVideoVO;
import cn.lili.modules.lmk.enums.general.VideoContentTypeEnum;
import cn.lili.modules.lmk.mapper.VideoMapper;
import cn.lili.modules.lmk.service.MySubscribeService;
import cn.lili.modules.lmk.service.ThumbsUpRecordService;
import cn.lili.modules.lmk.service.VideoService;
import cn.lili.modules.lmk.service.VideoTagRefService;
import cn.lili.utils.COSUtil;
import cn.lili.utils.PageUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * lmk-shop-java
 *
 * @author : zxl
 * @date : 2025-07-29 14:24
 **/
@Service
@RequiredArgsConstructor
public class VideoServiceMakeUpImpl{
    private final VideoTagRefService videoTagRefService;
    private final ThumbsUpRecordService thumbsUpRecordService;
    private final MySubscribeService mySubscribeService;
    private final VideoMapper videoMapper;
 
    private final COSUtil cosUtil;
    private final Cache cache;
    /**
     * 获取视频主页作者收藏视频分页
     *
     * @param query
     * @return
     */
    Result getAuthorCollectVideoPage(AuthorVideoQuery query){
        IPage<WxVideoVO> page = PageUtil.getPage(query, WxVideoVO.class);
        videoMapper.getAuthorCollectVideoPage(page, query);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<String> videoIds = page.getRecords().stream().map(WxVideoVO::getId).collect(Collectors.toList());
            Map<String, List<SimpleVideoTagVO>> tagMap = videoTagRefService.getTagsByVideoIds(videoIds)
                    .stream()
                    .collect(Collectors.groupingBy(SimpleVideoTagVO::getVideoId));
            Map<String, List<SimpleMyThumbsUpVO>> thumbsUpMap = thumbsUpRecordService.getThumbssByVideoIds(videoIds)
                    .stream()
                    .collect(Collectors.groupingBy(SimpleMyThumbsUpVO::getRefId));
            List<String> subscribes = mySubscribeService.getSubscribesByUserId(UserContext.getCurrentUserId());
            for (WxVideoVO v : page.getRecords()) {
                v.setTagList(tagMap.get(v.getId()));
                v.setCollected(Boolean.TRUE);
                v.setThumbsUp(CollectionUtils.isNotEmpty(thumbsUpMap.get(v.getId())));
                v.setCommentNum(this.getCommentNum(v.getId(), v.getCommentNum()));
                v.setCollectNum(this.getCollectNum(v.getId(), v.getCollectNum()));
                v.setThumbsUpNum(this.getThumbsUpNum(v.getId(), v.getThumbsUpNum()));
                v.setAuthorAvatar(cosUtil.getPreviewUrl(v.getAuthorAvatar()));
                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()).total(page.getTotal());
 
    }
    public 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;
    }
    public 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;
    }
 
    /**
     * 从redis中获取点赞数量,如果redis中没有则将mysql中的数量写入到redis
     *
     * @param videoId
     * @param mysqlNum
     * @return
     */
    public Integer getThumbsUpNum(String videoId, Integer mysqlNum) {
        Object redisNum = cache.get(CachePrefix.VIDEO_THUMBS_UP_NUM.getPrefixWithId(videoId));
        if (Objects.isNull(redisNum)) {
            // redis中没有就把数据库的写到redis中
            cache.put(CachePrefix.VIDEO_THUMBS_UP_NUM.getPrefixWithId(videoId), mysqlNum, RedisKeyExpireConstant.VIDEO_THUMBS_UP_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY);
            return mysqlNum;
        }
        return (Integer) redisNum;
    }
 
 
 
}