package cn.superdesk.uniorder.dubbo.provider.order.receive;

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.numerical.AnStringUtils;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.ipostoffice.PostOfficeOrderReceiveParam;
import cn.superdesk.uniorder.dubbo.facade.order.receive.DubboReceivePostOfficeOrderService;
import cn.superdesk.uniorder.dubbo.facade.operation.DubboUnifiedOrderIssuesService;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.order.receive.ReceivePostOfficeOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: kafka 服务之窗 小邮局订单
 * @Author: linjinyu
 * @CreateDate: 2021/11/30 下午4:38
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/11/30 下午4:38
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 180000)
public class DubboReceivePostOfficeOrderServiceImpl implements DubboReceivePostOfficeOrderService {

	@Autowired
	private ReceivePostOfficeOrderService postOfficeOrderReceiveKafkaService;

	@Autowired
	private CommonEventPublisher commonEventPublisher;

	/****************************************** 引用自身 Dubbo provider start ******************************************/

	@Reference(version = "1.0.0", timeout = 10000, retries = -1)
	private DubboUnifiedOrderIssuesService dubboUnifiedOrderIssuesService;

	/****************************************** kafka 服务之窗 小邮局订单 start ******************************************/

	@Override
	public void kafkaPostOfficeOrderMergeReceive(String message) {

		try {

			PostOfficeOrderReceiveParam postOfficeOrderReceiveParam = AnJsonUtils.toObject(message, PostOfficeOrderReceiveParam.class);
			log.info("\n转换:\n{}", AnJsonUtils.toJson(postOfficeOrderReceiveParam));

			// TODO 在这里写吧
			String uid = AnStringUtils.getUUID();
			postOfficeOrderReceiveParam.setUid(uid);
			dubboUnifiedOrderIssuesService.developCall(message, AnJsonUtils.toJson(postOfficeOrderReceiveParam), postOfficeOrderReceiveParam.getOrderSn(), uid);

			UnifiedOrderIssuesFlowingParam callback = postOfficeOrderReceiveKafkaService.kafkaPostOfficeOrderMergeReceive(postOfficeOrderReceiveParam);

			commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		} catch (Exception e) {
			log.error("kafka 小邮局 同步 错误 \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}
	}

	@Override
	public void kafkaPostOfficeOrderDeleteReceive(String message) {

		try {
			postOfficeOrderReceiveKafkaService.kafkaPostOfficeOrderDeleteReceive(message);
		} catch (Exception e) {
			log.error("kafka 小邮局 订单 退单 同步 错误 \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}
	}

	/****************************************** kafka 服务之窗 小邮局订单   end ******************************************/
}
