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;
|
}
|
|
|
|
}
|