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


import cn.superdesk.uniorder.dubbo.consumer.organisation.SysSupplierDubboConsumerService;
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: 7/7/20 3:11 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 7/7/20 3:11 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class ReceiveSupplierConsumerHandler {

	@Autowired
	private SysSupplierDubboConsumerService unifiedSupplierDubboConsumerService;

	/**
	 * 侨城汇 在用
	 * SELECT * FROM ims_kafka_consumer_record WHERE topic = 'superdesk-mall-receive_merchant'
	 * topic superdesk-mall-receive_merchant
	 * private Long   id;
	 * private String merchName;
	 *
	 * @param record
	 */
	@KafkaListener(topics = "${superdesk.mall.kafka.topic.organisation.supplier}")
	public void listenMerchant4SuperdeskMall_FLSC_QCH(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))) {

				if ("mergeReceiveMerchant".equals(String.valueOf(record.key()))) {

					unifiedSupplierDubboConsumerService.kafkaMergeReceiveMerchant4SuperdeskMallFLSC_QCH(String.valueOf(message));

				} else if ("deleteReceiveMerchant".equals(String.valueOf(record.key()))) {

					unifiedSupplierDubboConsumerService.kafkaDeleteReceiveMerchant4SuperdeskMallFLSC_QCH(String.valueOf(message));

				}
			}
		}

	}


	/***
	 * 企业采购 企业福利 在用
	 *
	 * SELECT * FROM ims_kafka_consumer_record WHERE topic = 'supplierSync'
	 *
	 * message_key = 'merchant'
	 *
	 * {
	 *     "saleId": "125",
	 *     "saleName": "深圳市众兴利华供应链有限公司",
	 *     "contactsName": "刘洪波",
	 *     "contactsPhone": "18630428076",
	 *     "contactsAdd": "",
	 *     "serviceNo": "FLSC"
	 * }
	 *
	 * 开发环境
	 * ip   : 192.168.6.223
	 * port : 9092
	 * topic: supplierSync
	 * message_key: mergeReceiveMerchant cu | deleteReceiveMerchant d
	 *
	 * @param record
	 */
	@KafkaListener(topics = {"${superdesk.mall.kafka.topic.supplier}"})
	public void listenMerchant4SuperdeskMall(ConsumerRecord<?, ?> 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))) {

				// 企业采购 企业福利 在用
				if ("merchant".equals(String.valueOf(record.key()))) {
					unifiedSupplierDubboConsumerService.kafkaMergeReceiveMerchant4SuperdeskMall(String.valueOf(message));
				}
			}
		}


	}
}