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


import cn.superdesk.app.transfer.constants.receive.TopicReceiveConstants;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysOrganizationDubboConsumerService;
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 Receive02OrganizationConsumerHandler {

	@Autowired
	private SysOrganizationDubboConsumerService organizationDubboConsumerService;

	/***
	 * 项目接收-服务之窗 dz 1031
	 *
	 * SELECT * FROM `ims_kafka_consumer_record` WHERE topic = 'superdesk-app-transfer-receive_organization'
	 *
	 * @param record
	 */
	@KafkaListener(topics = {TopicReceiveConstants.TOPIC_RECEIVE_ORGANIZATION})
	public void listenProject(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 ("mergeReceiveOrganization".equals(String.valueOf(record.key()))) {
					log.info("kafka-{}-{}}", TopicReceiveConstants.TOPIC_RECEIVE_ORGANIZATION, "mergeReceiveOrganization");
					organizationDubboConsumerService.mergeReceiveOrganization(String.valueOf(message));

				} else if ("deleteReceiveOrganization".equals(String.valueOf(record.key()))) {
					log.info("kafka-{}-{}}", TopicReceiveConstants.TOPIC_RECEIVE_ORGANIZATION, "deleteReceiveOrganization");
					organizationDubboConsumerService.deleteReceiveOrganization(String.valueOf(message));
				}
			}
		}
	}
}
