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

import cn.hutool.core.util.StrUtil;
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.common.rest.response.R;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.unified.qyng.UnifiedQyngOrderReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.unified.qyng.refund.UnifiedQyngRefundOrderReceiveParam;
import cn.superdesk.uniorder.dubbo.facade.order.receive.DubboReceiveMallQyngOrderService;
import cn.superdesk.uniorder.dubbo.facade.operation.DubboUnifiedOrderIssuesService;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.order.receive.ReceiveQyngOrderService;
import cn.superdesk.uniorder.service.order.receive.ReceiveQyngRefundOrderService;
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: Java类作用描述
 * @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 DubboReceiveMallQyngOrderServiceImpl implements DubboReceiveMallQyngOrderService {

	@Autowired
	private ReceiveQyngOrderService       receiveQyngOrderService;
	@Autowired
	private ReceiveQyngRefundOrderService receiveQyngRefundOrderService;

	@Autowired
	private CommonEventPublisher                 commonEventPublisher;

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

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

	/****************************************** kafka 一般订单 福利商城 start ******************************************/

	@Override
	public R<String> httpUnifiedQyngOrderReceive(UnifiedQyngOrderReceiveParam unifiedQyngOrderReceiveParam) {

		R<String> response = new R<>();

		String uid = AnStringUtils.getUUID();
		unifiedQyngOrderReceiveParam.setUid(uid);
		dubboUnifiedOrderIssuesService.developCall(AnJsonUtils.toJson(unifiedQyngOrderReceiveParam), AnJsonUtils.toJson(unifiedQyngOrderReceiveParam), unifiedQyngOrderReceiveParam.getOrderSn(), uid);

		UnifiedOrderIssuesFlowingParam callback = receiveQyngOrderService.kafkaUnifiedQyngOrderReceive(unifiedQyngOrderReceiveParam);
		if (StrUtil.isNotBlank(unifiedQyngOrderReceiveParam.getMessageId())) {
			callback.setMessageId(unifiedQyngOrderReceiveParam.getMessageId());
		}

		commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		return response;
	}

	@Override
	public R<String> httpUnifiedQyngRefundOrderReceive(UnifiedQyngRefundOrderReceiveParam unifiedOrderRefundReceiveParam) {

		R<String> response = new R<>();

		String uid = AnStringUtils.getUUID();
		unifiedOrderRefundReceiveParam.setUid(uid);
		dubboUnifiedOrderIssuesService.developCall(AnJsonUtils.toJson(unifiedOrderRefundReceiveParam), AnJsonUtils.toJson(unifiedOrderRefundReceiveParam), unifiedOrderRefundReceiveParam.getOrder().getRefundOrderSn(), uid);

		UnifiedOrderIssuesFlowingParam callback = receiveQyngRefundOrderService.kafkaUnifiedQyngRefundOrderReceive(unifiedOrderRefundReceiveParam);

		commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		return response;
	}


	@Override
	public void kafkaUnifiedQyngOrderReceive(String message) {

		log.info("##################### kafkaUnifiedOrderReceive start #####################\n原始:\n{}", message);

		try {

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

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

			UnifiedOrderIssuesFlowingParam callback = receiveQyngOrderService.kafkaUnifiedQyngOrderReceive(unifiedOrderReceiveParam);
			if (StrUtil.isNotBlank(unifiedOrderReceiveParam.getMessageId())) {
				callback.setMessageId(unifiedOrderReceiveParam.getMessageId());
			}

			commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		} catch (Exception e) {

			log.error("kafka 商城 订单 超级前台(一般) 正单 同步 错误 \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}
	}

	@Override
	public void kafkaUnifiedQyngRefundOrderReceive(String message) {

		log.info("\n##################### kafkaUnifiedOrderRefundReceive start #####################\n原始:\n{}", message);

		try {

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

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

			UnifiedOrderIssuesFlowingParam callback = receiveQyngRefundOrderService.kafkaUnifiedQyngRefundOrderReceive(unifiedOrderRefundReceiveParam);

			commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		} catch (Exception e) {

			log.error("kafka 商城 订单 超级前台(一般) 退单 同步 错误 \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}


	}

	/****************************************** kafka 一般订单 福利商城   end ******************************************/
}
