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

import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.enums.common.CommonProcessResultEnum;
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.opcenter.UnifiedFlscAccountingOrderParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.unified.flsc.UnifiedFlscOrderReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.unified.flsc.refund.UnifiedFlscRefundOrderReceiveParam;
import cn.superdesk.uniorder.dubbo.facade.order.receive.DubboReceiveMallFlscOrderService;
import cn.superdesk.uniorder.dubbo.facade.operation.DubboUnifiedOrderIssuesService;
import cn.superdesk.uniorder.event.CommonEventPublisher;
import cn.superdesk.uniorder.service.order.receive.ReceiveFlscOrderService;
import cn.superdesk.uniorder.service.order.receive.ReceiveFlscRefundOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @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 DubboReceiveMallFlscOrderServiceImpl implements DubboReceiveMallFlscOrderService {


    @Autowired
    private ReceiveFlscOrderService receiveFlscOrderService;
    @Autowired
    private ReceiveFlscRefundOrderService receiveFlscRefundOrderService;

    @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> httpUnifiedFlscOrderReceive(UnifiedFlscOrderReceiveParam unifiedFlscOrderReceiveParam) {

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

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

        UnifiedOrderIssuesFlowingParam callback = receiveFlscOrderService.kafkaUnifiedOrderReceive(unifiedFlscOrderReceiveParam);
        if (StrUtil.isNotBlank(unifiedFlscOrderReceiveParam.getMessageId())) {
            callback.setMessageId(unifiedFlscOrderReceiveParam.getMessageId());
        }

        commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

        return response;
    }

    @Override
    public R<String> httpUnifiedFlscRefundOrderReceive(UnifiedFlscRefundOrderReceiveParam 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 = receiveFlscRefundOrderService.kafkaUnifiedOrderRefundReceive(unifiedOrderRefundReceiveParam);

        commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

        return response;
    }

    /**
     * kafka 福利商城推送对账订单数据
     *
     * @param message
     */
    @Override
    public void kafkaUnifiedFlscAccountingOrderReceive(String message) {
        log.info("##################### kafkaUnifiedFlscAccountingOrderReceive start #####################\n原始:\n{}", message);
        String uid = AnStringUtils.getUUID();
        try {
            UnifiedFlscAccountingOrderParam unifiedFlscAccountingOrderParam = JSON.parseObject(message, UnifiedFlscAccountingOrderParam.class);

            log.info("\n转换:\n{}", JSON.toJSONString(unifiedFlscAccountingOrderParam));

            // 记录原始数据和转换后数据
            unifiedFlscAccountingOrderParam.setUid(uid);
            dubboUnifiedOrderIssuesService.developCall(
                    message,
                    JSON.toJSONString(unifiedFlscAccountingOrderParam),
                    unifiedFlscAccountingOrderParam.getOrderSn(),
                    uid
            );

            UnifiedOrderIssuesFlowingParam callback = receiveFlscOrderService.kafkaUnifiedFlscAccountingOrderReceive(unifiedFlscAccountingOrderParam);
            if (StrUtil.isNotBlank(unifiedFlscAccountingOrderParam.getMessageId())) {
                callback.setMessageId(unifiedFlscAccountingOrderParam.getMessageId());
            }

            commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

        } catch (Exception e) {
            log.error("kafka-超级前台福利商城 对账订单 同步错误 \n", e);
            UnifiedOrderIssuesFlowingParam callback = new UnifiedOrderIssuesFlowingParam();
            callback.setUid(uid);
            callback.setProcessResult(CommonProcessResultEnum.异常_抛错.getKey());
            callback.setProcessReport(ExceptionUtils.getMessage(e));
            commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);
        }

    }


    @Override
    public void kafkaUnifiedFlscOrderReceive(String message) {

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

        String uid = AnStringUtils.getUUID();

        try {

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

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

            UnifiedOrderIssuesFlowingParam callback = receiveFlscOrderService.kafkaUnifiedOrderReceive(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();


            //            UnifiedOrderIssuesFlowingParam
            //                    .builder()
            //                    .uid(uid)
            //
            //                    .processResult(CommonProcessResultEnum.异常_抛错.getKey())
            //                    .processReport(ExceptionUtils.getMessage(e))
            //                    .build()
            UnifiedOrderIssuesFlowingParam callback = new UnifiedOrderIssuesFlowingParam();
            callback.setUid(uid);
            callback.setProcessResult(CommonProcessResultEnum.异常_抛错.getKey());
            callback.setProcessReport(ExceptionUtils.getMessage(e));

            commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);
        }
    }

    @Override
    public void kafkaUnifiedFlscRefundOrderReceive(String message) {

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

        try {

            UnifiedFlscRefundOrderReceiveParam unifiedOrderRefundReceiveParam = AnJsonUtils.toObject(message, UnifiedFlscRefundOrderReceiveParam.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 = receiveFlscRefundOrderService.kafkaUnifiedOrderRefundReceive(unifiedOrderRefundReceiveParam);

            commonEventPublisher.triggerUnifiedOrderDevelopCallBack(callback);

        } catch (Exception e) {

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

            e.printStackTrace();
        }


    }

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