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

import com.apobates.forum.core.dao.TopicActionCollectionDao;
import com.apobates.forum.core.entity.Topic;
import com.apobates.forum.core.entity.TopicActionCollection;
import com.apobates.forum.core.impl.event.PlugTopicPublishEvent;
import com.apobates.forum.event.elderly.ActionEventCulpritor;
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 20200530
 */
@Component("plugTopicActionEntry")
public class PlugTopicRelateActionListener implements MessageListener<PlugTopicPublishEvent> {
    @Autowired
    private TopicActionCollectionDao topicActionCollectionDao;
    private final static Logger logger = LoggerFactory.getLogger(PlugTopicRelateActionListener.class);
    
    @Override
    public void onMessage(Message<PlugTopicPublishEvent> e) throws RetryableMessageException {
        logger.info("[PlugTopic][PublishEvent][2]插件话题关联动作开始执行");
        PlugTopicPublishEvent tpe = e.getPayload();
        Topic tr = tpe.getPlugTopic(); long contentPostsId = tpe.getPlugContent();
        ActionEventCulpritor culpritor = e.getPayload().getCulpritor();

        //积分计算时的受益会员ID,一般均为操作者
        long rodeMember = culpritor.getMemberId();
        TopicActionCollection tac = new TopicActionCollection(e.getPayload().getAction(), tr.getTitle(), tr.getId(), contentPostsId, culpritor, rodeMember);
        topicActionCollectionDao.save(tac);
        logger.info("[PlugTopic][PublishEvent][2]插件话题关联动作执行结束");
    }
}