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

import com.apobates.forum.core.dao.BoardConfigDao;
import com.apobates.forum.core.entity.Board;
import com.apobates.forum.core.entity.BoardConfig;
import com.apobates.forum.core.impl.event.BoardCreateEvent;
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("boardConfigEntry")
public class BoardConfigListener implements MessageListener<BoardCreateEvent> {
    @Autowired
    private BoardConfigDao boardConfigDao;
    private final static Logger logger = LoggerFactory.getLogger(BoardConfigListener.class);
    
    @Override
    public void onMessage(Message<BoardCreateEvent> event) throws RetryableMessageException {
        logger.info("[Board][CreateEvent][1]版块配置文件开始执行");
        Board board = event.getPayload().getBoard();
        BoardConfig boardConfig = Optional.ofNullable(event.getPayload().getBoardConfig()).orElse(BoardConfig.defaultConfig(board.getId()));
        //版块配置
        boardConfigDao.save(boardConfig);
        logger.info("[Board][CreateEvent][1]版块配置文件执行结束");
    }
}