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

import cn.superdesk.uniorder.dubbo.consumer.organisation.SysCliqueDubboConsumerService;
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;

@Slf4j
@Component
public class Receive12CliqueConsumerHandler {

	@Autowired
	private SysCliqueDubboConsumerService sysCliqueDubboConsumerService;

	/**
	 * 消息类型	topic	key
	 * 集团新增	superdesk-app-transfer-receive_clique	mergeReceiveClique
	 * 集团修改	superdesk-app-transfer-receive_clique	mergeReceiveClique
	 * 集团删除	superdesk-app-transfer-receive_clique	deleteReceiveClique
	 */

	/***
	 * 企业接收-服务之窗
	 *
	 * 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_clique"})
	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 ("mergeReceiveClique".equals(String.valueOf(record.key()))) {

					sysCliqueDubboConsumerService.kafkaMergeReceiveClique(String.valueOf(message));

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

					sysCliqueDubboConsumerService.kafkaDeleteReceiveClique(String.valueOf(message));
				}
			}
		}

	}
}
