package com.apobates.forum.thrones.admin.event;

import com.apobates.forum.core.entity.BoardModeratorRoleHistory;
import com.apobates.forum.core.impl.event.ModeratorRecallEvent;
import com.apobates.forum.member.dao.MemberDao;
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 20200831
 */
@Component("recallChangeRole")
public class ModeratorRecallChangeRoleListener implements MessageListener<ModeratorRecallEvent> {
    @Autowired
    private MemberDao memberDao;
    private final static Logger logger = LoggerFactory.getLogger(ModeratorRecallChangeRoleListener.class);
    //从BoardModeratorDaoImpl.deleteModerator中取消MemberDao
    @Override
    public void onMessage(Message<ModeratorRecallEvent> e) throws RetryableMessageException {
        logger.info("[Moderator][RecallEvent][1]版主卸任角色变更开始");
        BoardModeratorRoleHistory bmrh = e.getPayload().getRemoveRoleHistory();
        memberDao.editMemberRole(bmrh.getMemberId(), e.getPayload().getUpdateRole());
        logger.info("[Moderator][RecallEvent][1]版主卸任角色变更结束");
    }
}