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

import cn.superdesk.uniorder.dubbo.consumer.order.GoodsDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.OrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.SysOderUserDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedEncourageDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedSupplierDubboConsumerService;
import cn.superdesk.uniorder.utils.exception.ExceptionUtils;
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.List;
import java.util.Map;
import java.util.Optional;


@Slf4j
@Component
public class ReceiveSuperdeskMallConsumerHandler {


	@Autowired
	private OrderDubboConsumerService            orderDubboConsumerService;
	@Autowired
	private UnifiedOrderDubboConsumerService     unifiedOrderDubboConsumerService;
	@Autowired
	private GoodsDubboConsumerService            goodsDubboConsumerService;
	@Autowired
	private SysOderUserDubboConsumerService      sysOderUserDubboConsumerService;
	@Autowired
	private UnifiedEncourageDubboConsumerService unifiedEncourageDubboConsumerService;
	@Autowired
	private UnifiedSupplierDubboConsumerService  unifiedSupplierDubboConsumerService;


	/**
	 * 老的订单数据
	 * <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.order.main}")
	public void listenOrderUnified(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);

			Map  jsonMap   = com.alibaba.fastjson.JSON.parseObject(record.value(), Map.class);
			List goodsList = (List<Map>) jsonMap.get("goods");

			unifiedOrderDubboConsumerService.kafkaUnifiedOrderReceive(jsonMap, goodsList);//dai add 20190903

		}
	}


//	备份
//	@KafkaListener(topics = "${superdesk.mall.kafka.topic.order.main}")
//	public void listenOrder(ConsumerRecord<String, String> data) {
//
//		//Map jsonMap=com.alibaba.fastjson.JSON.parseObject(data.value(), Map.class);
//		//Long orderid=Long.valueOf((Integer)jsonMap.get("orderid"));
//
//		//System.out.println("offset====>"+String.valueOf(data.offset()));//消费者的位置
//		//System.out.println("orderid====>"+orderid);
//
//		String topic = data.topic();//消费的topic
//
//		log.info("-------------recieve message from {} topic-------------", topic);
//		log.info("data.String:{}", data);
//		log.info("partition:{}", String.valueOf(data.partition()));//消费的topic的分区
//		log.info("offset:{}", String.valueOf(data.offset()));//消费者的位置
//		log.info("get message from {} topic : {}", topic, data.value());//接收到的消息
//
//		//Map jsonMap=(Map<String,String>)gson.fromJson(data.value(), Map.class);
//
//		Map  jsonMap   = com.alibaba.fastjson.JSON.parseObject(data.value(), Map.class);
//		List goodsList = (List<Map>) jsonMap.get("goods");
//
//		log.info("get message jsonMap {} topic : {}", topic, jsonMap.toString());//接收到的消息
//
//		//kaFkaSendOrder(jsonMap, goodsList);
//		//orderDubboService.kaFkaSendOrder(jsonMap, goodsList);//加了事务
//
//		unifiedorderDubboService.kaFkaOrderSync(jsonMap, goodsList);//dai add 20190903
//	}

	/**
	 * 退货数据 add by qzy
	 *
	 * @param record
	 */
	@KafkaListener(topics = "${superdesk.mall.kafka.topic.order.refund}")
	public void listenOrderRefund(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);

			Map jsonMap = com.alibaba.fastjson.JSON.parseObject(record.value(), Map.class);

			orderDubboConsumerService.kafkaUnifiedOrderReturn(jsonMap);//加了事务

		}

	}





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

				} catch (Exception e) {
					log.error("kafka 侨城汇 分润 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}

	}


}


