package com.neusoft.smart.pos.payment.repository;

import com.alibaba.fastjson.JSON;
import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.payment.request.FumaoPayRequest;
import com.neusoft.smart.pos.dto.payment.response.*;
import com.neusoft.smart.pos.payment.constants.FumaoPayConstants;
import com.neusoft.smart.pos.payment.persistent.domain.MchPayChannel;
import com.neusoft.smart.pos.payment.persistent.domain.PayOrder;
import com.neusoft.smart.pos.payment.persistent.domain.PayTypeDict;
import com.neusoft.smart.pos.payment.persistent.ext.domain.*;
import com.neusoft.smart.pos.payment.utils.HttpClientUtils;
import com.neusoft.smart.pos.payment.utils.LogUtils;
import com.neusoft.smart.pos.payment.utils.WXPayUtils;
import com.neusoft.smart.pos.payment.utils.WXPayUtils.SignType;

import lombok.val;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 富茂支付使用Repository
 * @author tjm
 * @date 2019-08-21
 */
@Component("fumaoPayRepository")
public class FumaoPayRepository implements IThirdPayRepository {
	private final static Logger logger = LoggerFactory.getLogger(FumaoPayRepository.class);
	
	////////////////////////////////////////////**会员接口**////

	/**
	 * 创建富茂会员信息接口
	 * @param fumaoPayRequest 富茂请求
	 * @return FumaoMemberInfo
	 */
	//@Override
	//public FumaoMemberInfo createPayOrderToThirdPlatform(){
	public FumaoMemberInfoRes fumaoMemberInfoToThirdPlatform(FumaoPayRequest fumaoPayRequest){
		logger.debug("fumaoMemberInfoToThirdPlatform enter");
		//1.封装请求数据数据
		String RequestData =  fumaoMemberRequestDataHandle(fumaoPayRequest);
		//2.请求富茂会员接口
		String responseData = HttpClientUtils.postData(FumaoPayConstants.DOMAIN_API + FumaoPayConstants.QUERY_AND_CALC_SUFFIX, RequestData);
        //3.记录日志
		//LogUtils.savePaymentTradeLog(payOrder.getMcode(),payOrder.getmOrderNumber(), DictionaryConstant.PAYMENT_PAY_TYPE_WXSM.getCode(),LogUtils.LOG_DESCRIPTION_DDSC,weixinRequestData,responseData,DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
		//4.封装结果并返回
		return fumaoMemberResponseDataHandle(responseData);
	}

	/**
	 * 富茂会员请求数据整理方法
	 * @param fumaoPayRequest 富茂请求
	 * @return String
	 */
	private String fumaoMemberRequestDataHandle(FumaoPayRequest fumaoPayRequest){
		Map<String, String> reqObj = new HashMap<>();
        reqObj.put("dev_id", FumaoPayConstants.BUSINESS_ID);
        reqObj.put("tenant_id", fumaoPayRequest.getTenantId());
        reqObj.put("nonce_str", WXPayUtils.generateNonceStr());//"123456789");
        reqObj.put("merchant_id", fumaoPayRequest.getMerchantId());
        reqObj.put("order_operation_id", fumaoPayRequest.getOrderOperatorId());
        if(fumaoPayRequest.getMemPhone() != null) {
            reqObj.put("mem_phone_number", fumaoPayRequest.getMemPhone());
        } else {
            reqObj.put("mem_id", fumaoPayRequest.getMemId());
        }
        reqObj.put("business_type", fumaoPayRequest.getBusinessType());
        reqObj.put("order_amount", fumaoPayRequest.getOrderAmount());
        reqObj.put("order_amount_left", fumaoPayRequest.getOrderAmountLeft());
        reqObj.put("order_create_sn", fumaoPayRequest.getDeviceSN());

		if(fumaoPayRequest.getCouponOrderIdList() != null) {
			reqObj.put("coupon_list", "[".concat(String.join(",", fumaoPayRequest.getCouponOrderIdList())).concat("]"));
		} else {
            reqObj.put("coupon_list", "[]");
        }

		reqObj.put("sign", WXPayUtils.generateSignature(reqObj, FumaoPayConstants.DEV_REQ_KEY, SignType.HMACSHA256));
		logger.info("sign:"+WXPayUtils.generateSignature(reqObj, FumaoPayConstants.DEV_REQ_KEY, SignType.HMACSHA256));
		
		JSONObject jsonObject = JSONObject.fromObject(reqObj);
		String result = jsonObject.toString();
        logger.info("fumaoMemberRequestDataHandle:"+result);
		return result;
	}

