package com.apobates.forum.thrones.event;

import com.apobates.forum.member.impl.event.MemberChangeEvent;
import com.github.davidmarquis.redisq.Message;
import com.github.davidmarquis.redisq.consumer.MessageListener;
import com.github.davidmarquis.redisq.consumer.retry.RetryableMessageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;

@Component("asyncInfoCache")
public class MemberChangeEventListener implements MessageListener<MemberChangeEvent> {
    @Autowired
    private RedisTemplate<String, Object> template;
    private final static Logger logger = LoggerFactory.getLogger(MemberChangeEventListener.class);

    @Override
    public void onMessage(Message<MemberChangeEvent> message) throws RetryableMessageException {
        logger.info("[Member][ChangeEvent][1]会员变更侦听器处理开始");
        MemberChangeEvent event = message.getPayload();
        Map<String, Integer> gm = Map.ofEntries(Map.entry(event.getKey(), event.getValue()));
        template.opsForHash().putAll(getEventRedisKey(event.getMemberId()), gm);
        logger.info("[Member][ChangeEvent][1]会员变更侦听器结束处理");
    }

    public static String getEventRedisKey(long memberId){
        return "memberEvent:"+memberId;
    }
}
