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


import cn.superdesk.uniorder.dubbo.consumer.channel.UnifiedChannelProfitSyncDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.channel.UnifiedChannelProfitXVirtDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.channel.UnifiedChannelSyncDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedEncourageDubboConsumerService;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
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: 来源 superdesk-app-organisation-service 运营商 与 渠道商 同步
 * @Author: linjinyu
 * @CreateDate: 4/17/20 10:58 AM
 * @UpdateUser: linjinyu
 * @UpdateDate: 4/17/20 10:58 AM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class Receive12OperaterConsumerHandler {


	@Autowired
	private UnifiedEncourageDubboConsumerService          encourageDubboConsumerService;
	@Autowired
	private UnifiedChannelProfitSyncDubboConsumerService  unifiedChannelProfitSyncDubboConsumerService;
	@Autowired
	private UnifiedChannelProfitXVirtDubboConsumerService unifiedChannelProfitXVirtDubboConsumerService;
	@Autowired
	private UnifiedChannelSyncDubboConsumerService        unifiedChannelSyncDubboConsumerService;

	/**
	 * dz 1205
	 * linjinyu 重构 20200901
	 * 运营商 渠道商  同步
	 *
	 * @param record
	 */
	@KafkaListener(topics = {"${superdesk.app.services.organization.kafka.topic.channel.main}"})
	public void listenChannelOperater(ConsumerRecord<?, ?> record) {


		Optional<?> kafkaMessage = Optional.ofNullable(record.value());

		if (kafkaMessage.isPresent()) {

			Object message = kafkaMessage.get();

			log.info("########## #msg #" + "\n" + record);
			log.info("########## #key #" + "\n" + record.key());
			log.info("########## #body#" + "\n" + message);

			if (!StringUtils.isEmpty(String.valueOf(record.key())) && !StringUtils.isEmpty(String.valueOf(message))) {

				// 运营商|渠道商 ---- 新增
				// sw-un-operater-insert
				// 运营商|渠道商 ---- 修改
				// sw-un-operater-update
				// 运营商|渠道商 ---- 删除
				// sw-un-operater-delete

				try {

					if ("sw-un-operater-insert".equals(String.valueOf(record.key()))) {

						unifiedChannelSyncDubboConsumerService.mergeReceiveUnifiedChannelOperater(String.valueOf(message));

					} else if ("sw-un-operater-update".equals(String.valueOf(record.key()))) {

						unifiedChannelSyncDubboConsumerService.mergeReceiveUnifiedChannelOperater(String.valueOf(message));

					} else if ("sw-un-operater-delete".equals(String.valueOf(record.key()))) {

						unifiedChannelSyncDubboConsumerService.deleteReceiveUnifiedChannelOperater(String.valueOf(message));
					}

				} catch (Exception e) {
					log.error("kafka 运营商 | 渠道间 error\n{}", ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}

	}

	/***
	 * 渠道商 业务 分润 同步 dz 1205
	 * 重构 by linjinyu 20200526
	 * 来源 服务之窗 对接人是 姜勇
	 * @param record
	 */
	@KafkaListener(topics = {"${superdesk.app.services.organization.kafka.topic.channel.opt}"})
	public void listenChannelOperaterBusinessProfit(ConsumerRecord<?, ?> record) {


		Optional<?> kafkaMessage = Optional.ofNullable(record.value());

		if (kafkaMessage.isPresent()) {

			Object message = kafkaMessage.get();

			log.info("########## #msg #" + "\n" + record);
			log.info("########## #key #" + "\n" + record.key());
			log.info("########## #body#" + "\n" + message);

			if (!StringUtils.isEmpty(String.valueOf(record.key())) && !StringUtils.isEmpty(String.valueOf(message))) {

				// 渠道商业务分润新增
				// sw-un-businesspor-insert
				// 渠道商业务分润修改
				// sw-un-businesspor-update
				// 渠道商业务分润删除
				// sw-un-businesspor-delete

				try {

					if ("sw-un-businesspor-insert".equals(String.valueOf(record.key()))) {

						unifiedChannelProfitSyncDubboConsumerService.mergeReceiveUnifiedOperaterBusinessProfit(String.valueOf(message));

					} else if ("sw-un-businesspor-update".equals(String.valueOf(record.key()))) {

						unifiedChannelProfitSyncDubboConsumerService.mergeReceiveUnifiedOperaterBusinessProfit(String.valueOf(message));

					} else if ("sw-un-businesspor-delete".equals(String.valueOf(record.key()))) {

						unifiedChannelProfitSyncDubboConsumerService.deleteReceiveUnifiedOperaterBusinessProfit(String.valueOf(message));
					}

				} catch (Exception e) {
					log.error("kafka 渠道商 业务分润 error\n{}" + ExceptionUtils.getMessage(e));
					e.printStackTrace();
				}
			}
		}
	}


}
