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

import com.apobates.forum.core.dao.BoardStatsDao;
import com.apobates.forum.core.dao.TopicStatsDao;
import com.apobates.forum.core.entity.Topic;
import com.apobates.forum.core.entity.TopicStats;
import com.apobates.forum.core.impl.event.TopicPublishEvent;
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("topicStatsEntry")
public class TopicStatsListener implements MessageListener<TopicPublishEvent> {
    @Autowired
    private TopicStatsDao topicStatsDao;
    @Autowired
    private BoardStatsDao boardStatsDao;
    private final static Logger logger = LoggerFactory.getLogger(TopicStatsListener.class);
    
    @Override
    public void onMessage(Message<TopicPublishEvent> event) throws RetryableMessageException {
        logger.info("[Topic][PublishEvent][2]话题统计和版块统计处理开始");
        //话题统计
        Topic topic = event.getPayload().getTopic();
        topicStatsDao.save(TopicStats.init(topic.getId(), topic.getMemberId(), topic.getMemberNickname(), topic.getVolumesId(), topic.getBoardId()));
        boardStatsDao.updateTopic(topic.getBoardId(), topic.getTitle(), topic.getId(), topic.getEntryDateTime(), topic.getMemberId(), topic.getMemberNickname());
        logger.info("[Topic][PublishEvent][2]话题统计和版块统计处理结束");
    }
}