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


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

	@Autowired
	private SysCompanyDubboConsumerService kfkCompanyDubboConsumerService;

	/***
	 * 运营中心接收-新服务之窗 dz 1031
	 *
	 * SELECT * FROM `ims_kafka_consumer_record` WHERE topic = 'superdesk-app-transfer-receive_company'
	 *
	 * @param record
	 */
	@KafkaListener(topics = {TopicReceiveConstants.TOPIC_RECEIVE_COMPANY})
	public void listenCompany(ConsumerRecord<?, ?> record) {

		log.info("-------------recieve message from {} topic-------------", TopicReceiveConstants.TOPIC_RECEIVE_COMPANY);
		log.info("data.String:{}", record);
		// 消费的topic的分区
		log.info("key:{}", String.valueOf(record.key()));
		// 消费的topic的分区
		log.info("partition:{}", String.valueOf(record.partition()));
		// 消费者的位置
		log.info("offset:{}", String.valueOf(record.offset()));
		// 接收到的消息
		log.info("get message from {} topic : {}", TopicReceiveConstants.TOPIC_RECEIVE_COMPANY, record.value());

		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 ("mergeReceiveCompany".equals(String.valueOf(record.key()))) {
					log.info("kafka-{}-{}}", TopicReceiveConstants.TOPIC_RECEIVE_COMPANY, "mergeReceiveCompany");
					kfkCompanyDubboConsumerService.mergeReceiveCompany(String.valueOf(message));
				} else if ("deleteReceiveCompany".equals(String.valueOf(record.key()))) {
					log.info("kafka-{}-{}}", TopicReceiveConstants.TOPIC_RECEIVE_COMPANY, "deleteReceiveCompany");
					kfkCompanyDubboConsumerService.deleteReceiveCompany(String.valueOf(message));
				}
			}
		}
	}
}