	/**
	 * 富茂会员接口响应数据处理
	 * @param responseData response数据
	 * @return FumaoMemberInfo
	 */
	private FumaoMemberInfoRes fumaoMemberResponseDataHandle(String responseData){
	    logger.info("fumaoMemberResponseDataHandle:"+responseData);
        FumaoMemberInfoRes memberInfo = new FumaoMemberInfoRes();

        Map resultMap = (Map)JSON.parse(responseData);
        String result;
        if(("0").equals(resultMap.get("result_code").toString())){
            result = (String)resultMap.get("result_data");
            JSONObject jsonObject  = JSONObject.fromObject(result);
            memberInfo = (FumaoMemberInfoRes) JSONObject.toBean(jsonObject, FumaoMemberInfoRes.class);
            memberInfo.setReturn_code(resultMap.get("return_code").toString());
            memberInfo.setReturn_msg(resultMap.get("return_msg").toString());
            memberInfo.setResult_code(resultMap.get("result_code").toString());
            String pomotionData = jsonObject.getString("promotion_data");
            JSONObject jsonObjectPromotion  = JSONObject.fromObject(pomotionData);
            FumaoPromotionData fumaoPromotionData = (FumaoPromotionData)JSONObject.toBean(jsonObjectPromotion, FumaoPromotionData.class);
            FumaoPromotionInfo promotionInfo = (FumaoPromotionInfo) JSONObject.toBean(JSONObject.fromObject(fumaoPromotionData.getPromotion_info()),FumaoPromotionInfo.class);
            FumaoCouponData coupon = (FumaoCouponData)JSONObject.toBean(JSONObject.fromObject(promotionInfo.getCoupon()),FumaoCouponData.class) ;
            JSONArray jsonArray = JSONArray.fromObject(coupon.getCoupon_list());
            List<FumaoCouponInfo> couponList =  JSONArray.toList(jsonArray, FumaoCouponInfo.class);
            fumaoPromotionData.getPromotion_info().getCoupon().setCoupon_list(couponList);
            memberInfo.setPromotionData(fumaoPromotionData);
            logger.info("memberInfo.getMemberPhoneNumber():"+memberInfo.getMem_phone_number());
        } else {
            memberInfo.setReturn_code(resultMap.get("result_code").toString());
            memberInfo.setReturn_msg(resultMap.get("result_msg").toString());
        }

		return memberInfo;
	}

	////***********************************************************************************************////
//	//////////////////////////////////////////////**优惠券支付接口**////
//	/**
//	 * 创建富茂优惠券核销检查信息接口
//	 * @param fumaoPayRequest
//	 * @return FumaoMemberInfo
//	 */
//	//@Override
//	public FumaoCheckCouponRes checkCouponOrderForVerify(FumaoPayRequest fumaoPayRequest){
//		//1.封装请求数据数据
//		String RequestData =  fumaoCouponOrderRequestDataHandle(fumaoPayRequest);
//		//2.请求富茂会员接口
//		String responseData = HttpClientUtils.postData(FumaoPayConstants.DOMAIN_API + FumaoPayConstants.COUPON_CHECK_URL_SUFFIX, RequestData);
//        //3.记录日志
//		//LogUtils.savePaymentTradeLog(payOrder.getMcode(),payOrder.getmOrderNumber(), DictionaryConstant.PAYMENT_PAY_TYPE_WXSM.getCode(),LogUtils.LOG_DESCRIPTION_DDSC,weixinRequestData,responseData,DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
//		//4.封装结果并返回
//		return fumaoCouponOrderResponseDataHandle(responseData);
//	}

	
//	/**
//	 * 富茂优惠券核销检查接口响应数据处理
//	 * @param responseData
//	 * @return FumaoMemberInfo
//	 */
//	private FumaoCheckCouponRes fumaoCouponOrderResponseDataHandle(String responseData){
//		//封装富茂会员对象
//		responseData = responseData.replaceAll("expire_time", "expired_time");
//		JSONObject jsonObject  = JSONObject.fromObject(responseData);
//		FumaoCheckCouponRes checkCouponRes = (FumaoCheckCouponRes) JSONObject.toBean(jsonObject, FumaoCheckCouponRes.class);
//		//封装可用优惠券对象
//		if (jsonObject.get("ret") != null) {
//			String str_ret = jsonObject.getString("ret");
//			JSONArray jsonArray = JSONArray.fromObject(str_ret);
//			List<FumaoCouponInfo> couponList =  JSONArray.toList(jsonArray, FumaoCouponInfo.class);
//			checkCouponRes.setRet_list(couponList);
//		}
//
//		return checkCouponRes;
//	}

