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

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

	@Autowired
	private ReceiveMallMixOrderDubboConsumerService mallMixOrderDubboConsumerService;

	@KafkaListener(topics = "${superdesk.mall.kafka.topic.mix.order}")
	public void listenUnifiedMixOrder(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 {
					mallMixOrderDubboConsumerService.kafkaUnifiedMixOrderReceive(String.valueOf(message));

				} catch (Exception e) {
					log.error("商城-企业福利商城-计划弃用-订单数据 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}

		}
	}

	@KafkaListener(topics = "${superdesk.mall.kafka.topic.mix.refund}")
	public void listenUnifiedMixRefundOrder(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 {
					mallMixOrderDubboConsumerService.kafkaUnifiedMixRefundOrderReceive(String.valueOf(message));
				} catch (Exception e) {
					log.error("商城-企业福利商城-计划弃用-退单数据 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}

}