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.FsUserVideoLikeMapper;
import com.fs.course.domain.FsUserVideoLike;
import com.fs.course.service.IFsUserVideoLikeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课堂视频点赞Service业务层处理
 *
 * @author fs
 * @date 2024-05-15
 */
@Service
public class FsUserVideoLikeServiceImpl implements IFsUserVideoLikeService
{
    @Autowired
    private FsUserVideoLikeMapper fsUserVideoLikeMapper;

    @Autowired
    private RedisTemplate<String, Boolean> redisTemplate;

    /**
     * 查询课堂视频点赞
     *
     * @param likeId 课堂视频点赞主键
     * @return 课堂视频点赞
     */
    @Override
    public FsUserVideoLike selectFsUserVideoLikeByLikeId(Long likeId)
    {
        return fsUserVideoLikeMapper.selectFsUserVideoLikeByLikeId(likeId);
    }

    /**
     * 查询课堂视频点赞列表
     *
     * @param fsUserVideoLike 课堂视频点赞
     * @return 课堂视频点赞
     */
    @Override
    public List<FsUserVideoLike> selectFsUserVideoLikeList(FsUserVideoLike fsUserVideoLike)
    {
        return fsUserVideoLikeMapper.selectFsUserVideoLikeList(fsUserVideoLike);
    }

    /**
     * 新增课堂视频点赞
     *
     * @param fsUserVideoLike 课堂视频点赞
     * @return 结果
     */
    @Override
    public int insertFsUserVideoLike(FsUserVideoLike fsUserVideoLike)
    {
        fsUserVideoLike.setCreateTime(DateUtils.getNowDate());
        return fsUserVideoLikeMapper.insertFsUserVideoLike(fsUserVideoLike);
    }

    /**
     * 修改课堂视频点赞
     *
     * @param fsUserVideoLike 课堂视频点赞
     * @return 结果
     */
    @Override
    public int updateFsUserVideoLike(FsUserVideoLike fsUserVideoLike)
    {
        fsUserVideoLike.setUpdateTime(DateUtils.getNowDate());
        return fsUserVideoLikeMapper.updateFsUserVideoLike(fsUserVideoLike);
    }

    /**
     * 批量删除课堂视频点赞
     *
     * @param likeIds 需要删除的课堂视频点赞主键
     * @return 结果
     */
    @Override
    public int deleteFsUserVideoLikeByLikeIds(Long[] likeIds)
    {
        return fsUserVideoLikeMapper.deleteFsUserVideoLikeByLikeIds(likeIds);
    }

    /**
     * 删除课堂视频点赞信息
     *
     * @param likeId 课堂视频点赞主键
     * @return 结果
     */
    @Override
    public int deleteFsUserVideoLikeByLikeId(Long likeId)
    {
        return fsUserVideoLikeMapper.deleteFsUserVideoLikeByLikeId(likeId);
    }

    private static final String LIKE_KEY_PREFIX = "like:video:";
    private static final String UNLIKE_KEY_PREFIX = "unlike:video:";
    @Override
    public R checkLike(Long videoId, long userId) {
        String key = LIKE_KEY_PREFIX + videoId + ":user:" + userId;
        Boolean hasLiked = redisTemplate.opsForValue().get(key);
        if (hasLiked != null && hasLiked) {
            return R.ok().put("isLike",1);
        }else {
            return fsUserVideoLikeMapper.checkLike(videoId,userId)>0?R.ok().put("isLike",1):R.error().put("isLike",0);
        }
    }

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

    @Override
    @Transactional
    @Synchronized
    public void unlikeVideo(Long videoId, long userId) {

        String key = LIKE_KEY_PREFIX + videoId + ":user:" + userId;
        redisTemplate.delete(key);

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