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

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

	@Autowired
	private SysCliqueOrgDubboConsumerService sysCliqueOrgDubboConsumerService;

	/**
	 * 消息类型	topic	key
	 * 集团-项目关联新增	superdesk-app-transfer-receive_clique_org	mergeReceiveCliqueOrg
	 * 集团-项目关联修改	superdesk-app-transfer-receive_clique_org	mergeReceiveCliqueOrg
	 * 集团-项目关联删除	superdesk-app-transfer-receive_clique_org	deleteReceiveCliqueOrg
	 */

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

					sysCliqueOrgDubboConsumerService.kafkaMergeReceiveCliqueOrg(String.valueOf(message));

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

					sysCliqueOrgDubboConsumerService.kafkaDeleteReceiveCliqueOrg(String.valueOf(message));
				}
			}
		}

	}
}
