package com.yonge.toolset.payment.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yonge.toolset.base.result.BaseResult;
import com.yonge.toolset.payment.base.PaymentTemplate;
import com.yonge.toolset.payment.base.enums.MethodNameEnum;
import com.yonge.toolset.payment.base.enums.OpenEnum;
import com.yonge.toolset.payment.base.enums.PayChannelEnum;
import com.yonge.toolset.payment.base.enums.PaymentClientEnum;
import com.yonge.toolset.payment.base.model.ClosePayment;
import com.yonge.toolset.payment.base.model.OpenAuth;
import com.yonge.toolset.payment.base.model.Payment;
import com.yonge.toolset.payment.base.model.RefundBill;
import com.yonge.toolset.payment.base.model.callback.PaymentCallBack;
import com.yonge.toolset.payment.core.util.PaywayUtil;
import com.yonge.toolset.payment.core.service.PaymentClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: liweifan
 * @Data: 2022/5/11 15:40
 */
@Service
public class PaymentClientImpl implements PaymentClient {
    private final static Logger log = LoggerFactory.getLogger(PaymentClientImpl.class);

    @Override
    public BaseResult<Map<String, Object>> getOpenAuthMsg(OpenAuth openAuth) {
        return PaywayUtil.getRealTemplate(openAuth.getOpenType(), openAuth.getPayChannel()).getOpenAuthMsg(openAuth);
    }

    @Override
    public BaseResult<Payment> executePayment(Payment payment) {
        log.info("支付-付款请求参数:{}", JSONObject.toJSONString(payment));
        BaseResult<Payment> paymentBaseResult = PaywayUtil.getRealTemplate(payment.getOpenType(), payment.getPayChannel()).executePayment(payment);
        log.info("支付-付款返回参数:{}", JSONObject.toJSONString(paymentBaseResult));
        return paymentBaseResult;
    }

    @Override
    public BaseResult<Payment> queryPayment(Payment payment) {
        log.info("支付-查询请求参数:{}", JSONObject.toJSONString(payment));
        BaseResult<Payment> paymentBaseResult = PaywayUtil.getRealTemplate(payment.getOpenType(), payment.getPayChannel()).queryPayment(payment);
        log.info("支付-查询返回参数:{}", JSONObject.toJSONString(paymentBaseResult));
        return paymentBaseResult;
    }

    @Override
    public BaseResult<ClosePayment> closePayment(ClosePayment closePayment) {
        log.info("支付-关单请求参数:{}", JSONObject.toJSONString(closePayment));
        BaseResult<ClosePayment> closePaymentBaseResult = PaywayUtil.getRealTemplate(closePayment.getOpenType(), closePayment.getPayChannel()).closePayment(closePayment);
        log.info("支付-关单返回参数:{}", JSONObject.toJSONString(closePaymentBaseResult));
        return closePaymentBaseResult;
    }

    @Override
    public BaseResult<RefundBill> refundPayment(RefundBill refundBill) {
        log.info("支付-退款请求参数:{}", JSONObject.toJSONString(refundBill));
        BaseResult<RefundBill> refundBillBaseResult = PaywayUtil.getRealTemplate(refundBill.getOpenType(), refundBill.getPayChannel()).refundPayment(refundBill);
        log.info("支付-退款返回参数:{}", JSONObject.toJSONString(refundBillBaseResult));
        return refundBillBaseResult;
    }

    @Override
    public BaseResult<PaymentCallBack> analysisNotice(OpenEnum openType, PaymentClientEnum paymentClient, PayChannelEnum payChannel, MethodNameEnum methodName, HttpServletRequest request) {
        BaseResult<PaymentCallBack> paymentCallBackBaseResult = PaywayUtil.getRealTemplate(openType, payChannel).analysisNotice(openType, paymentClient, payChannel, methodName, request);
        log.info("支付-回调解析结果:{}", JSONObject.toJSONString(paymentCallBackBaseResult));
        return paymentCallBackBaseResult;
    }
}