	//////////////////////////////////////////**优惠券预核销接口**////
//	/**
//	* 创建富茂预核销订单接口
//	* @param fumaoPayRequest 富茂请求
//	* @return FumaoMemberInfo
//	*/
//	//@Override
//	public FumaoCouponVerifyRes couponOrderVerify(FumaoPayRequest fumaoPayRequest){
//		//1.封装请求数据数据
//		String RequestData =  fumaoCouponOrderRequestDataHandle(fumaoPayRequest);
//		//2.请求富茂会员接口
//		String responseData = HttpClientUtils.postData(FumaoPayConstants.DOMAIN_API + FumaoPayConstants.COUPON_VERIFY_URL_SUFFIX, RequestData);
//		//3.记录日志
//		//LogUtils.savePaymentTradeLog(payOrder.getMcode(),payOrder.getmOrderNumber(), DictionaryConstant.PAYMENT_PAY_TYPE_WXSM.getCode(),LogUtils.LOG_DESCRIPTION_DDSC,weixinRequestData,responseData,DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
//		//4.封装结果并返回
//		return fumaoCouponOrderVerifyResponseDataHandle(responseData);
//	}


	////////////////////////////富茂消费卡支付接口/////////////////////////////
//    /**
//     * 创建富茂预核销订单接口
//     * @param fumaoPayRequest 富茂请求
//     * @return FumaoMemberInfo
//     */
//    //@Override
//    public FumaoCardPayRes cardPayPre(FumaoPayRequest fumaoPayRequest){
//        //1.封装请求数据数据
//        String RequestData =  fumaoCardPayRequestDataHandle(fumaoPayRequest);
//        //2.请求富茂会员接口
//        String responseData = HttpClientUtils.postData(FumaoPayConstants.DOMAIN_API + FumaoPayConstants.COUPON_VERIFY_URL_SUFFIX, RequestData);
//        //3.记录日志
//        //LogUtils.savePaymentTradeLog(payOrder.getMcode(),payOrder.getmOrderNumber(), DictionaryConstant.PAYMENT_PAY_TYPE_WXSM.getCode(),LogUtils.LOG_DESCRIPTION_DDSC,weixinRequestData,responseData,DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
//        //4.封装结果并返回
//        return fumaoCardPayResponseDataHandle(responseData);
//    }

    ////*********************************************************************************************************/////

