package com.ywt.outpatient.service.rpc.common;

import com.ywt.gapi.paycenter.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class PayCenterProvider {
    Logger logger = LoggerFactory.getLogger(PayCenterProvider.class.getName());
    @DubboReference
    PayService payServiceBlockingStub;

    public WeChatCorpPayToWalletResponse withdrawToWeChat(String orderId, String openId, int amount, String ipAddr) {
        try {
            WeChatCorpPayToWalletRequest request = WeChatCorpPayToWalletRequest.newBuilder()
                    .setPayCode("1003")
                    .setPartnerTradeNo(orderId)
                    .setOpenid(openId)
                    .setAmount(amount)
                    .setDesc("提现到微信零钱")
                    .setSpbillCreateIp(ipAddr)
                    .build();
            return payServiceBlockingStub.weChatCorpPayToWallet(request);
        } catch (Exception e) {
            logger.error("withdrawToWeChat():", e);
            return null;
        }
    }


    public WeChatRefundResponse weChatRefund(String internalPayNo, String transactionId,
                                             String outTradeNo, int refundFee, String refundDesc) {
        try {
            WeChatRefundRequest request = WeChatRefundRequest.newBuilder()
                    .setInternalPayNo(internalPayNo)
                    .setTransactionId(transactionId)
                    .setOutTradeNo(outTradeNo)
                    .setRefundFee(refundFee)
                    .setRefundDesc(refundDesc)
                    .build();
            return payServiceBlockingStub.weChatRefund(request);
        } catch (Exception e) {
            logger.error("weChatRefund(): {}", e.getMessage(), e);
            return null;
        }
    }
}
