package com.red.circle.other.app.listener;

import com.red.circle.component.mq.MessageEventProcess;
import com.red.circle.component.mq.MessageEventProcessDescribe;
import com.red.circle.component.mq.config.RocketMqMessageListener;
import com.red.circle.component.mq.service.Action;
import com.red.circle.component.mq.service.ConsumerMessage;
import com.red.circle.component.mq.service.MessageListener;
import com.red.circle.mq.business.model.event.BadgeEvent;
import com.red.circle.mq.rocket.business.streams.BadgeCheckSink;
import com.red.circle.other.app.common.props.BadgeCommon;
import com.red.circle.other.inner.enums.material.BadgeKeyEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 徽章检测处理.
 *
 * @author pengliang on 2021/4/8
 */
@Slf4j
@RocketMqMessageListener(groupId = BadgeCheckSink.INPUT, tag = BadgeCheckSink.TAG)
@RequiredArgsConstructor
public class BadgeCheckListener implements MessageListener {

  private final BadgeCommon badgeCommon;
  private final MessageEventProcess messageEventProcess;

  @Override
  public Action consume(ConsumerMessage message) {
    return messageEventProcess.consume(
        MessageEventProcessDescribe.builder()
            .logTag("徽章检测处理")
            .consumeMsgTimeoutMinute(60)
            .repeatConsumeMinute(1)
            .repeatConsumeTag("LiveVoiceSession")
            .checkConsumeTag(BadgeCheckSink.TAG)
            .message(message)
            .build(),
        BadgeEvent.class,
        eventBody -> badgeCommon.checkActivateBadgePermanent(eventBody.getUserId(),
            BadgeKeyEnum.valueOf(eventBody.getBadgeKey())));

  }

}
