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.score.ScoreOrderReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.score.refund.ScoreOrderRefundReceiveParam;
import cn.superdesk.uniorder.dubbo.facade.order.receive.DubboReceiveMallFlscQchOrderService;
import cn.superdesk.uniorder.dubbo.facade.operation.DubboUnifiedOrderIssuesService;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.order.receive.ReceiveFlscQchOrderService;
import cn.superdesk.uniorder.service.order.receive.ReceiveFlscQchRefundOrderService;
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 DubboReceiveMallFlscQchOrderServiceImpl implements DubboReceiveMallFlscQchOrderService {

	@Autowired
	private ReceiveFlscQchOrderService       scoreOrderReceiveKafkaService;
	@Autowired
	private ReceiveFlscQchRefundOrderService scoreOrderRefundReceiveKafkaService;

	@Autowired
	private CommonEventPublisher commonEventPublisher;

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

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

	/****************************************** kafka 积分订单 福利商城_侨城汇 start ******************************************/

	/**
	 * http 积分订单 mark by linjinyu at 20210308
	 *
	 * @param orderReceiveParam
	 * @return
	 */
	@Override
	public R<String> httpScoreOrderReceive(ScoreOrderReceiveParam orderReceiveParam) {

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

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

		UnifiedOrderIssuesFlowingParam callback = scoreOrderReceiveKafkaService.kafkaScoreOrderReceive(orderReceiveParam);

		commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		return response;
	}

	/**
	 * http 积分订单 mark by linjinyu at 20210308
	 *
	 * @param refundOrderReceiveParam
	 * @return
	 */
	@Override
	public R<String> httpScoreOrderRefundReceive(ScoreOrderRefundReceiveParam refundOrderReceiveParam) {

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

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

		UnifiedOrderIssuesFlowingParam callback = scoreOrderRefundReceiveKafkaService.kafkaScoreOrderRefundReceive(refundOrderReceiveParam);

		commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		return response;
	}

	@Override
	public void kafkaScoreOrderReceive(String message) {

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

		try {

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

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

			UnifiedOrderIssuesFlowingParam callback = scoreOrderReceiveKafkaService.kafkaScoreOrderReceive(scoreOrderReceiveParam);

			commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		} catch (Exception e) {

			log.error("kafka 商城 订单 侨城汇(积分) 正单 同步 错误 \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}
	}

	@Override
	public void kafkaScoreOrderRefundReceive(String message) {

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

		try {

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


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

			UnifiedOrderIssuesFlowingParam callback = scoreOrderRefundReceiveKafkaService.kafkaScoreOrderRefundReceive(scoreOrderRefundReceiveParam);

			commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

		} catch (Exception e) {
			log.error("kafka 商城 订单 侨城汇(积分) 退单 同步 错误 \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}

	}

	/****************************************** kafka 积分订单 福利商城_侨城汇   end ******************************************/
}
