package cn.pluss.platform.ryx.pay.impl;


import cn.pluss.platform.api.ResultCode;
import cn.pluss.platform.constants.Constant;
import cn.pluss.platform.dto.MemberScanPayDTO;
import cn.pluss.platform.dto.MerChantOrderDTO;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.payAreaRyx.PayAreaRyxService;
import cn.pluss.platform.refundOrder.MerchantRefundOrderService;
import cn.pluss.platform.region.RegionReflectService;
import cn.pluss.platform.ryx.RyxConstants;
import cn.pluss.platform.ryx.RyxService;
import cn.pluss.platform.ryx.pay.RyxPayService;
import cn.pluss.platform.util.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ExecutorService;


@Service
@Slf4j
public  class RyxPayServiceimpl implements RyxPayService {


    @Resource
    private RyxService ryxService;

    @Setter(onMethod_ = {@Lazy, @Autowired})
    private MerchantOrderService merchantOrderService;

    @Setter(onMethod_ = {@Lazy, @Autowired})
    private MerchantRefundOrderService merchantRefundOrderService;

    @Resource
    private ExecutorService executorService;

    @Resource
    private RegionReflectService regionReflectService;

    @Resource
    private PayAreaRyxService payAreaRyxService;

    /**
     * 瑞银信被扫支付
     * @param order
     * @param channel
     * @return
     */
    @Override
    public JSONObject tradePay(MerchantOrder order, MerchantChannelStatus channel,MerchantBaseInfo merchant) {
        log.info("==============>【瑞银信】被扫交易下单请求，请求参数：{}<==================",JSONObject.toJSONString(order));
        JSONObject reqData = new JSONObject();
        reqData.put("merchantCode",channel.getMerchantId());
        merchant.setChannelLimitPay(reqData,channel.getChannel());
        ryxSetAreaInfo(reqData,merchant);
        String payWay = "WXZF";
        String barCode = RyxConfig.barCodePayService;
        if(StringUtil.isNotEmpty(order.getScanType()) && "01".equals(order.getScanType())){
            barCode = RyxConfig.qrActiveCodePay;
            payWay = "wechatPay".equals(order.getPayTypeCode()) ? "WXZF" : "aliPay".equals(order.getPayTypeCode()) ? "ZFBZF" : "UPZF";
        }else{
            if("wechatPay".equals(order.getPayTypeCode())){
                barCode = RyxConfig.barCodePayApplets;
                reqData.put("userId",order.getMercUserId());
                reqData.put("subAppId",ParametersUtil.APPLETS_APPID);
            }else if("aliPay".equals(order.getPayTypeCode())){
                payWay = "ZFBZF";
                reqData.put("buyerId",order.getMercUserId());
            }else{
                barCode = RyxConfig.barCodeUnionJsPay;
                payWay = "UPZF";
                reqData.put("userId",order.getMercUserId());
                reqData.put("customerIp","127.0.0.1");
                reqData.put("frontUrl", ParametersUtil.domain + "/wap/merchant/jumpUrl?orderNumber=" + order.getOrderNumber());
                //前台失败通知地址
                reqData.put("frontFailUrl", ParametersUtil.domain + "/wap/merchant/jumpUrl?orderNumber=" + order.getOrderNumber());
            }
        }
        reqData.put("scene",1);
        reqData.put("payWay",payWay);
        reqData.put("totalAmount",order.getConsumeFee());
        if(StringUtil.isNotEmpty(order.getSubject())){
            reqData.put("subject",order.getSubject());
        }else{
            reqData.put("subject",order.getMerchantName());
        }
        reqData.put("desc",order.getRemark());
        reqData.put("expireTime",15);
        reqData.put("reqMsgId",order.getOrderNumber());
        if(StringUtil.isNotEmpty(order.getExtendParam())){
            reqData.put(Constant.RYX_PAY_ECHO_KEY_NAME,order.getExtendParam());
        }

        JSONObject respMap = ryxService.analysisCommonPayParams(RyxConfig.payUrl,barCode,reqData);

        log.info("==============>【瑞银信】被扫交易下单请求，返回参数解析成功：{}<==================",JSONObject.toJSONString(respMap));

        String respCode = respMap.getString("respCode");

        String respType = respMap.getString("respType");

        String respMsg = respMap.getString("respMsg");

        JSONObject result = new JSONObject(16);

        result.put("code",ResultCode.SUCCESS.code());

        result.put("msg",respMsg);

        if(!RyxConfig.PAY_SUCCESS_CODE.equals(respCode)){
            result.put("code",ResultCode.FAIL.code());
            if(respMsg.contains("商户需补齐相关资料")){
                result.put("msg","D1次日到账：商户未在收银呗APP-商户认证-微信认证内完成授权，暂时无法微信收款");
            }
            return result;
        }
        if(!"S".equals(respType)){
            result.put("code",ResultCode.FAIL.code());
            return result;
        }
        JSONObject data = respMap.getJSONObject("data");

        JSONObject payData = new JSONObject();
        if(StringUtil.isNotEmpty(order.getScanType()) && "01".equals(order.getScanType())){
            payData.put("payUrl",data.getString("qrCode"));
            payData.put("orderNumber",order.getOrderNumber());
            payData.put("mercOrderNo",order.getMercOrderNo());
            payData.put("payAmt",order.getConsumeFee().toString());
        }else{
            if("wechatPay".equals(order.getPayTypeCode())){
                JSONObject wxPayParams = data.getJSONObject("wxjsapiStr");
                payData.put("payAppId",wxPayParams.getString("appId"));
                payData.put("payTimeStamp",wxPayParams.getString("timeStamp"));
                payData.put("paynonceStr",wxPayParams.getString("nonceStr"));
                payData.put("payPackage",wxPayParams.getString("package"));
                payData.put("paySignType",wxPayParams.getString("signType"));
                payData.put("paySign",wxPayParams.getString("paySign"));
            }else if("aliPay".equals(order.getPayTypeCode())){
                payData.put("source",data.getString("channelTradeNo").substring(2));
            }else{
                payData.put("redirectUrl",data.get("redirectUrl"));
            }
        }
        payData.put("channel",channel.getChannel());
        result.put("transNo",respMap.getString("smzfMsgId"));
        order.setTransNo(respMap.getString("smzfMsgId"));
        result.put("payData",payData);
        return result;
    }

