xiangpei
2025-05-22 e868be6c913a6a99e1491c088d052a5f58e252f9
framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java
@@ -1,5 +1,6 @@
package cn.lili.modules.lmk.service.impl;
import cn.lili.base.AbsQuery;
import cn.lili.common.security.context.UserContext;
import cn.lili.modules.lmk.domain.entity.VideoAuditRecord;
import cn.lili.modules.lmk.domain.entity.VideoTag;
@@ -8,7 +9,7 @@
import cn.lili.modules.lmk.domain.form.VideoDownForm;
import cn.lili.modules.lmk.domain.form.VideoRecommendForm;
import cn.lili.modules.lmk.domain.query.ManagerVideoQuery;
import cn.lili.modules.lmk.domain.vo.SimpleVideoTagVO;
import cn.lili.modules.lmk.domain.vo.*;
import cn.lili.modules.lmk.enums.general.TagCreateTypeEnum;
import cn.lili.modules.lmk.enums.general.VideoStatusEnum;
import cn.lili.modules.lmk.service.*;
@@ -21,8 +22,8 @@
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lili.modules.lmk.domain.form.VideoForm;
import cn.lili.modules.lmk.domain.vo.VideoVO;
import cn.lili.modules.lmk.domain.query.VideoQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
@@ -49,6 +50,7 @@
    private final VideoTagRefService videoTagRefService;
    private final LmkFileService lmkFileService;
    private final VideoAuditRecordService videoAuditRecordService;
    private final MyCollectService myCollectService;
    private final COSUtil cosUtil;
    /**
@@ -252,4 +254,28 @@
        // TODO 将下架原因以通知的方式告知用户
        return Result.ok("下架成功");
    }
    @Override
    public Result recommendVideo(AbsQuery query) {
        // 推荐算法: 1. 根据用户的收藏视频的标签  2. 根据用户关注的作者的其它视频  3. 根据用户的观看记录(观看时长较长的、重复观看次数较多的) 4. 基于相似用户的观看行为来给该用户推荐
        IPage<WxVideoVO> page = PageUtil.getPage(query, WxVideoVO.class);
        baseMapper.recommendVideo(page);
        if (page.getTotal() > 0) {
            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<SimpleMyCollectVO>> collectMap =myCollectService.getCollectsByVideoIds(videoIds)
                    .stream()
                    .collect(Collectors.groupingBy(SimpleMyCollectVO::getRefId));
            // 3. 获取视频临时访问地址、设置视频标签、我是否收藏
            page.getRecords().forEach(v -> {
                v.setGoods(new VideoGoodsVO());
                v.setTagList(tagMap.get(v.getId()));
                v.setCollected(CollectionUtils.isNotEmpty(collectMap.get(v.getId())));
                v.setVideoUrl(cosUtil.getPreviewUrl(v.getVideoFileKey()));
            });
        }
        return Result.ok().data(page.getRecords());
    }
}