    @Override
    public ThirdPlatformCreateOrder createPayOrderToThirdPlatform(PayOrder payOrder, MchPayChannel mchPayChannel, PayTypeDict payTypeDict) {
        logger.info("createPayOrderToThirdPlatform enter");
        String payType = payTypeDict.getPayType();
        if(payType.equals(DictionaryConstant.PAYMENT_PAY_TYPE_DICOUNT_PAY.getCode())) {
            //1.封装请求数据数据
            String couponRequestData = fumaoCouponOrderRequestDataHandle(payOrder, mchPayChannel, payTypeDict);
            logger.info("createPayOrderToThirdPlatform::couponRequestData=" + couponRequestData);
            //2.请求富茂会员接口
            String responseData = HttpClientUtils.postData(FumaoPayConstants.DOMAIN_API + FumaoPayConstants.COUPON_VERIFY_URL_SUFFIX, couponRequestData);
            logger.info("createPayOrderToThirdPlatform::couponResponseData=" + responseData);
            //3.记录日志
            LogUtils.savePaymentTradeLog(payOrder.getMcode(), payOrder.getmOrderNumber(), DictionaryConstant.PAYMENT_PAY_TYPE_DICOUNT_PAY.getCode(), LogUtils.LOG_DESCRIPTION_DDSC, couponRequestData, responseData, DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
            //4.封装结果并返回
            return fumaoCouponOrderVerifyResponseDataHandle(payOrder, responseData);
        }
//        else if(payType.equals(DictionaryConstant.PAYMENT_PAY_TYPE_FM_CARD_PAY.getCode())) {
//            //1.封装请求数据数据
//            String cardRequestData = fumaoCardPayRequestDataHandle(payOrder, mchPayChannel, payTypeDict);
//            logger.info("createPayOrderToThirdPlatform::cardRequestData=" + cardRequestData);
//            //2.请求富茂消费卡接口
//            String responseData = HttpClientUtils.postData(FumaoPayConstants.DOMAIN_API + FumaoPayConstants.CARD_PAY_PRE_URL_SUFFIX, cardRequestData);
//            logger.info("createPayOrderToThirdPlatform::cardResponseData=" + responseData);
//            //3.记录日志
//            LogUtils.savePaymentTradeLog(payOrder.getMcode(), payOrder.getmOrderNumber(), DictionaryConstant.PAYMENT_PAY_TYPE_FM_CARD_PAY.getCode(), LogUtils.LOG_DESCRIPTION_DDSC, cardRequestData, responseData, DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
//            //4.封装结果并返回
//            return fumaoCardPayResponseDataHandle(payOrder, responseData);
//        }

        return null;
    }

    /**
     * 富茂优惠数据整理方法
     * @param payOrder 支付请求
     * @param mchPayChannel 支付渠道信息
     * @param payTypeDict 支付类型信息
     * @return String
     */
    private String fumaoCouponOrderRequestDataHandle(PayOrder payOrder, MchPayChannel mchPayChannel, PayTypeDict payTypeDict){
        Map<String, String> reqObj = new HashMap<>();
        reqObj.put("dev_id", FumaoPayConstants.BUSINESS_ID);
        reqObj.put("nonce_str", WXPayUtils.generateNonceStr());//"123456789");
        //TODO
//        String payInfoString = payOrder.getPayInfoJsonString();
//        logger.info("fumaoCouponOrderRequestDataHandle::payInfoString="+payInfoString);
//        JSONObject payInfoJsonObject  = JSONObject.fromObject(payInfoString);
//        try {
//            reqObj.put("merchant_id", payInfoJsonObject.getString("merchant_id"));
//            reqObj.put("tenant_id", payInfoJsonObject.getString("tenant_id"));
//            reqObj.put("order_amount", payInfoJsonObject.getString("order_amount"));
//            reqObj.put("order_operation_id", payInfoJsonObject.getString("order_operator_id"));
//            if(!payInfoJsonObject.getString("mem_phone_number").equals("")) {
//                reqObj.put("mem_phone_number", payInfoJsonObject.getString("mem_phone_number"));
//            }
//            if(!payInfoJsonObject.getString("mem_id").equals("")) {
//                reqObj.put("mem_id", payInfoJsonObject.getString("mem_id"));
//            }
//            reqObj.put("business_type", payInfoJsonObject.getString("business_type"));
//            List<String> couponList = (List<String>)payInfoJsonObject.get("coupon_list");
//            if(couponList != null) {
//                reqObj.put("coupon_list", "[".concat(String.join(",", couponList)).concat("]"));
//            } else {
//                reqObj.put("coupon_list", "[]");
//            }
//        } catch (Exception e) {
//            throw new BusinessCommonException(PaymentErrorCode.PAY_TYPE_INFO_NOT_EMPTY);
//        }
//        reqObj.put("order_amount_left", payOrder.getLeftAmount().toString());
        reqObj.put("order_number_of_platform", payOrder.getmOrderNumber());
        reqObj.put("order_create_sn", payOrder.getDeviceId());
        reqObj.put("sign", WXPayUtils.generateSignature(reqObj, FumaoPayConstants.DEV_REQ_KEY, SignType.HMACSHA256));
        JSONObject jsonObject = JSONObject.fromObject(reqObj);
        return jsonObject.toString();
    }

    /**
     * 富茂优惠券核销接口响应数据处理
     * @param responseData response数据
     * @return ThirdPlatformCreateOrder
     */
    private ThirdPlatformCreateOrder fumaoCouponOrderVerifyResponseDataHandle(PayOrder payOrder, String responseData){
        logger.info("fumaoCouponOrderVerifyResponseDataHandle enter: responseData="+responseData);

        val thirdPlatformOrder = new ThirdPlatformCreateOrder();
        thirdPlatformOrder.setMcode(payOrder.getMcode());
        thirdPlatformOrder.setTradeId(payOrder.getTradeId());
        thirdPlatformOrder.setMOrderNumber(payOrder.getmOrderNumber());
        thirdPlatformOrder.setOuterOrderNumber("");

        Map resultMap = (Map)JSON.parse(responseData);
        String result;
        if(("0").equals(resultMap.get("result_code").toString())){
            result = (String)resultMap.get("result_data");
            JSONObject jsonObject  = JSONObject.fromObject(result);
            try {
                String promotionData = jsonObject.getString("promotion_data");
//                thirdPlatformOrder.setOuterCodeUrl(promotionData); //TODO 存所有返回数据
//                thirdPlatformOrder.setOuterOrderNumber();//TODO 存优惠券券码? 同时多个券码怎么办？？
            } catch (Exception e) {
                thirdPlatformOrder.setOuterCodeUrl("");
            }
            thirdPlatformOrder.setPayStatus(DictionaryConstant.PAY_STATUS_YFK.getCode());
        }
        return thirdPlatformOrder;
    }

    /**
     * 富茂消费卡支付请求数据整理方法
     * @param payOrder 支付请求
     * @param mchPayChannel 支付渠道信息
     * @param payTypeDict 支付类型信息
     * @return String
     */
    private String fumaoCardPayRequestDataHandle(PayOrder payOrder, MchPayChannel mchPayChannel, PayTypeDict payTypeDict){
        Map<String, String> reqObj = new HashMap<>();
        reqObj.put("dev_id", FumaoPayConstants.BUSINESS_ID);
        reqObj.put("nonce_str", WXPayUtils.generateNonceStr());//"123456789");
        //TODO
//        String payInfoString = payOrder.getPayInfoJsonString();
//        logger.info("fumaoCardPayRequestDataHandle::payInfoString="+payInfoString);
//        JSONObject payInfoJsonObject  = JSONObject.fromObject(payInfoString);
//        try {
//            reqObj.put("merchant_id", payInfoJsonObject.getString("merchant_id"));
//            reqObj.put("tenant_id", payInfoJsonObject.getString("tenant_id"));
//            reqObj.put("order_amount", payInfoJsonObject.getString("order_amount"));
//            reqObj.put("order_operation_id", payInfoJsonObject.getString("order_operator_id"));
//            if(!payInfoJsonObject.getString("mem_phone_number").equals("")) {
//                reqObj.put("mem_phone_number", payInfoJsonObject.getString("mem_phone_number"));
//            }
//            if(!payInfoJsonObject.getString("mem_id").equals("")) {
//                reqObj.put("mem_id", payInfoJsonObject.getString("mem_id"));
//            }
//            reqObj.put("business_type", payInfoJsonObject.getString("business_type"));
//            reqObj.put("card_id",payInfoJsonObject.getString("card_id"));
//        } catch (Exception e) {
//            throw new BusinessCommonException(PaymentErrorCode.PAY_TYPE_INFO_NOT_EMPTY);
//        }
//        reqObj.put("order_amount_left", payOrder.getLeftAmount().toString());
        reqObj.put("order_number_of_platform", payOrder.getmOrderNumber());
        reqObj.put("order_create_sn", payOrder.getDeviceId());
        reqObj.put("sign", WXPayUtils.generateSignature(reqObj, FumaoPayConstants.DEV_REQ_KEY, SignType.HMACSHA256));
        JSONObject jsonObject = JSONObject.fromObject(reqObj);
        return jsonObject.toString();
    }

    /**
     * 富茂消费卡支付接口响应数据处理
     * @param responseData response数据
     * @return ThirdPlatformCreateOrder
     */
    private ThirdPlatformCreateOrder fumaoCardPayResponseDataHandle(PayOrder payOrder, String responseData){
        logger.info("fumaoCardPayResponseDataHandle enter: responseData="+responseData);
        val thirdPlatformOrder = new ThirdPlatformCreateOrder();
        thirdPlatformOrder.setMcode(payOrder.getMcode());
        thirdPlatformOrder.setTradeId(payOrder.getTradeId());
        thirdPlatformOrder.setMOrderNumber(payOrder.getmOrderNumber());
        thirdPlatformOrder.setOuterOrderNumber("");

        Map resultMap = (Map)JSON.parse(responseData);
        String result;
        if(("0").equals(resultMap.get("result_code").toString())){
            result = (String)resultMap.get("result_data");
            JSONObject jsonObject  = JSONObject.fromObject(result);
            try {
                String promotionData = jsonObject.getString("promotion_data");
//                thirdPlatformOrder.setOuterCodeUrl(promotionData); //TODO 存所有返回数据
//                thirdPlatformOrder.setOuterOrderNumber();//TODO 存卡号
            } catch (Exception e) {
                thirdPlatformOrder.setOuterCodeUrl("");
            }
            thirdPlatformOrder.setPayStatus(DictionaryConstant.PAY_STATUS_YFK.getCode());
        }
        return thirdPlatformOrder;
    }

    @Override
    public ThirdPlatformQueryOrder queryPayOrderFromThirdPlatform(PayOrder payOrder, MchPayChannel mchPayChannel) {
        return null;
    }

    @Override
    public ThirdPlatformCancelOrder cannelPayOrderFromThirdPlatform(PayOrder payOrder, MchPayChannel mchPayChannel) {
        return null;
    }

    @Override
    public ThirdPlatformRefundOrder refundPayOrderFromThirdPlatform(PayOrder payOrder, String refundNumber, Long refundAmount, MchPayChannel mchPayChannel, PayTypeDict payTypeDict) {
        logger.info("refundPayOrderFromThirdPlatform enter");
        String payType = payTypeDict.getPayType();
        if(payType.equals(DictionaryConstant.PAYMENT_PAY_TYPE_DICOUNT_PAY.getCode())) {
            //1.封装请求数据数据
            String couponRequestData = fumaoCouponOrderCancelRequestDataHandle(payOrder, mchPayChannel, payTypeDict);
            logger.info("refundPayOrderFromThirdPlatform::couponRequestData=" + couponRequestData);
            //2.请求富茂会员接口
            String responseData = HttpClientUtils.postData(FumaoPayConstants.DOMAIN_API + FumaoPayConstants.COUPON_VERIFY_CANCEL_URL_SUFFIX, couponRequestData);
            logger.info("refundPayOrderFromThirdPlatform::couponResponseData=" + responseData);
            //3.记录日志
            LogUtils.savePaymentTradeLog(payOrder.getMcode(), payOrder.getmOrderNumber(), DictionaryConstant.PAYMENT_PAY_TYPE_DICOUNT_PAY.getCode(), LogUtils.LOG_DESCRIPTION_SQTK, couponRequestData, responseData, DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
            //4.封装结果并返回
            return fumaoCouponOrderCancelResponseDataHandle(payOrder, responseData);
        }
//        else if(payType.equals(DictionaryConstant.PAYMENT_PAY_TYPE_FM_CARD_PAY.getCode())) {
//            //1.封装请求数据数据
//            String cardRequestData = fumaoCardPayCancelRequestDataHandle(payOrder, mchPayChannel, payTypeDict);
//            logger.info("refundPayOrderFromThirdPlatform::cardRequestData=" + cardRequestData);
//            //2.请求富茂消费卡接口
//            String responseData = HttpClientUtils.postData(FumaoPayConstants.DOMAIN_API + FumaoPayConstants.CARD_PAY_PRE_CANCEL_URL_SUFFIX, cardRequestData);
//            logger.info("refundPayOrderFromThirdPlatform::cardResponseData=" + responseData);
//            //3.记录日志
//            LogUtils.savePaymentTradeLog(payOrder.getMcode(), payOrder.getmOrderNumber(), DictionaryConstant.PAYMENT_PAY_TYPE_FM_CARD_PAY.getCode(), LogUtils.LOG_DESCRIPTION_SQTK, cardRequestData, responseData, DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());
//            //4.封装结果并返回
//            return fumaoCardPayCancelResponseDataHandle(payOrder, responseData);
//        }

        return null;
    }

    @Override
    public ThirdPlatformReverseOrder reversePayOrderFromThirdPlatform(PayOrder payOrder, String orderNumber, MchPayChannel mchPayChannel) {
        return null;
    }

    /**
     * 富茂券核销取消请求数据整理方法
     * @param payOrder 订单
     * @param mchPayChannel 支付渠道信息
     * @param payTypeDict 支付类型信息
     * @return String
     */
    private String fumaoCouponOrderCancelRequestDataHandle(PayOrder payOrder, MchPayChannel mchPayChannel, PayTypeDict payTypeDict){
        Map<String, String> reqObj = new HashMap<>();
        reqObj.put("dev_id", FumaoPayConstants.BUSINESS_ID);
        reqObj.put("nonce_str", WXPayUtils.generateNonceStr());//"123456789");
        //TODO
//        String payInfoString = payOrder.getPayInfoJsonString();
//        logger.info("fumaoCouponOrderCancelRequestDataHandle::payInfoString="+payInfoString);
//        JSONObject payInfoJsonObject  = JSONObject.fromObject(payInfoString);
//        try {
//            reqObj.put("merchant_id", payInfoJsonObject.getString("merchant_id"));
//            reqObj.put("tenant_id", payInfoJsonObject.getString("tenant_id"));
////            reqObj.put("order_amount", payInfoJsonObject.getString("order_amount"));
//            reqObj.put("order_operation_id", payInfoJsonObject.getString("order_operator_id"));
//            if(!payInfoJsonObject.getString("mem_phone_number").equals("")) {
//                reqObj.put("mem_phone_number", payInfoJsonObject.getString("mem_phone_number"));
//            }
//            if(!payInfoJsonObject.getString("mem_id").equals("")) {
//                reqObj.put("mem_id", payInfoJsonObject.getString("mem_id"));
//            }
//            reqObj.put("business_type", payInfoJsonObject.getString("business_type"));
//            JSONObject paymentInfo = new JSONObject();
//            paymentInfo.put("coupon_id",payInfoJsonObject.getString("coupon_id"));
//            reqObj.put("payment_info", paymentInfo.toString());
//        } catch (Exception e) {
//            throw new BusinessCommonException(PaymentErrorCode.PAY_TYPE_INFO_NOT_EMPTY);
//        }
//        reqObj.put("order_amount_left", payOrder.getLeftAmount().toString());
        reqObj.put("order_number_of_platform", payOrder.getmOrderNumber());
        reqObj.put("order_create_sn", payOrder.getDeviceId());
        reqObj.put("sign", WXPayUtils.generateSignature(reqObj, FumaoPayConstants.DEV_REQ_KEY, SignType.HMACSHA256));
        JSONObject jsonObject = JSONObject.fromObject(reqObj);
        return jsonObject.toString();
    }

    /**
     * 富茂券核销取消申请响应数据处理
     * @param payOrder
     * @param responseData
     * @return
     */
    private ThirdPlatformRefundOrder fumaoCouponOrderCancelResponseDataHandle(PayOrder payOrder, String responseData){
        logger.info("fumaoCouponOrderCancelResponseDataHandle enter: responseData="+responseData);
        val thirdPlatformOrder = new ThirdPlatformRefundOrder();
        thirdPlatformOrder.setMcode(payOrder.getMcode());
        thirdPlatformOrder.setMOrderNumber(payOrder.getmOrderNumber());
        thirdPlatformOrder.setPayType(payOrder.getPayType());

        Map resultMap = (Map)JSON.parse(responseData);
        String result;
        if(("0").equals(resultMap.get("result_code").toString())){
            result = (String)resultMap.get("result_data");
            JSONObject jsonObject  = JSONObject.fromObject(result);
            try {
                String promotionData = jsonObject.getString("promotion_data");
//                thirdPlatformOrder.setOuterCodeUrl(promotionData); //TODO
            } catch (Exception e) {
//                thirdPlatformOrder.setOuterCodeUrl("");
            }
        }

//        if(!resultMap.get("result_code").equalsIgnoreCase(ApplicationConstants.SUCCESS)){
//            if(resultMap.get("err_code").equalsIgnoreCase("ORDERNOTEXIST")){
//                thirdPlatformOrder.setRefundStatus("3");
//            }else{
//                thirdPlatformOrder.setRefundStatus("0");
//            }
//        }else{
//            thirdPlatformOrder.setRefundNumber(resultMap.get("out_refund_no"));
//            thirdPlatformOrder.setThirdRefundNumber(resultMap.get("refund_id"));
//            thirdPlatformOrder.setRefundAmount(Long.parseLong(resultMap.get("refund_fee")));
//            thirdPlatformOrder.setRefundStatus("1");
//        }
        return thirdPlatformOrder;
    }


    /**
     * 消费卡支付取消请求数据整理方法
     * @param payOrder 订单
     * @param mchPayChannel 支付渠道信息
     * @param payTypeDict 支付类型信息
     * @return String
     */
    private String fumaoCardPayCancelRequestDataHandle(PayOrder payOrder, MchPayChannel mchPayChannel, PayTypeDict payTypeDict){
        Map<String, String> reqObj = new HashMap<>();
        reqObj.put("dev_id", FumaoPayConstants.BUSINESS_ID);
        reqObj.put("nonce_str", WXPayUtils.generateNonceStr());//"123456789");
        //TODO
//        String payInfoString = payOrder.getPayInfoJsonString();
//        logger.info("fumaoCardPayCancelRequestDataHandle::payInfoString="+payInfoString);
//        JSONObject payInfoJsonObject  = JSONObject.fromObject(payInfoString);
//        try {
//            reqObj.put("merchant_id", payInfoJsonObject.getString("merchant_id"));
//            reqObj.put("tenant_id", payInfoJsonObject.getString("tenant_id"));
////            reqObj.put("order_amount", payInfoJsonObject.getString("order_amount"));
//            reqObj.put("order_operation_id", payInfoJsonObject.getString("order_operator_id"));
//            if(!payInfoJsonObject.getString("mem_phone_number").equals("")) {
//                reqObj.put("mem_phone_number", payInfoJsonObject.getString("mem_phone_number"));
//            }
//            if(!payInfoJsonObject.getString("mem_id").equals("")) {
//                reqObj.put("mem_id", payInfoJsonObject.getString("mem_id"));
//            }
//            reqObj.put("business_type", payInfoJsonObject.getString("business_type"));
//            JSONObject paymentInfo = new JSONObject();
//            paymentInfo.put("card_id",payInfoJsonObject.getString("card_id"));
//            paymentInfo.put("card_spend_id",payInfoJsonObject.getString("card_spend_id"));
//            reqObj.put("payment_info", paymentInfo.toString());
//        } catch (Exception e) {
//            throw new BusinessCommonException(PaymentErrorCode.PAY_TYPE_INFO_NOT_EMPTY);
//        }
//        reqObj.put("order_amount_left", payOrder.getLeftAmount().toString());
        reqObj.put("order_number_of_platform", payOrder.getmOrderNumber());
        reqObj.put("order_create_sn", payOrder.getDeviceId());
        reqObj.put("sign", WXPayUtils.generateSignature(reqObj, FumaoPayConstants.DEV_REQ_KEY, SignType.HMACSHA256));
        JSONObject jsonObject = JSONObject.fromObject(reqObj);
        return jsonObject.toString();
    }


    /**
     * 消费卡支付取消响应数据处理
     * @param payOrder
     * @param responseData
     * @return
     */
    private ThirdPlatformRefundOrder fumaoCardPayCancelResponseDataHandle(PayOrder payOrder, String responseData){
        logger.info("fumaoCouponOrderCancelResponseDataHandle enter: responseData="+responseData);
        val thirdPlatformOrder = new ThirdPlatformRefundOrder();
        thirdPlatformOrder.setMcode(payOrder.getMcode());
        thirdPlatformOrder.setMOrderNumber(payOrder.getmOrderNumber());
        thirdPlatformOrder.setPayType(payOrder.getPayType());

        Map resultMap = (Map)JSON.parse(responseData);
        String result;
        if(("0").equals(resultMap.get("result_code").toString())){
            result = (String)resultMap.get("result_data");
            JSONObject jsonObject  = JSONObject.fromObject(result);
            try {
                String promotionData = jsonObject.getString("promotion_data");
//                thirdPlatformOrder.setOuterCodeUrl(promotionData); //TODO
            } catch (Exception e) {
//                thirdPlatformOrder.setOuterCodeUrl("");
            }
        }

//        if(!resultMap.get("result_code").equalsIgnoreCase(ApplicationConstants.SUCCESS)){
//            if(resultMap.get("err_code").equalsIgnoreCase("ORDERNOTEXIST")){
//                thirdPlatformOrder.setRefundStatus("3");
//            }else{
//                thirdPlatformOrder.setRefundStatus("0");
//            }
//        }else{
//            thirdPlatformOrder.setRefundNumber(resultMap.get("out_refund_no"));
//            thirdPlatformOrder.setThirdRefundNumber(resultMap.get("refund_id"));
//            thirdPlatformOrder.setRefundAmount(Long.parseLong(resultMap.get("refund_fee")));
//            thirdPlatformOrder.setRefundStatus("1");
//        }
        return thirdPlatformOrder;
    }
}
