package cn.superdesk.uniorder.mq.consumer.superdeskapp.organisation;

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: 2021/5/7 上午9:27
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/5/7 上午9:27
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class Receive14MerchantConsumerHandler {

	@Autowired
	private SysSupplierDubboConsumerService unifiedSupplierDubboConsumerService;

	/**
	 * 消息类型	topic	key
	 * 商户新增	superdesk-app-transfer-receive_merchant	mergeReceiveMerchant
	 * 商户修改	superdesk-app-transfer-receive_merchant	mergeReceiveMerchant
	 * 商户删除	superdesk-app-transfer-receive_merchant	deleteReceiveMerchant
	 */


	/***
	 * 企业接收-服务之窗
	 *
	 * SELECT * FROM `ims_kafka_consumer_record` WHERE topic = 'superdesk-app-transfer-receive_virtualarchitecture'
	 *
	 * @param record
	 */
//	@KafkaListener(topics = {TopicReceiveConstants.TOPIC_RECEIVE_VIRTUALARCHITECTURE})
	@KafkaListener(topics = {"superdesk-app-transfer-receive_merchant"})
	public void listenVirt(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 ("mergeReceiveMerchant".equals(String.valueOf(record.key()))) {

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

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

					unifiedSupplierDubboConsumerService.kafkaDeleteReceiveMerchant4SuperdeskMall(String.valueOf(message));
				}
			}
		}

	}
}
