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.Posts;
import com.apobates.forum.core.impl.event.PostsPublishEvent;
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("postsStatsEntry")
public class PostsStatsListener implements MessageListener<PostsPublishEvent> {
    @Autowired
    private TopicStatsDao topicStatsDao;
    @Autowired
    private BoardStatsDao boardStatsDao;
    private final static Logger logger = LoggerFactory.getLogger(PostsStatsListener.class);
    
    @Override
    public void onMessage(Message<PostsPublishEvent> event) throws RetryableMessageException {
        logger.info("[Posts][PublishEvent][1]回复创建后, 执行统计文件更新");
        Posts posts = event.getPayload().getPosts();
        //统计{版块:回复数+1,}
        boardStatsDao.updatePosts(posts.getBoardId());
        //统计{话题: 回复数+1}
        //话题的最近回复{topic.rankingDateTime=}
        topicStatsDao.updatePosts(posts.getTopicId(), posts.getMemberId(), posts.getMemberNickname(), posts.getEntryDateTime(), posts.getId());
        logger.info("[Posts][PublishEvent][1]执行回复相关的统计更新结束");
    }
}