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

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveMallFlscOrderDubboConsumerService;
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 ReceiveMallFlscConsumerHandler {

	@Autowired
	private ReceiveMallFlscOrderDubboConsumerService receiveMallFlscOrderDubboConsumerService;

	/**
	 * 老的订单数据
	 * <p>
	 * 这个大概就是
	 * <p>
	 * 燕璐
	 * <p>
	 * 代客下单系统:https://ui-a.superdesk.cn/login?redirect=%2Fdashboard
	 * <p>
	 * 账号:yanlu@avic-s.com  密码:123456
	 * <p>
	 * SUBMIT20200206184939749204
	 * <p>
	 * http://order.avic-s.com/#/homePage
	 * yanl   密码:123456
	 * <p>
	 * PHP
	 * $topic = 'orderpro';            // 配置在env中
	 * $url   = '39.108.55.24:9092';   // 配置在env中 统一订单中心
	 * <p>
	 * topic orderpro
	 *
	 * @param record
	 */
	@KafkaListener(topics = "${superdesk.mall.kafka.topic.flsc.order}")
	public void listenUnifiedFlscOrder(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 {
					receiveMallFlscOrderDubboConsumerService.kafkaUnifiedFlscOrderReceive(String.valueOf(message));

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

		}
	}

	@KafkaListener(topics = "${superdesk.mall.kafka.topic.flsc.refund}")
	public void listenUnifiedFlscRefundOrder(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 {
					receiveMallFlscOrderDubboConsumerService.kafkaUnifiedFlscRefundOrderReceive(String.valueOf(message));
				} catch (Exception e) {
					log.error("商城-福利商城-退单数据 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}


	@KafkaListener(topics = "${superdesk.mall.kafka.topic.flsc.accounting.order}")
	public void listenUnifiedFlscAccountingOrder(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 {
					receiveMallFlscOrderDubboConsumerService.kafkaUnifiedFlscAccountingOrderReceive(String.valueOf(message));
				} catch (Exception e) {
					log.error("商城-福利商城-对账数据 error\n ", e);
					e.printStackTrace();
				}
			}
		}
	}


}


