package cn.superdesk.uniorder.mq.consumer.superdeskapp.services.flashdeliver;

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceiveFlashDeliverOrderDubboConsumerService;
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: 4/17/20 11:16 AM
 * @UpdateUser: linjinyu
 * @UpdateDate: 4/17/20 11:16 AM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class FlashdeliverConsumerListener {


	@Autowired
	private ReceiveFlashDeliverOrderDubboConsumerService receiveFlashDeliverOrderDubboConsumerService;


	/**
	 * 楼闪送 kafka 订单信息 开发环境测试
	 * ip    : 192.168.6.223
	 * port  : 9092
	 * topic : flashdeliver_order
	 *
	 * @param record
	 */
	@KafkaListener(topics = "${superdesk.app.services.flashdeliver.kafka.topic.order.main}")
	public void listenFlashdeliverOrder(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 {

					receiveFlashDeliverOrderDubboConsumerService.kafkaFlashDeliverOrderReceive(String.valueOf(message));

				} catch (Exception e) {
					log.error("kafka 服务之窗 楼闪送 订单 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 楼闪送 kafka 退单信息 开发环境测试
	 * ip    : 192.168.6.223
	 * port  : 9092
	 * topic : flashdeliver_order_refund
	 *
	 * @param record
	 */
	@KafkaListener(topics = "${superdesk.app.services.flashdeliver.kafka.topic.order.refund}")
	public void listenFlashdeliverOrderRefund(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 {
					receiveFlashDeliverOrderDubboConsumerService.kafkaFlashDeliverOrderRefundReceive(String.valueOf(message));

				} catch (Exception e) {
					log.error("kafka 服务之窗 楼闪送 退单 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * TODO 这个是什么鬼 楼闪送的? 确实是楼闪送用的
	 * 明白了 这只是订单状态更新的处理
	 * # unified kfk config
	 * superdesk.app.services.flashdeliver.kafka.topic.order.update_status=unifiedorderupdatedev
	 * <p>
	 * {"sn":"LS2020051100033","pay_state":"1","order_state":"4","order_state_name":"已配送","complet_state":"1","user_name":"szys","user_id":"1530","user_type":"8"}
	 * {"sn":"LS2020051200010","pay_state":"2","order_state":"3","order_state_name":"配送中","complet_state":"2","user_name":"w13544121819","user_id":"1528","user_type":"8"}
	 *
	 * @param record
	 */
	@KafkaListener(topics = "${superdesk.app.services.flashdeliver.kafka.topic.order.update_status}")
	public void listenFlashDeliverOrderStatusUpdate(ConsumerRecord<String, String> record) {

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

		if (kafkaMessage.isPresent()) {

			Object message = kafkaMessage.get();

			log.info("########## #msg #" + "\n" + record);
			log.info("########## #key #" + "\n" + record.key());
			log.info("########## #body#" + "\n" + message);

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

				try {

					receiveFlashDeliverOrderDubboConsumerService.kafkaFlashDeliverOrderStateUpdate(String.valueOf(message));

				} catch (Exception e) {
					log.error("kafka 服务之窗 楼闪送 订单 状态更新 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}

		}

	}


	/**
	 * 内部用 走队列而已 放差kafka不用再去开发队列我是傻嘛！
	 *
	 * @param record
	 */
	@KafkaListener(topics = "PATCH_FLASHDELIVER_ORDER_FIELD_OUT_VIRT")
	public void listenPatchFlashDeliverOrderFieldOutVirt(ConsumerRecord<String, String> record) {

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

		if (kafkaMessage.isPresent()) {

			Object message = kafkaMessage.get();

			log.info("########## #msg #" + "\n" + record);
			log.info("########## #key #" + "\n" + record.key());
			log.info("########## #body#" + "\n" + message);

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

				if ("patchUnifiedOrderFieldOutVirt".equals(String.valueOf(record.key()))) {
					receiveFlashDeliverOrderDubboConsumerService.kafkaFlashDeliverPatchUnifiedOrderFieldOutVirt(String.valueOf(message));
				}

			}

		}
	}
}