    /**
     * 换取瑞银信区域信息
     * @date: 2022/2/23 16:04
     * @param reqData:
     * @param merchant:
     */
    private void ryxSetAreaInfo(JSONObject reqData, MerchantBaseInfo merchant) {
        RegionCodeRyx ryxRegionCode = regionReflectService.getRyxRegionCode(merchant.getProvince(), merchant.getCity(), merchant.getDistrict());
        if(ryxRegionCode != null){
            PayAreaRyx payAreaRyx = payAreaRyxService.getByArea(ryxRegionCode.getProvince(),ryxRegionCode.getCity(),ryxRegionCode.getArea());
            if(payAreaRyx != null){
                reqData.put("areaInfo",payAreaRyx.getAreaCode());
            }else {
                setRyxCommentAreaInfo(reqData,merchant);
            }
        }else{
            setRyxCommentAreaInfo(reqData,merchant);
        }
    }

    private void setRyxCommentAreaInfo(JSONObject reqData, MerchantBaseInfo merchant) {
        String addressNo = merchant.getAddressNo();
        if(StringUtil.isNotEmpty(addressNo)){
            String[] areaArr = addressNo.split("-");
            String areaInfo = areaArr[areaArr.length - 1];
            reqData.put("areaInfo",areaInfo);
        }
    }
    /**
     * 瑞银信主扫支付
     * @param merchantOrderDTO
     * @param order
     * @return
     */
    @Override
    public JSONObject tradePay(MerChantOrderDTO merchantOrderDTO, MerchantOrder order) {
        JSONObject object = new JSONObject();
        object.put("merchantCode",merchantOrderDTO.getChannelNo());
        object.put("payWay","aliPay".equals(merchantOrderDTO.getPayTypeCode()) ? "ZFBZF": "wechatPay".equals(merchantOrderDTO.getPayTypeCode()) ? "WXZF" : "UPZF" );
        object.put("scene","1");
        object.put("authCode",merchantOrderDTO.getAuthCode());
        object.put("totalAmount",order.getConsumeFee());
        object.put("subject",merchantOrderDTO.getMerchantName());
        object.put("desc",merchantOrderDTO.getRemark());
        object.put("reqMsgId",order.getOrderNumber());
        ryxSetAreaInfo(object,merchantOrderDTO.getMerchantBaseInfo());
        if(merchantOrderDTO.getMerchantBaseInfo() != null){
            merchantOrderDTO.getMerchantBaseInfo().setChannelLimitPay(object,Integer.valueOf(order.getAisleSwitch()));
        }
        JSONObject respMap = ryxService.analysisCommonPayParams(RyxConfig.payUrl,RyxConfig.barCodePayWay,object);

        log.info("==============>【瑞银信】主扫交易下单请求，返回参数解析成功：{}<==================",JSONObject.toJSONString(respMap));

        JSONObject obj = new JSONObject(6);

        obj.put("code", ResultCode.FAIL.code());

        obj.put("msg","处理成功");

        String respCode = respMap.getString("respCode");

        String respMsg = respMap.getString("respMsg");
        //响应成功
        if(RyxConfig.PAY_SUCCESS_CODE.equals(respCode) || RyxConfig.PAY_AWAIT_CODE.equals(respCode)){
            obj.put("code", ResultCode.SUCCESS.code());
            obj.put("msg","处理成功");
            String respType = respMap.getString("respType");
            JSONObject data = respMap.getJSONObject("data");
            order.setMercUserId(data.getString("buyerId"));
            obj.put("transNo",respMap.getString("smzfMsgId"));
            order.setTransNo(respMap.getString("smzfMsgId"));
            if(StringUtil.isNotEmpty(data.getString("channelTradeNo"))){
                String channelOrderNo = data.getString("channelTradeNo");
                if("aliPay".equals(order.getPayTypeCode())){
                    obj.put("channelOrderNo",channelOrderNo.substring(2));
                }else{
                    obj.put("channelOrderNo",channelOrderNo);
                }
            }
            if("S".equals(respType)){
                String payTime = data.getString("payTime");
                //通道订单号
                obj.put("payTime",DateUtils.parse(payTime,"yyyyMMddHHmmss"));
                obj.put("payStatus","1");
            }else if("R".equals(respType)){
                //支付中
                obj.put("code", ResultCode.TRANSUNKNOW.code());
                obj.put("msg","等待支付中...");
                obj.put("payStatus","7");
            }else{
                obj.put("payStatus","3");
            }
            String drType = data.getString("payType");
            order.setDrType(drType);
            if(!Constant.PAY_TYPE_YSFPAY.equals(order.getPayTypeCode())){
                order.setChannelRate(RyxConstants.CHANNEL_WECHAT_ALIPAY_COST_RATE.multiply(new BigDecimal(10000)));
                order.setChannelFee(new BigDecimal(String.valueOf(order.getConsumeFee())).multiply(RyxConstants.CHANNEL_WECHAT_ALIPAY_COST_RATE).setScale(2,BigDecimal.ROUND_HALF_UP));
            }else{
                if(order.getConsumeFee() <= 1000.0d){
                    order.setChannelRate(RyxConstants.CHANNEL_BANK_COST_RATE.multiply(new BigDecimal(10000)));
                    order.setChannelFee(new BigDecimal(String.valueOf(order.getConsumeFee())).multiply(RyxConstants.CHANNEL_BANK_COST_RATE).setScale(2,BigDecimal.ROUND_HALF_UP));
                }else{
                    if("1".equals(drType)){
                        order.setChannelRate(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE.multiply(new BigDecimal(10000)));
                        BigDecimal fee = new BigDecimal(String.valueOf(order.getConsumeFee())).multiply(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE).setScale(2,BigDecimal.ROUND_HALF_UP);
                        if(fee.compareTo(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE_MAX_FEE) > -1){
                            order.setChannelFee(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE_MAX_FEE);
                        }else{
                            order.setChannelFee(fee.setScale(2,BigDecimal.ROUND_HALF_UP));
                        }
                    }else{
                        order.setChannelRate(RyxConstants.CREDIT_CARD_BANK_RATE_LARGE.multiply(new BigDecimal(10000)));
                        BigDecimal fee = new BigDecimal(String.valueOf(order.getConsumeFee())).multiply(RyxConstants.CREDIT_CARD_BANK_RATE_LARGE).setScale(2,BigDecimal.ROUND_HALF_UP);
                        order.setChannelFee(fee);
                    }
                }
            }
        }else{
            obj.put("code", ResultCode.FAIL.code());
            obj.put("msg",respMsg);
        }
        return obj;
    }

