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


import cn.superdesk.app.transfer.constants.receive.TopicReceiveConstants;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysEnterpriseDubboConsumerService;
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: 对位 canal
 * @Author: linjinyu
 * @CreateDate: 4/17/20 10:53 AM
 * @UpdateUser: linjinyu
 * @UpdateDate: 4/17/20 10:53 AM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class Receive04VirtualarchitectureConsumerHandler {

	@Autowired
	private SysEnterpriseDubboConsumerService sysEnterpriseDubboConsumerService;

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

					sysEnterpriseDubboConsumerService.mergeReceiveVirtualarchitecture4SuperdeskTransfer(String.valueOf(message));

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

					sysEnterpriseDubboConsumerService.deleteReceiveVirtualarchitecture4SuperdeskTransfer(String.valueOf(message));
				}
			}
		}

	}
}
