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

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


	@Autowired
	private ReceiveMallFlscQchOrderDubboConsumerService receiveMallFlscQchOrderDubboConsumerService;

	@KafkaListener(topics = "${superdesk.mall.kafka.topic.flsc_qch.order}")
	public void listenScoreFlscQchOrder(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 {
					receiveMallFlscQchOrderDubboConsumerService.kafkaScoreOrderReceive(String.valueOf(message));

				} catch (Exception e) {
					log.error("商城-福利商城-侨城汇-订单数据 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}

	@KafkaListener(topics = "${superdesk.mall.kafka.topic.flsc_qch.refund}")
	public void listenScoreFlscQchOrderRefund(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 {
					receiveMallFlscQchOrderDubboConsumerService.kafkaScoreOrderRefundReceive(String.valueOf(message));

				} catch (Exception e) {
					log.error("商城-福利商城-侨城汇-退单数据 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}
}
