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.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.flashdeliver.FlashDeliverOrderPatchUnifiedOrderFieldOutVirt;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.flashdeliver.FlashDeliverOrderReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.flashdeliver.FlashDeliverOrderStatusUpdateReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.flashdeliver.FlashDeliverRefundOrderReceiveParam;
import cn.superdesk.uniorder.dubbo.facade.operation.DubboUnifiedOrderIssuesService;
import cn.superdesk.uniorder.dubbo.facade.order.receive.DubboReceiveFlashDeliverOrderService;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.order.receive.ReceiveFlashDeliverOrderService;
import cn.superdesk.uniorder.service.order.receive.ReceiveFlashDeliverRefundOrderService;
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;

import java.util.List;

/**
 * @Description: kafka 服务之窗 楼闪送
 * @Author: linjinyu
 * @CreateDate: 2021/11/30 下午4:37
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/11/30 下午4:37
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 180000)
public class DubboReceiveFlashDeliverOrderServiceImpl implements DubboReceiveFlashDeliverOrderService {

    @Autowired
    private ReceiveFlashDeliverOrderService       flashDeliverOrderReceiveKafkaService;
    @Autowired
    private ReceiveFlashDeliverRefundOrderService flashDeliverOrderRefundReceiveKafkaService;

    @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> httpFlashDeliverOrderReceive(FlashDeliverOrderReceiveParam 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 = flashDeliverOrderReceiveKafkaService.kafkaFlashdeliverOrderReceive(orderReceiveParam);

        commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

        return response;
    }

    @Override
    public R<String> httpFlashDeliverOrderRefundReceive(FlashDeliverRefundOrderReceiveParam refundOrderReceiveParam) {

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

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

        UnifiedOrderIssuesFlowingParam callback = flashDeliverOrderRefundReceiveKafkaService.kafkaFlashdeliverOrderRefundReceive(refundOrderReceiveParam);

        commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

        return response;
    }

    @Override
    public void kafkaFlashDeliverOrderReceive(String message) {

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

        try {

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

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

            UnifiedOrderIssuesFlowingParam callback = flashDeliverOrderReceiveKafkaService.kafkaFlashdeliverOrderReceive(flashdeliverOrderReceiveParam);

            commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

        } catch (Exception e) {

            log.error("kafka 楼闪送 订单 同步 错误 \n " + ExceptionUtils.getMessage(e));

            e.printStackTrace();
        }
    }

    @Override
    public void kafkaFlashDeliverOrderRefundReceive(String message) {

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

        try {

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


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

            UnifiedOrderIssuesFlowingParam callback = flashDeliverOrderRefundReceiveKafkaService.kafkaFlashdeliverOrderRefundReceive(flashdeliverOrderRefundReceiveParam);

            commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

        } catch (Exception e) {
            log.error("kafka 楼闪送 退单 同步 错误 \n " + ExceptionUtils.getMessage(e));

            e.printStackTrace();
        }
    }

    @Override
    public void kafkaFlashDeliverOrderReceiveStateUpdate(String message) {

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

        try {
            FlashDeliverOrderStatusUpdateReceiveParam flashdeliverOrderStatusUpdateReceiveParam = AnJsonUtils.toObject(message, FlashDeliverOrderStatusUpdateReceiveParam.class);

            flashDeliverOrderReceiveKafkaService.kafkaFlashDeliverOrderReceiveStateUpdate(flashdeliverOrderStatusUpdateReceiveParam);

        } catch (Exception e) {

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

            e.printStackTrace();
        }

    }

    /****************************************** kafka 服务之窗 楼闪送   end ******************************************/

    /****************************************** kafka patchUnifiedOrderFieldOutVirt start ******************************************/

    @Override
    public R<List<UnifiedOrder>> patchFlashDeliverPatchUnifiedOrderFieldOutVirt(PageQueryParam pageQueryParam) {
        return flashDeliverOrderReceiveKafkaService.patchFlashDeliverPatchUnifiedOrderFieldOutVirt(pageQueryParam);
    }

    public void kafkaFlashDeliverPatchUnifiedOrderFieldOutVirt(String message) {

        try {

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

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

            UnifiedOrderIssuesFlowingParam callback = flashDeliverOrderReceiveKafkaService.kafkaFlashDeliverPatchUnifiedOrderFieldOutVirt(patchUnifiedOrderFieldOutVirt);

            commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

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

            e.printStackTrace();
        }
    }

    /****************************************** kafka patchUnifiedOrderFieldOutVirt   end ******************************************/
}
