package com.ruoyi.ss.pay.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.common.utils.wx.config.WxTransferConfig;
import com.ruoyi.ss.pay.domain.request.MyInitiateBatchTransferRequest;
import com.ruoyi.ss.transfer.domain.TransferVO;
import com.ruoyi.ss.transferDetail.domain.TransferDetailVO;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

    @Autowired
    private TransferBatchService transferBatchService;

    @Autowired
    private WxTransferConfig config;

    /**
     * 转账到零钱
     */
    public InitiateBatchTransferResponse batchTransfer(TransferVO transferVO) {
        List<TransferDetailInput> detailList = toTransferDetailInput(transferVO.getDetailList());
        MyInitiateBatchTransferRequest request = new MyInitiateBatchTransferRequest();
        request.setAppid(config.getAppId());
        request.setOutBatchNo(transferVO.getBatchNo());
        request.setBatchName(transferVO.getBatchName());
        request.setBatchRemark(transferVO.getBatchRemark());
        request.setTotalAmount(getTotalAmount(transferVO));
        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 List<TransferDetailInput> toTransferDetailInput(List<TransferDetailVO> detailList) {
        if (CollectionUtils.isEmptyElement(detailList)) {
            return Collections.emptyList();
        }

        List<TransferDetailInput> result = new ArrayList<>();
        BigDecimal decimal100 = new BigDecimal(100);
        for (TransferDetailVO detail : detailList) {
            TransferDetailInput inputDetail = new TransferDetailInput();
            inputDetail.setOutDetailNo(detail.getDetailNo());
            inputDetail.setTransferAmount(detail.getAmount().multiply(decimal100).longValue());
            inputDetail.setTransferRemark(detail.getRemark());
            inputDetail.setOpenid(detail.getAccountNo());
            inputDetail.setUserName(detail.getUserName());
            result.add(inputDetail);
        }
        return result;
    }

    private static long getTotalAmount(TransferVO transferVO) {
        return new BigDecimal(100).multiply(transferVO.getTotalAmount()).longValue();
    }

    /** 通过微信批次单号查询批次单 */
    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);
    }

}
