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.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import lombok.Synchronized;
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:";
    private static final String NO_FAVORITE_KEY_PREFIX = "nofavorite:video:";
    @Override
    public R checkFavorite(Long videoId, long userId) {
        String key = FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
        Boolean hasFavorite = redisTemplate.opsForValue().get(key);
        if (hasFavorite != null && hasFavorite) {
            return R.ok().put("isFavorite",1);
        }else {
            return fsUserVideoFavoriteMapper.checkFavorite(videoId, userId) > 0 ? R.ok().put("isFavorite", 1) : R.error().put("isFavorite", 0);
        }
    }

    @Override
    @Transactional
    @Synchronized
    public void favoriteVideo(Long videoId, long userId) {
        String unlikeKey = NO_FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
        redisTemplate.delete(unlikeKey);
        String key = FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
        redisTemplate.opsForValue().set(key, true, 1, TimeUnit.DAYS);
    }

    @Override
    @Transactional
    @Synchronized
    public void deleteFavorite(Long videoId, long userId) {
        String key = FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
        redisTemplate.delete(key);

        // 将取消点赞记录写入Redis
        String unlikeKey = NO_FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
        redisTemplate.opsForValue().set(unlikeKey, true, 1, TimeUnit.DAYS);
    }
}
