package com.jeequan.jeepay.core.interfaces.paychannel;

import com.jeequan.jeepay.core.beans.RequestKitBean;
import com.jeequan.jeepay.core.entity.TransferOrder;
import com.jeequan.jeepay.core.entity.TransferSubject;
import com.jeequan.jeepay.core.entity.TransferWallet;
import com.jeequan.jeepay.core.model.applyment.PaywayFee;
import com.jeequan.jeepay.core.model.openapi.biz.transfer.TransferOrderParam;
import com.jeequan.jeepay.core.model.params.IsvParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.utils.SeqKit;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.MutablePair;

import java.util.Date;

/**
* 转账接口
*
* @author terrfly
*
* @date 2021/8/11 13:59
*/
public interface ITransferService {

    /* 获取到接口code **/
    String getIfCode();

    /** 是否支持该支付入账方式 */
    boolean isSupport(String entryType);

    PaywayFee paywayFee(TransferOrderParam bizRQ, TransferOrder transferOrder);

    default TransferOrder genTransferOrder(TransferOrderParam rq, TransferSubject transferSubject,
                                           TransferWallet wallet, String ifCode){

        TransferOrder transferOrder = new TransferOrder();
        transferOrder.setTransferId(SeqKit.genTransOrderId()); //生成转账订单号
        transferOrder.setTransferSubjectIdFq(rq.getTransferFqId()); //生成转账订单号
        transferOrder.setMchNo(transferSubject.getMchNo()); //商户号
        transferOrder.setIsvNo(transferSubject.getIsvNo()); //服务商号
        transferOrder.setWalletId(wallet.getId());
        transferOrder.setAgentNo(transferSubject.getAgentNo()); // 服务商号
        transferOrder.setAppId(transferSubject.getAppId()); //商户应用appId
        transferOrder.setMchOrderNo(rq.getMchOrderNo()); //商户订单号
        transferOrder.setIfCode(ifCode); //接口代码
        transferOrder.setEntryType(rq.getEntryType()); //入账方式
        transferOrder.setAmount(rq.getAmount()); //订单金额
        transferOrder.setCurrency(rq.getCurrency()); //币种

        RequestKitBean requestKitBean = SpringBeansUtil.getBean(RequestKitBean.class);
        transferOrder.setClientIp(StringUtils.defaultIfEmpty(rq.getClientIp(), requestKitBean.getClientIp())); //客户端IP
        transferOrder.setState(TransferOrder.STATE_INIT); //订单状态, 默认订单生成状态
        transferOrder.setAccountNo(rq.getAccountNo()); //收款账号
        transferOrder.setOriginAccountName(transferSubject.getAccountName());
        transferOrder.setOriginAccountNo(transferSubject.getAccount());
        transferOrder.setAccountName(rq.getAccountName()); //账户姓名
        transferOrder.setAccountType(rq.getAccountType());
        transferOrder.setBankName(rq.getBankName()); //银行名称
        transferOrder.setTransferDesc(rq.getTransferDesc()); //转账备注
        transferOrder.setExtParam(rq.getExtParam()); //商户扩展参数
        transferOrder.setNotifyUrl(rq.getNotifyUrl()); //异步通知地址
        transferOrder.setTransferType(TransferOrder.TRANSFER_TYPE_DF);

        PaywayFee paywayFee = paywayFee(rq, transferOrder);

        // 转账手续费，  可以为空。
        if(paywayFee != null){
            MutablePair<String, Long> feeAndSnapshot = paywayFee.calFeeAndSnapshot(transferOrder.getAmount());
            transferOrder.setMchFeeRate(feeAndSnapshot.left);
            transferOrder.setMchOrderFeeAmount(feeAndSnapshot.right); //商户手续费,单位分
        }

        transferOrder.setCreatedAt(new Date()); //订单创建时间
        return transferOrder;

    }

    /** 通用校验，一般来说全部通道都需要进行该校验 */
    void commonCheck(TransferOrderParam bizRQ, TransferOrder transferOrder);

    /** 前置检查如参数等信息是否符合要求， 返回错误信息或直接抛出异常即可  */
    String preCheck(TransferOrderParam bizRQ, TransferOrder transferOrder);


    /** 调起转账接口，并响应数据；  内部处理普通商户和服务商模式  **/
    ChannelRetMsg transfer(TransferOrderParam bizRQ, TransferOrder transferOrder) throws Exception;

    /** 调起转账查询接口  **/
    ChannelRetMsg query(TransferOrder transferOrder, IsvParams isvParams);

    /** 查询分账账户余额  **/
    MutablePair<String, Long> queryBalanceAmount(TransferWallet transferWallet);

}
