package com.apobates.forum.core.impl.event.listener;

import com.apobates.forum.core.dao.TopicConfigDao;
import com.apobates.forum.core.entity.TopicConfig;
import com.apobates.forum.core.impl.event.PlugTopicPublishEvent;
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.stereotype.Component;

/**
 * 插件话题发布事件侦听器.创建话题的配置文件
 * @author xiaofanku
 * @since 20200530
 */
@Component("plugTopicConfigEntry")
public class PlugTopicConfigListener implements MessageListener<PlugTopicPublishEvent> {
    @Autowired
    private TopicConfigDao topicConfigDao;
    private final static Logger logger = LoggerFactory.getLogger(TopicConfigListener.class);
    
    @Override
    public void onMessage(Message<PlugTopicPublishEvent> event) throws RetryableMessageException {
        logger.info("[PlugTopic][PublishEvent][1]插件话题配置文件开始执行");
        // 话题配置
        TopicConfig tc = event.getPayload().getPlugConfig();
        if(null != tc){
            topicConfigDao.save(tc);
        }
        logger.info("[PlugTopic][PublishEvent][1]插件话题配置文件执行结束");
    }
}