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

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.gift.OfflineProcessGiftEvent;
import com.red.circle.mq.rocket.business.streams.OfflineProcessGiftSink;
import com.red.circle.other.app.listener.gift.strategy.FactoryForGiftStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 礼物相关统计处理.
 *
 * @author pengliang on 2022/7/26
 */
@Slf4j
@RocketMqMessageListener(groupId = OfflineProcessGiftSink.INPUT, tag = OfflineProcessGiftSink.TAG)
@RequiredArgsConstructor
public class OfflineProcessGiftListener implements MessageListener {

  private final MessageEventProcess messageEventProcess;
  private final FactoryForGiftStrategy factoryForGiftStrategy;

  @Override
  public Action consume(ConsumerMessage message) {
    return messageEventProcess.consume(
        MessageEventProcessDescribe.builder()
            .logTag("礼物相关统计处理")
            .consumeMsgTimeoutMinute(15)
            .repeatConsumeMinute(15)
            .repeatConsumeTag("OfflineProcessGift")
            .checkConsumeTag(OfflineProcessGiftSink.TAG)
            .message(message)
            .build(),
        OfflineProcessGiftEvent.class,
        eventBody -> factoryForGiftStrategy.getStrategy(eventBody.getType()).processor(eventBody));
  }

}
