package com.fs.course.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserVideoFavoriteMapper;
import com.fs.course.domain.FsUserVideoFavorite;
import com.fs.course.service.IFsUserVideoFavoriteService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课堂视频收藏Service业务层处理
 *
 * @author fs
 * @date 2024-05-15
 */
@Service
public class FsUserVideoFavoriteServiceImpl implements IFsUserVideoFavoriteService
{
    @Autowired
    private FsUserVideoFavoriteMapper fsUserVideoFavoriteMapper;
    @Autowired(required = false)
    private RedisTemplate<String, Boolean> redisTemplate;

    /**
     * 查询课堂视频收藏
     *
     * @param favoriteId 课堂视频收藏主键
     * @return 课堂视频收藏
     */
    @Override
    public FsUserVideoFavorite selectFsUserVideoFavoriteByFavoriteId(Long favoriteId)
    {
        return fsUserVideoFavoriteMapper.selectFsUserVideoFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 查询课堂视频收藏列表
     *
     * @param fsUserVideoFavorite 课堂视频收藏
     * @return 课堂视频收藏
     */
    @Override
    public List<FsUserVideoFavorite> selectFsUserVideoFavoriteList(FsUserVideoFavorite fsUserVideoFavorite)
    {
        return fsUserVideoFavoriteMapper.selectFsUserVideoFavoriteList(fsUserVideoFavorite);
    }

    /**
     * 新增课堂视频收藏
     *
     * @param fsUserVideoFavorite 课堂视频收藏
     * @return 结果
     */
    @Override
    public int insertFsUserVideoFavorite(FsUserVideoFavorite fsUserVideoFavorite)
    {
        fsUserVideoFavorite.setCreateTime(DateUtils.getNowDate());
        return fsUserVideoFavoriteMapper.insertFsUserVideoFavorite(fsUserVideoFavorite);
    }

    /**
     * 修改课堂视频收藏
     *
     * @param fsUserVideoFavorite 课堂视频收藏
     * @return 结果
     */
    @Override
    public int updateFsUserVideoFavorite(FsUserVideoFavorite fsUserVideoFavorite)
    {
        fsUserVideoFavorite.setUpdateTime(DateUtils.getNowDate());
        return fsUserVideoFavoriteMapper.updateFsUserVideoFavorite(fsUserVideoFavorite);
    }

    /**
     * 批量删除课堂视频收藏
     *
     * @param favoriteIds 需要删除的课堂视频收藏主键
     * @return 结果
     */
    @Override
    public int deleteFsUserVideoFavoriteByFavoriteIds(Long[] favoriteIds)
    {
        return fsUserVideoFavoriteMapper.deleteFsUserVideoFavoriteByFavoriteIds(favoriteIds);
    }

    /**
     * 删除课堂视频收藏信息
     *
     * @param favoriteId 课堂视频收藏主键
     * @return 结果
     */
    @Override
    public int deleteFsUserVideoFavoriteByFavoriteId(Long favoriteId)
    {
        return fsUserVideoFavoriteMapper.deleteFsUserVideoFavoriteByFavoriteId(favoriteId);
    }
    private static final String FAVORITE_KEY_PREFIX = "favorite:video:";

    @Override
    public R checkFavorite(Long videoId, long userId) {
        String key = favoriteKey(videoId, userId);
        Boolean hasFavorite = redisTemplate.opsForValue().get(key);
        if (Boolean.TRUE.equals(hasFavorite)) {
            return R.ok().put("isFavorite", 1);
        }
        if (Boolean.FALSE.equals(hasFavorite)) {
            return R.error().put("isFavorite", 0);
        }
        return fsUserVideoFavoriteMapper.checkFavorite(videoId, userId) > 0
                ? R.ok().put("isFavorite", 1)
                : R.error().put("isFavorite", 0);
    }

    @Override
    @Transactional
    public void favoriteVideo(Long videoId, long userId) {
        if (fsUserVideoFavoriteMapper.checkFavorite(videoId, userId) == 0) {
            FsUserVideoFavorite favorite = new FsUserVideoFavorite();
            favorite.setVideoId(videoId);
            favorite.setUserId(userId);
            favorite.setCreateTime(DateUtils.getNowDate());
            fsUserVideoFavoriteMapper.insertFsUserVideoFavorite(favorite);
        }
        redisTemplate.opsForValue().set(favoriteKey(videoId, userId), true, 1, TimeUnit.DAYS);
    }

    @Override
    @Transactional
    public void deleteFavorite(Long videoId, long userId) {
        fsUserVideoFavoriteMapper.deleteFavorite(videoId, userId);
        redisTemplate.opsForValue().set(favoriteKey(videoId, userId), false, 1, TimeUnit.DAYS);
    }

    private static String favoriteKey(Long videoId, long userId) {
        return FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
    }
}
