package cn.superdesk.uniorder.mq.consumer.superdeskapp.services.ipostoffice;

import cn.superdesk.uniorder.dubbo.consumer.order.receive.ReceivePostOfficeOrderDubboConsumerService;
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: 小邮局
 * @Author: linjinyu
 * @CreateDate: 2020/11/30 下午5:30
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020/11/30 下午5:30
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class PostOfficeConsumerListener {

	@Autowired
	private ReceivePostOfficeOrderDubboConsumerService receivePostOfficeOrderDubboConsumerService;

	@KafkaListener(topics = "${superdesk.app.services.postoffice.kafka.topic.order.main}")
	public void listenPostOfficeOrder(ConsumerRecord<String, String> 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 ("mergeReceiveZhyjOrder".equals(String.valueOf(record.key()))) {

					receivePostOfficeOrderDubboConsumerService.kafkaPostOfficeOrderMergeReceive(String.valueOf(message));

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

					receivePostOfficeOrderDubboConsumerService.kafkaPostOfficeOrderDeleteReceive(String.valueOf(message));

				}
			}
		}

	}
}
