package cn.superdesk.uniorder.mq.consumer.superdeskmall;


import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveProxyClientOrderDubboConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;


/**
 * @Description: 代客下单
 * @Author: linjinyu
 * @CreateDate: 2021/4/26 上午11:19
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/4/26 上午11:19
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class ReceiveProxyClientConsumerHandler {

	@Autowired
	private ReceiveProxyClientOrderDubboConsumerService receiveProxyClientOrderDubboConsumerService;


	/**
	 * 代客下单
	 * 开发环境
	 * ip   : 192.168.6.223
	 * port : 9092
	 * topic: proxyclient_order
	 *
	 * @param record
	 */
	@KafkaListener(topics = "${superdesk.mall.kafka.topic.proxyclient.order}")
	public void listenProxyClientOrder(ConsumerRecord<String, String> record) {

		Optional<?> kafkaMessage = Optional.ofNullable(record.value());

		if (kafkaMessage.isPresent()) {

			Object message = kafkaMessage.get();

			if (//!StringUtils.isEmpty(String.valueOf(record.key())) &&
					!StringUtils.isEmpty(String.valueOf(message))
			) {

				try {
					receiveProxyClientOrderDubboConsumerService.kafkaProxyClientOrderReceive(String.valueOf(message));

				} catch (Exception e) {
					log.error("kafka 代客下单 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}
}
