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.common.rest.response.R;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.proxyclient.ProxyClientOrderOptBecomeInvalidQueryCriteria;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.proxyclient.ProxyClientOrderReceiveParam;
import cn.superdesk.uniorder.dubbo.facade.order.receive.DubboReceiveProxyClientOrderService;
import cn.superdesk.uniorder.dubbo.facade.operation.DubboUnifiedOrderIssuesService;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.order.receive.ReceiveProxyClientOrderService;
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 DubboReceiveProxyClientOrderServiceImpl implements DubboReceiveProxyClientOrderService {

	@Autowired
	private ReceiveProxyClientOrderService proxyClientOrderReceiveKafkaService;

	@Autowired
	private CommonEventPublisher commonEventPublisher;

	/****************************************** 引用自身 Dubbo provider start ******************************************/
	@Reference(version = "1.0.0", timeout = 10000, retries = -1)
	private DubboUnifiedOrderIssuesService dubboUnifiedOrderIssuesService;

	/****************************************** kafka 代客下单 start ******************************************/

	@Override
	public void kafkaProxyClientOrderReceive(String message) {

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

		try {

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

			// TODO 在这里写吧
			String uid = AnStringUtils.getUUID();
			proxyClientOrderReceiveParam.setUid(uid);

			dubboUnifiedOrderIssuesService.developCall(message, AnJsonUtils.toJson(proxyClientOrderReceiveParam), proxyClientOrderReceiveParam.getOrderSn(), uid);

			UnifiedOrderIssuesFlowingParam callback = proxyClientOrderReceiveKafkaService.kafkaProxyClientOrderReceive(proxyClientOrderReceiveParam);

			commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		} catch (Exception e) {

			log.error("kafka 代客下单 订单 正负零单 同步 错误 \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}
	}

	@Override
	public R<String> proxyClientOrderOptBecomeInvalid(ProxyClientOrderOptBecomeInvalidQueryCriteria queryCriteria) {

		return proxyClientOrderReceiveKafkaService.proxyClientOrderOptBecomeInvalid(queryCriteria);

	}

	/****************************************** kafka 代客下单   end ******************************************/
}