    @Override
    public Map<String, Object> refundPay(MerchantOrder order, String mchNo) {
        log.info("==============>【瑞银信】交易订单退款接口，退款订单号：{}<==================",order.getOrderNumber());
        JSONObject reqData = new JSONObject();
        reqData.put("merchantCode",mchNo);
        reqData.put("oriReqDate",DateUtils.formatDateDefault(order.getCreateDt(),"yyyyMMdd"));
        reqData.put("oriReqMsgId",order.getOrderNumber());
        reqData.put("refundAmount",StringUtil.isNotEmpty(order.getRefundType()) && order.getRefundType() == 2 ? order.getRefundAmt() : order.getConsumeFee());
        reqData.put("refundReason",order.getRemark());
        JSONObject respMap = ryxService.analysisCommonPayParams(RyxConfig.refundUrl,RyxConfig.barCodePayRefund,reqData);
        log.info("==============>【瑞银信】交易订单退款接口，退款返回参数报文解析成功：{}<==================",respMap);

        String respCode = respMap.getString("respCode");

        String respMsg = respMap.getString("respMsg");

        Map<String,Object> resultMap = new HashMap<>(5);

        resultMap.put("code",ResultCode.SUCCESS.code());

        resultMap.put("msg",respMsg);

        if(!RyxConfig.PAY_SUCCESS_CODE.equals(respCode)){
            resultMap.put("code",ResultCode.FAIL.code());
            return resultMap;
        }
        String respType = respMap.getString("respType");
        resultMap.put("refundNo","RYXR_"+StringUtil.getBillno());
        if("S".equals(respType)){
            resultMap.put("status","1");
            return resultMap;
        }else if("E".equals(respType)){
            resultMap.put("code",ResultCode.FAIL.code());
            resultMap.put("status","3");
            return resultMap;
        }else if("R".equals(respType)){
            resultMap.put("status","2");
            return resultMap;
        }
        resultMap.put("status","3");
        resultMap.put("code",ResultCode.FAIL.code());
        resultMap.put("msg","退款失败，未知状态");
        return resultMap;
    }

