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.TopicPublishEvent;
import java.util.Optional;
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 20200521
 */
@Component("topicConfigEntry")
public class TopicConfigListener implements MessageListener<TopicPublishEvent> {
    @Autowired
    private TopicConfigDao topicConfigDao;
    private final static Logger logger = LoggerFactory.getLogger(TopicConfigListener.class);
    
    @Override
    public void onMessage(Message<TopicPublishEvent> event) throws RetryableMessageException {
        logger.info("[Topic][PublishEvent][1]话题配置文件开始执行");
        TopicPublishEvent tpe = event.getPayload();
        // 话题配置
        TopicConfig tc = Optional.ofNullable(tpe.getConfig()).orElse(TopicConfig.defaultConfig(tpe.getTopic().getId()));
        topicConfigDao.save(tc);
        logger.info("[Topic][PublishEvent][1]话题配置文件执行结束");
    }
}