package cn.superdesk.uniorder.event.operationTrigger;

import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingParam;
import cn.superdesk.uniorder.openapicall.superdeskmall.service.FlscCallService;
import cn.superdesk.uniorder.service.operation.UnifiedOrderIssuesFlowingService;
import cn.superdesk.uniorder.service.operation.UnifiedOrderIssuesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

//import cn.superdesk.uniorder.dubbo.facade.operation.DubboUnifiedOrderIssuesService;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/6/25 下午4:28
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/6/25 下午4:28
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class UnifiedOrderDevelopCallBackListener {

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


    @Autowired
    private UnifiedOrderIssuesService unifiedOrderIssuesService;
    @Autowired
    private UnifiedOrderIssuesFlowingService unifiedOrderIssuesFlowingService;

    @Autowired
    private FlscCallService kafkaUnifiedOrderCallbackService;

    @Async
    @EventListener
    public void handleUnifiedOrderDevelopCallBackEvent(UnifiedOrderDevelopCallBackEvent event) {

        UnifiedOrderIssuesFlowingParam unifiedOrderIssuesFlowingParam = event.getUnifiedOrderIssuesFlowingParam();

        log.info("回调结算 {}", AnJsonUtils.toJson(unifiedOrderIssuesFlowingParam));

        // TODO 更新 流水

        // TODO 更新 订单 Map NND 这个为空是什么情况
        if (null != unifiedOrderIssuesFlowingParam) {
            unifiedOrderIssuesService
                    .developCallBack(
                            unifiedOrderIssuesFlowingParam.getProcessResult(),
                            unifiedOrderIssuesFlowingParam.getProcessReport(),
                            unifiedOrderIssuesFlowingParam.getUid()
                    );

            // TODO 20210926 add
            if (StrUtil.isNotBlank(unifiedOrderIssuesFlowingParam.getMessageId())) {

                kafkaUnifiedOrderCallbackService
                        .receiveCallback(
                                "DieString",
                                unifiedOrderIssuesFlowingParam.getMessageId(),
                                unifiedOrderIssuesFlowingParam.getProcessResult()
                        );
            }
        }


    }
}