    @Override
    public JSONObject tradeQuery(MerchantOrder order, String mchId) {
        log.info("==============>【瑞银信】交易订单查询，查询单号：{}<==================",order.getOrderNumber());
        JSONObject reqData = new JSONObject(6);
        reqData.put("merchantCode",mchId);
        reqData.put("oriReqDate",DateUtils.formatDateDefault(order.getCreateDt(),"yyyyMMdd"));
        reqData.put("oriReqMsgId",order.getOrderNumber());

        JSONObject respMap = ryxService.analysisCommonPayParams(RyxConfig.payUrl,RyxConfig.barCodePayQuery,reqData);

        log.info("==========>【瑞银信】交易查询响应参数，解析成功：参数为：{}<============", respMap);

        String respCode = respMap.getString("respCode");

        String respMsg = respMap.getString("respMsg");

        String smzfMsgId = respMap.getString("smzfMsgId");

        JSONObject result = new JSONObject(6);

        result.put("code",ResultCode.SUCCESS.code());

        if(!RyxConfig.PAY_SUCCESS_CODE.equals(respCode)){
            result.put("code",ResultCode.FAIL.code());
            result.put("msg",respMsg);
            return result;
        }

        result.put("transNo",smzfMsgId);

        JSONObject data = respMap.getJSONObject("data");

//        String oriRespCode = data.getString("oriRespCode");

        result.put("msg",data.getString("oriRespMsg"));

//        if(!RyxConfig.PAY_SUCCESS_CODE.equals(oriRespCode)){
//            result.put("code",ResultCode.FAIL.code());
//            result.put("payStatus","0");
//            return result;
//        }
        String payType = data.getString("payType");
        setChannelRateAndFee(payType,order);

        String oriRespType = data.getString("oriRespType");
        if("S".equals(oriRespType)){
            result.put("channelOrderNo",data.getString("channelTradeNo"));
            result.put("payTime",DateUtils.parse(data.getString("payTime"),"yyyyMMddHHmmss"));
            result.put("payStatus","1");
            result.put("transNo",smzfMsgId);
            result.put("buyerId",data.getString("buyerId"));
            return result;
        }else if("E".equals(oriRespType)){
            result.put("payStatus","0");
            return result;
        } else if("R".equals(oriRespType)){
            result.put("payStatus","7");
            return result;
        }
        result.put("code",ResultCode.FAIL.code());
        result.put("msg","未知状态");
        return result;
    }

