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

import com.apobates.forum.core.dao.TopicTagDao;
import com.apobates.forum.core.entity.TopicTag;
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;
import java.util.Set;

/**
 * 保存话题的标签
 * 即使失败也可通过话题内容重塑
 * @author xiaofanku
 * @since 20200521
 */
@Component("topicTagEntry")
public class TopicTagListener implements MessageListener<TopicPublishEvent> {
    @Autowired
    private TopicTagDao topicTagDao;
    private final static Logger logger = LoggerFactory.getLogger(TopicTagListener.class);
    
    @Override
    public void onMessage(Message<TopicPublishEvent> event) throws RetryableMessageException {
        logger.info("[Topic][PublishEvent][3]话题标签处理开始");
        TopicPublishEvent tpe = event.getPayload();
        Set<TopicTag> tags = tpe.getTages();
        if (null == tags || tags.isEmpty()) {
            logger.info("[Topic][PublishEvent][3]话题标签是空");
            return;
        }
        //所有标签
        topicTagDao.batchSave(tpe.getTopic().getId(), tags);
        logger.info("[Topic][PublishEvent][3]话题标签处理结束");
    }
}