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

import com.apobates.forum.member.dao.RegisteInviteCodeDao;
import com.apobates.forum.member.entity.Member;
import com.apobates.forum.member.impl.event.MemberSignUpEvent;
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("inviteCode")
public class MemberSignUpInviteCodeListener implements MessageListener<MemberSignUpEvent> {
    @Autowired
    private RegisteInviteCodeDao registeInviteCodeDao;
    private final static Logger logger = LoggerFactory.getLogger(MemberSignUpInviteCodeListener.class);

    @Override
    public void onMessage(Message<MemberSignUpEvent> event) throws RetryableMessageException {
        logger.info("[Member][SignUpEvent][2]开始激活邀请码");
        Member m = event.getPayload().getMember();
        if (m.getInviteCodeId() > 0) {
            registeInviteCodeDao.active(m.getInviteCodeId(), m.getId(), m.getNames());
        }
        logger.info("[Member][SignUpEvent][2]邀请码激活结束");
    }
}