    private void setChannelRateAndFee(String payType, MerchantOrder order) {
        if(order.getConsumeFee() > 1000.0d && Constant.PAY_TYPE_YSFPAY.equals(order.getPayTypeCode()) && StringUtil.isNotEmpty(payType)){
            //借记卡费率
            if("1".equals(payType)){
                order.setChannelRate(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE.multiply(new BigDecimal(10000)));
                BigDecimal fee = new BigDecimal(String.valueOf(order.getConsumeFee())).multiply(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE);
                if(fee.compareTo(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE_MAX_FEE) > -1){
                    order.setChannelFee(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE_MAX_FEE);
                }else{
                    order.setChannelFee(fee.setScale(2,BigDecimal.ROUND_HALF_UP));
                }
            }else{
                order.setChannelRate(RyxConstants.CREDIT_CARD_BANK_RATE_LARGE.multiply(new BigDecimal(10000)));
                BigDecimal fee = new BigDecimal(String.valueOf(order.getConsumeFee())).multiply(RyxConstants.DEBIT_CARD_BANK_RATE_LARGE);
                order.setChannelFee(fee.setScale(2,BigDecimal.ROUND_HALF_UP));
            }
        }
        order.setDrType(payType);
    }

    @Override
    public JSONObject refundQuery(MerchantRefundOrder refundOrder) {
//        QueryWrapper<MerchantOrder> queryWrapper = new QueryWrapper<MerchantOrder>()
//                .eq("orderNumber",refundOrder.getOrderNumber()).eq("status","1");
//        MerchantOrder order = merchantOrderService.getOne(queryWrapper);
//        MsgException.checkNull(order,"订单数据异常！");
//        JSONObject result = this.tradeQuery(order, refundOrder.getChannelMercNo());
        return null;
    }

