package com.ruoyi.common.pay.wx.service;

import com.alipay.api.domain.MultiStepTransferParticipant;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.pay.TransferApi;
import com.ruoyi.common.pay.wx.config.WxTransferConfig;
import com.ruoyi.common.pay.BatchTransferAble;
import com.ruoyi.common.pay.wx.domain.request.MyInitiateBatchTransferRequest;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import com.wechat.pay.java.service.transferbatch.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wjh
 * 2024/8/12
 */
@Service
public class WxTransferService {

    @Autowired
    private WxTransferConfig config;

    @Autowired
    private TransferBatchService transferBatchService;

    /**
     * 转账到零钱
     */
    public InitiateBatchTransferResponse batchTransfer(BatchTransferAble batchTransferAble) {
        List<TransferDetailInput> detailList = batchTransferAble.wxTransferDetailList();
        MyInitiateBatchTransferRequest request = new MyInitiateBatchTransferRequest();
        request.setAppid(config.getAppId());
        request.setOutBatchNo(batchTransferAble.transferOutBatchNo());
        request.setBatchName(batchTransferAble.transferBatchName());
        request.setBatchRemark(batchTransferAble.transferBatchRemark());
        request.setTotalAmount(batchTransferAble.transferTotalFen());
        request.setTotalNum(detailList.size());
        request.setTransferDetailList(detailList);
        request.setNotifyUrl(config.getTransferNotifyUrl());
        try {
            return transferBatchService.initiateBatchTransfer(request);
        } catch (com.wechat.pay.java.core.exception.ServiceException e) {
            throw new ServiceException(String.format("微信发起转账到零钱出错：%s", e.getErrorMessage()));
        }
    }

    /** 通过微信批次单号查询批次单 */
    public TransferBatchEntity getTransferBatchByNo(GetTransferBatchByNoRequest request) {
        return transferBatchService.getTransferBatchByNo(request);
    }

    /** 通过商家批次单号查询批次单 */
    public TransferBatchEntity getTransferBatchByOutNo(GetTransferBatchByOutNoRequest request) {
        return transferBatchService.getTransferBatchByOutNo(request);
    }

    /** 通过微信明细单号查询明细单 */
    public TransferDetailEntity getTransferDetailByNo(GetTransferDetailByNoRequest request ) {
        return transferBatchService.getTransferDetailByNo(request);
    }

    /** 通过商家明细单号查询明细单 */
    public TransferDetailEntity getTransferDetailByOutNo(GetTransferDetailByOutNoRequest request ) {
        return transferBatchService.getTransferDetailByOutNo(request);
    }

}
