package com.zhentao.config;

import com.zhentao.service.impl.AnnouncementServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * Redis消息监听器配置
 */
@Configuration
public class RedisMessageListenerConfig {
    
    @Autowired
    private AnnouncementServiceImpl announcementService;
    
    /**
     * Redis消息监听器容器
     */
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(
            RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        
        // 添加公告更新监听器
        container.addMessageListener(
            messageListenerAdapter(),
            new PatternTopic("announcement:update")
        );
        
        return container;
    }
    
    /**
     * 消息监听适配器
     */
    @Bean
    public MessageListenerAdapter messageListenerAdapter() {
        return new MessageListenerAdapter(announcementService, "handleAnnouncementUpdate");
    }
}