    /**
     * 关闭订单接口
     * @param orderNumber
     * @param channelNo
     * @return
     */
    @Override
    public Map<String, Object> closeOrder(String orderNumber, String channelNo) {
        log.info("==============>【瑞银信】请求关闭订单接口，订单号：{}<==================",orderNumber);
        JSONObject reqData = new JSONObject(2);
        reqData.put("merchantCode",channelNo);
        reqData.put("oriReqMsgId",orderNumber);

        JSONObject respMap = ryxService.analysisCommonPayParams(RyxConfig.payUrl, RyxConfig.barCodeCloseOrder, reqData);

        log.info("==========>【瑞银信】请求关闭订单接口响应参数，解析成功，参数为：{}<============", respMap);

        String respCode = respMap.getString("respCode");

        String respMsg = respMap.getString("respMsg");

        Map<String,Object> resultMap = new HashMap<>(2);

        resultMap.put("code",ResultCode.SUCCESS.code());

        resultMap.put("msg",respMsg);

        if(!RyxConfig.PAY_SUCCESS_CODE.equals(respCode)){
            resultMap.put("code",ResultCode.FAIL.code());
            resultMap.put("msg",respMsg);
            return resultMap;
        }
        String respType = respMap.getString("respType");
        if("S".equals(respType)){
            return resultMap;
        }
        resultMap.put("code",ResultCode.FAIL.code());
        return resultMap;
    }

    /**
     * 获取微信openid接口
     * @param memberScanPayDTO
     * @param channel
     * @return
     */
    @Override
    public Map<String, Object> getWxOpenId(MemberScanPayDTO memberScanPayDTO, MerchantChannelStatus channel){
        log.info("==============>【瑞银信】微信授权码获取 openid接口，请求业务参数：{}<==================",JSONObject.toJSONString(memberScanPayDTO));
        JSONObject reqData = new JSONObject(3);
        reqData.put("merchantCode",channel.getMerchantId());
        reqData.put("authCode",memberScanPayDTO.getMemberCode());
        JSONObject respMap = ryxService.analysisCommonPayParams(RyxConfig.payUrl,RyxConfig.barCodeGetOpenid,reqData);

        log.info("==============>【瑞银信】微信授权码获取 openid接口，解析参数成功：{}<==================",JSONObject.toJSONString(respMap));

        Map<String,Object> reusltMap = new HashMap<>();

        String respCode = respMap.getString("respCode");

        String respMsg = respMap.getString("respMsg");

        reusltMap.put("code",ResultCode.FAIL.code());

        reusltMap.put("msg",respMsg);

        String respType = respMap.getString("respType");

        if(!RyxConfig.PAY_SUCCESS_CODE.equals(respCode) || !"S".equals(respType)){
            return respMap;
        }

        reusltMap.put("code",ResultCode.SUCCESS.code());

        JSONObject data = respMap.getJSONObject("data");

        reusltMap.put("openid",data.getString("openid"));

        reusltMap.put("subOpenid",data.getString("subOpenid"));

        return reusltMap;
    }

