package com.ruoyi.system.listener;

import com.ruoyi.system.domain.communityNews.CommentContent;
import com.ruoyi.system.domain.communityNews.CommunityNews;
import com.ruoyi.system.mapper.CommentContentMapper;
import com.ruoyi.system.mapper.CommunityNewsMapper;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.ruoyi.common.constant.Constants.ONE_LIKE_COUNT_TIME;
import static com.ruoyi.common.constant.Constants.TWO_LIKE_COUNT_TIME;

/**
 * redis过期事件监听器
 * @Author: tjf
 * @Date: 2025/2/27 11:13
 * @Describe:
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    //"__keyevent@0__:expired"表示监听数据库0中的过期事件。如果你想监听特定数据库的过期事件，只需更改@0__中的数字为相应的数据库编号
    // 。@*__ 表示监听所有redis数据库 redis0-15一共16个数据库
    //todo 项目上线需要确定redis监听的数据库编号，在此处修改
    private final Topic top = new PatternTopic("__keyevent@8__:expired");

    @Resource
    private CommunityNewsMapper communityNewsMapper;

    @Resource
    private CommentContentMapper commentContentMapper;
    /**
     * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /*设置绑定的redis数据库编号*/
    //"__keyevent@0__:expired"表示监听数据库0中的过期事件。如果你想监听特定数据库的过期事件，只需更改@0__中的数字为相应的数据库编号
    // 。@*__ 表示监听所有redis数据库 redis0-15一共16个数据库
    @Override
    protected void doRegister(RedisMessageListenerContainer listenerContainer) {
        listenerContainer.addMessageListener(this,top);
    }

    /**
     *  redis过期key的具体逻辑
     * @param message 过期的key
     * @param pattern 空间名称
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 过期key 获取不到value
        String expiredKey = message.toString();
        //ONE_LIKE_COUNT_TIME = key=one_like_count_time:{文章id}#{数量}
        // or TWO_LIKE_COUNT_TIME = key=two_like_count_time:{评论id}#{数量}
        if (expiredKey.contains(ONE_LIKE_COUNT_TIME)){
            //处理文章的点赞数量
            String[] split = expiredKey.split(":");
            //{文章id}#{数量}
            String[] splitResult = split[1].split("#");
            CommunityNews communityNews = new CommunityNews();
            communityNews.setCommunityId(Long.parseLong(splitResult[0]));
            communityNews.setUserLikes(splitResult[1]);
            communityNewsMapper.updateCommunityNews(communityNews);
            System.out.println("监听key文章的点赞:"+expiredKey);
        }else if (expiredKey.contains(TWO_LIKE_COUNT_TIME)){
            //处理评论的点赞数量
            String[] split = expiredKey.split(":");
            //{评论id}#{数量}
            String[] splitResult = split[1].split("#");
            CommentContent commentContent = new CommentContent();
            commentContent.setCommentId(Long.parseLong(splitResult[0]));
            commentContent.setLikeCount(Long.parseLong(splitResult[1]));
            commentContentMapper.updateCommentContentByCommentId(commentContent);
            System.out.println("监听key评论的点赞:"+expiredKey);
        }

    }
    public Topic getTop() {
        return top;
    }
}
