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

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveMallQyngOrderDubboConsumerService;
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/11/30 下午4:10
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/11/30 下午4:10
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class ReceiveMallQyngConsumerHandler {

	@Autowired
	private ReceiveMallQyngOrderDubboConsumerService receiveMallQyngOrderDubboConsumerService;

	@KafkaListener(topics = "${superdesk.mall.kafka.topic.qyng.order}")
	public void listenUnifiedQyngOrder(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 {
					receiveMallQyngOrderDubboConsumerService.kafkaUnifiedQyngOrderReceive(String.valueOf(message));

				} catch (Exception e) {
					log.error("商城-企业内购-订单数据 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}

		}
	}

	@KafkaListener(topics = "${superdesk.mall.kafka.topic.qyng.refund}")
	public void listenUnifiedQyngRefundOrder(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 {
					receiveMallQyngOrderDubboConsumerService.kafkaUnifiedQyngRefundOrderReceive(String.valueOf(message));
				} catch (Exception e) {
					log.error("商城-企业内购-退单数据 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}
}