    /**
     * 获取银联标识接口
     * @param userAuthCode
     * @param paymentApp
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getUnionInfo(String userAuthCode, String paymentApp,String merchantId) throws Exception {
        JSONObject reqData = new JSONObject(3);
        reqData.put("merchantCode",merchantId);
        reqData.put("payWay","UPZF");
        reqData.put("userAuthCode",userAuthCode);
        reqData.put("appUpIdentifier",paymentApp);
        JSONObject respMap = ryxService.analysisCommonPayParams(RyxConfig.payUrl,RyxConfig.barCodeUnionPayUserId,reqData);
        log.info("==============>【瑞银信】获取银联用户标识 userId接口，解析参数成功：{}<==================",JSONObject.toJSONString(respMap));

        Map<String,Object> reusltMap = new HashMap<>(2);

        String respCode = respMap.getString("respCode");

        String respMsg = respMap.getString("respMsg");

        reusltMap.put("code",ResultCode.FAIL.code());

        reusltMap.put("msg",respMsg);

        String respType = respMap.getString("respType");

        if(!RyxConfig.PAY_SUCCESS_CODE.equals(respCode) || !"S".equals(respType)){
            return respMap;
        }
        reusltMap.put("code",ResultCode.SUCCESS.code());
        JSONObject data = respMap.getJSONObject("data");
        reusltMap.put("data",data.getString("userId"));
        reusltMap.put("code","1");
        return reusltMap;
    }

    /**
     * @description:支付异步回调
     * @date: 2021/8/23 9:58
     * @param params:
     * @return java.lang.String
     */
    @Override
    public String payNotifyCallBack(Map<String, String> params) {
        //解析返回参数
        JSONObject respMap = ryxService.analysisPayParamsEncrypt(JSON.toJSONString(params));
        log.info("=========================>【瑞银信】支付异步回调参数解析成功，解析参数为:{}<=====================",respMap);
        String respCode = respMap.getString("respCode");
        if(RyxConfig.PAY_SUCCESS_CODE.equals(respCode)){
            String respType = respMap.getString("respType");
            if("S".equals(respType)){
                JSONObject data = respMap.getJSONObject("data");
                String smzfMsgId = respMap.getString("smzfMsgId");
                String billType = data.getString("billType");
                //借贷标识
                String payType = data.getString("payType");
                //yyyyMMddHHmmss
                String payDateStr = data.getString("payTime");
                String buyerId = data.getString("buyerId");
                Date payDate = DateUtils.parse(payDateStr, "yyyyMMddHHmmss");
                QueryWrapper<MerchantOrder> orderQueryWrapper = new QueryWrapper<MerchantOrder>().eq("transNo",smzfMsgId)
                        .in("status","5","7");
                MerchantOrder order = merchantOrderService.getOne(orderQueryWrapper);
                if(order != null){
                    String channelOrderNo = data.getString("channelTradeNo");
                    if(StringUtil.isNotEmpty(channelOrderNo)){
                        if("aliPay".equals(order.getPayTypeCode())){
                            channelOrderNo = channelOrderNo.substring(2);
                        }
                    }
                    order.setDrType(payType);
                    String orderNumber = order.getOrderNumber();
                    //退款
                    if("01".equals(billType)){
                        log.info("====================>【瑞银信】退款成功回调，我方原订单号：{}<====================",orderNumber);
                        QueryWrapper<MerchantRefundOrder> queryWrapper = new QueryWrapper<MerchantRefundOrder>().eq("orderNumber",orderNumber).eq("channelNo",smzfMsgId);
                        queryWrapper.eq("status","2");
                        MerchantRefundOrder refundOrder = merchantRefundOrderService.getOne(queryWrapper);
                        if(refundOrder == null){
                            log.error("====================>【瑞银信】退款成功回调，我方原订单号：{}，随行付原订单号：{}，查询出的退款订单为空！<====================",orderNumber,smzfMsgId);
                        }else{
                            refundOrder.setRefundTime(payDate);
                            refundOrder.setStatus("1");
                            refundOrder.setFailMsg("退款成功");
                            executorService.execute(() -> merchantRefundOrderService.updateOrderAndSendNotify(refundOrder));
                        }
                        //收款
                    }else if("02".equals(billType)){
                        order.setTransDt(payDate);
                        order.setTransNo(smzfMsgId);
                        order.setStatus("1");
                        setChannelRateAndFee(payType,order);
                        order.setThirdTransNo(channelOrderNo);
                        if(StringUtil.isEmpty(order.getMercUserId())){
                            order.setMercUserId(buyerId);
                        }
                        executorService.execute(() -> merchantOrderService.updateOrderAndCreateProfit(order));
                    }else if("03".equals(billType)){
                        //TODO 瑞银信撤销交易回调 暂时没接
                        log.info("===========>【瑞银信】撤销交易回调没接<==============");
                    }else{
                        log.info("===========>【瑞银信】未知billType参数值，billType参数为：{}<==============",billType);
                    }
                }
            }
        }
        return RyxConfig.PAY_SUCCESS_CODE;
    }

    @Override
    public String tradePayWap(MerchantOrder order, MerchantChannelStatus channel) {
        return null;
    }

    @Override
    public JSONObject getUnionAccountUrl(JSONObject reqData) {
        return null;
    }

    @Override
    public JSONObject jftPay(JftReceiptOrder order, JftMercBaseInfo jftMerc, JftMercPaymentChannel jftChannel) {
        return null;
    }
}
