package com.fs.course.service.impl;

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

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.FsUserVideoCommentLikeMapper;
import com.fs.course.domain.FsUserVideoCommentLike;
import com.fs.course.service.IFsUserVideoCommentLikeService;

/**
 * 课堂评论点赞Service业务层处理
 *
 * @author fs
 * @date 2024-05-15
 */
@Service
public class FsUserVideoCommentLikeServiceImpl implements IFsUserVideoCommentLikeService
{
    @Autowired
    private FsUserVideoCommentLikeMapper fsUserVideoCommentLikeMapper;

    @Autowired
    private RedisTemplate<String, Boolean> redisTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisTemplateObj;

    private static final String COMMENT_LIKE_COUNT_KEY_PREFIX = "likecount:comment:";
    private static final String COMMENT_LIKE_KEY_PREFIX = "like:comment:";

    private static final String REPLY_LIKE_KEY_PREFIX = "like:reply:";

    /**
     * 查询课堂评论点赞
     *
     * @param id 课堂评论点赞主键
     * @return 课堂评论点赞
     */
    @Override
    public FsUserVideoCommentLike selectFsUserVideoCommentLikeById(Long id)
    {
        return fsUserVideoCommentLikeMapper.selectFsUserVideoCommentLikeById(id);
    }

    /**
     * 查询课堂评论点赞列表
     *
     * @param fsUserVideoCommentLike 课堂评论点赞
     * @return 课堂评论点赞
     */
    @Override
    public List<FsUserVideoCommentLike> selectFsUserVideoCommentLikeList(FsUserVideoCommentLike fsUserVideoCommentLike)
    {
        return fsUserVideoCommentLikeMapper.selectFsUserVideoCommentLikeList(fsUserVideoCommentLike);
    }

    /**
     * 新增课堂评论点赞
     *
     * @param fsUserVideoCommentLike 课堂评论点赞
     * @return 结果
     */
    @Override
    public int insertFsUserVideoCommentLike(FsUserVideoCommentLike fsUserVideoCommentLike)
    {
        fsUserVideoCommentLike.setCreateTime(DateUtils.getNowDate());
        return fsUserVideoCommentLikeMapper.insertFsUserVideoCommentLike(fsUserVideoCommentLike);
    }

    /**
     * 修改课堂评论点赞
     *
     * @param fsUserVideoCommentLike 课堂评论点赞
     * @return 结果
     */
    @Override
    public int updateFsUserVideoCommentLike(FsUserVideoCommentLike fsUserVideoCommentLike)
    {
        fsUserVideoCommentLike.setUpdateTime(DateUtils.getNowDate());
        return fsUserVideoCommentLikeMapper.updateFsUserVideoCommentLike(fsUserVideoCommentLike);
    }

    /**
     * 批量删除课堂评论点赞
     *
     * @param ids 需要删除的课堂评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteFsUserVideoCommentLikeByIds(Long[] ids)
    {
        return fsUserVideoCommentLikeMapper.deleteFsUserVideoCommentLikeByIds(ids);
    }

    /**
     * 删除课堂评论点赞信息
     *
     * @param id 课堂评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteFsUserVideoCommentLikeById(Long id)
    {
        return fsUserVideoCommentLikeMapper.deleteFsUserVideoCommentLikeById(id);
    }


    @Override
    public void likeComment(String commentId, Long userId) {
        String key = COMMENT_LIKE_KEY_PREFIX + commentId + ":user:" + userId;
        redisTemplate.opsForValue().set(key, true, 1, TimeUnit.DAYS);
        // 更新点赞数
        String likeCountKey = COMMENT_LIKE_COUNT_KEY_PREFIX + commentId;
        if (redisTemplateObj.opsForValue().get(likeCountKey) == null) {
            redisTemplateObj.opsForValue().set(likeCountKey, 0, 1, TimeUnit.DAYS);
        }
        redisTemplateObj.opsForValue().increment(likeCountKey, 1);
        FsUserVideoCommentLike like = new FsUserVideoCommentLike();
        like.setCommentId(Long.valueOf(commentId));
        like.setUserId(userId);
        like.setCreateTime(DateUtils.getNowDate());
        fsUserVideoCommentLikeMapper.insertFsUserVideoCommentLike(like);
    }

    @Override
    public void unlikeComment(String commentId, Long userId) {
        String key = COMMENT_LIKE_KEY_PREFIX + commentId + ":user:" + userId;
        redisTemplate.delete(key);
        // 更新点赞数
        String likeCountKey = COMMENT_LIKE_COUNT_KEY_PREFIX + commentId;
        redisTemplateObj.opsForValue().decrement(likeCountKey, 1);
        fsUserVideoCommentLikeMapper.deleteCommentLike(commentId, userId);
    }

    @Override
    public boolean isCommentLiked(String commentId, Long userId) {
        String key = COMMENT_LIKE_KEY_PREFIX + commentId + ":user:" + userId;
        Boolean isLiked = redisTemplate.opsForValue().get(key);
        if (isLiked == null) {
            isLiked = fsUserVideoCommentLikeMapper.checkCommentLike(commentId, userId) > 0;
            if (isLiked) {
                redisTemplate.opsForValue().set(key, true, 1, TimeUnit.DAYS);
            }
        }
        return Boolean.TRUE.equals(isLiked);
    }

}
