package com.ruoyi.ss.transfer.domain;

import com.alipay.api.domain.MultiStepTransOrderDetailRequest;
import com.alipay.api.domain.MultiStepTransferParticipant;
import com.ruoyi.common.pay.BatchTransferAble;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.transferDetail.domain.TransferDetailVO;
import com.wechat.pay.java.service.transferbatch.model.TransferDetailInput;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author wjh
 * 2024/8/9
 */
@Data
public class TransferVO extends Transfer implements BatchTransferAble {

    @ApiModelProperty("转账明细列表")
    private List<TransferDetailVO> detailList;

    /**
     * 转账总金额
     */
    @Override
    public Long transferTotalFen() {
        return new BigDecimal(100).multiply(this.getTotalAmount()).longValue();
    }

    /**
     * 转账外部订单号
     */
    @Override
    public String transferOutBatchNo() {
        return getBatchNo();
    }

    /**
     * 转账备注
     */
    @Override
    public String transferBatchRemark() {
        return getBatchRemark();
    }

    /**
     * 转账名称
     */
    @Override
    public String transferBatchName() {
        return getBatchName();
    }

    /**
     * 转账明细
     */
    @Override
    public List<TransferDetailInput> wxTransferDetailList() {
        return this.toTransferDetailInput(this.getDetailList());
    }

    /**
     * 支付宝转账明细
     */
    @Override
    public List<MultiStepTransOrderDetailRequest> aliTransferDetailList() {
        if (CollectionUtils.isEmptyElement(detailList)) {
            return Collections.emptyList();
        }

        List<MultiStepTransOrderDetailRequest> orderDetails = new ArrayList<MultiStepTransOrderDetailRequest>();

        int seq = 1;
        for (TransferDetailVO detail : detailList) {
            MultiStepTransOrderDetailRequest orderDetail = new MultiStepTransOrderDetailRequest();
            orderDetail.setOrderSeq(String.valueOf(seq));
            orderDetail.setOrderTitle("转账到支付宝");
//            orderDetail.setBusinessParams("withdraw_timeliness=T0");
            orderDetail.setRemark(detail.getRemark());
            orderDetail.setOutBizNo(detail.getDetailNo());
            orderDetail.setTransAmount(detail.getAmount().toString());
            MultiStepTransferParticipant payeeInfo = new MultiStepTransferParticipant();
            payeeInfo.setIdentity(detail.getAccountNo());
            payeeInfo.setName(detail.getUserName());
            payeeInfo.setIdentityType("ALIPAY_OPEN_ID");
            orderDetail.setPayeeInfo(payeeInfo);
            orderDetails.add(orderDetail);
            seq ++;
        }


        return orderDetails;
    }

    /**
     * 转为明细
     */
    private 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;
    }
}
