package com.ms.biz.quque.consumer;

import com.alibaba.fastjson.JSON;
import com.jinritemai.cloud.base.core.util.LogUtils;
import com.ms.biz.quque.ConsumerListener;
import com.ms.biz.quque.Group;
import com.ms.biz.quque.Topic;
import com.ms.biz.quque.message.DsMessage;
import com.ms.biz.service.DsMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Slf4j
@ConsumerListener(consumerGroup = Group.GROUP_DS_MESSAGE, topic = Topic.TOPIC_DS_MESSAGE)
@Component
public class DsMessageConsumer extends BaseConsumer {
    @Autowired
    private DsMessageService dsMessageService;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        LogUtils.setLogId(LogUtils.generateLogId());
        log.info("DsMessageConsumer Receive New Messages: {}", JSON.toJSONString(list));
        for (MessageExt message: list) {
            try {
                log.info("consume message: {}", new String(message.getBody()));
                DsMessage dsMessage = JSON.parseObject(new String(message.getBody()), DsMessage.class);
                dsMessageService.processDsMessage(dsMessage.getDsMessageId());
            } catch (Throwable e) {
                log.error("DsMessageConsumerError", e);
            }
        }
        log.info("DsMessageConsumer consumeMessage success");
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}
