package cn.reghao.tnb.content.app.vod.db.repository;

import cn.reghao.tnb.content.api.constant.VideoStatus;
import cn.reghao.tnb.content.app.vod.db.mapper.*;
import cn.reghao.tnb.common.db.GroupCount;
import cn.reghao.tnb.content.app.vod.model.po.VideoFile;
import cn.reghao.tnb.content.app.vod.model.po.VideoPost;
import cn.reghao.tnb.content.app.vod.model.po.VideoStatistic;
import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
import cn.reghao.oss.sdk.model.dto.media.VideoUrlDto;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author reghao
 * @date 2023-08-28 15:00:43
 */
@Repository
public class VideoRepository {
    private final VideoFileMapper videoFileMapper;
    private final VideoPostMapper videoPostMapper;
    private final VideoStatisticMapper videoStatisticMapper;

    public VideoRepository(VideoPostMapper videoPostMapper, VideoStatisticMapper videoStatisticMapper,
                           VideoFileMapper videoFileMapper) {
        this.videoPostMapper = videoPostMapper;
        this.videoStatisticMapper = videoStatisticMapper;
        this.videoFileMapper = videoFileMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public void savePostVideo(VideoPost videoPost, VideoStatistic videoStatistic) {
        videoPostMapper.updateVideoPublish(videoPost);
        videoStatisticMapper.save(videoStatistic);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveVideo(VideoFile videoFile, VideoPost videoPost) {
        videoFileMapper.save(videoFile);
        videoPostMapper.save(videoPost);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateBiliVideoFile(String videoId, VideoInfo videoInfo, int channelId) {
        VideoFile videoFile = new VideoFile(videoId, videoInfo, channelId);
        videoFileMapper.save(videoFile);
        videoPostMapper.updateVideoCached(videoId, videoInfo, channelId);
    }

    public List<VideoUrlDto> findVideoUrls(String videoId) {
        return videoFileMapper.findVideoUrls(videoId);
    }

    //@Cacheable(cacheNames = "tnb:content:publishBy", key = "publishBy", unless = "#result == null")
    public List<GroupCount> getPublishUsers(int publishType) {
        List<GroupCount> list;
        if (publishType == 1) {
            list = videoPostMapper.findPublishByVideo();
        } else if (publishType == 2) {
            list = videoPostMapper.findPublishByImage();
        } else {
            list = videoPostMapper.findPublishByPost();
        }

        return list;
    }

    @Cacheable(cacheNames = "tnb:content:short_video", unless = "#result == null")
    public List<String> getShortVideoIds(List<Integer> userScopes) {
        List<String> videoIds = videoPostMapper.findShortVideo(userScopes);
        return videoIds;
    }

    public VideoPost getVideoPost(String videoId) {
        return videoPostMapper.findByVideoId(videoId);
    }
}
