package com.fdkankan.common.rocketmq.consumer;

import com.alibaba.fastjson.JSON;
import com.fdkankan.common.rocketmq.MQLogFormat;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

//@Component
public class MqGetMessage implements MessageListenerConcurrently {

    private static Logger log = LoggerFactory.getLogger("programLog");


    @Value("${rocketmq.consumer.testGrop}")
    private String consumerGroupName;

    @Value("${rocketmq.topic.testGrop}")
    private String topicName;

    @Value("${rocketmq.tag.testGrop}")
    private String  tagName;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgExt,
                                                    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        if (CollectionUtils.isEmpty(msgExt)) {
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
        MessageExt messageExt = msgExt.get(0);
        Object object = JSON.parse(messageExt.getBody());
        String msg = JSON.toJSONString(object);
        work(msg);
        log.info(String.format(MQLogFormat.mqConsumerLogFormat, consumerGroupName, topicName, tagName, JSON.toJSONString(object)));
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }



    public  void work(String msg){
        
    }
}
