package com.jeequan.jeepay.core.entity;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.exception.BizException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * TODO
 *
 * @author crystal
 * @date 2023/12/11 16:23
 */
@Data
public class AccountOperate {
    /**
     * 最小充值金额
     */
    public static final Long MIN_RECHARGE_AMT = 10000L;

    /**
     * 最大充值充值
     */
    public static final Long MAX_RECHARGE_AMT = 5000000L;

    /**
     * 扫码入账比例 0.006 的手续费比例
     */
    public static final BigDecimal SCAN_REALITY_SCALE = BigDecimal.valueOf(0.994);

    /**
     * 账户号
     */
    private String accountNo;

    /**
     * 金额
     */
    private Long amount;

    /**
     * 实际金额
     */
    private Long finalAmt;

    /**
     * 变动方式  -1:提现驳回 0：扫码充值 1：对公转账 2：账户提现 3:一般户退款
     */
    private Byte changeMethod;

    /**
     * 变动类型 0：充值 1：提现 2：转账 3：退款
     */
    private Byte changeType;

    /**
     * 收款商户号
     */
    private String mchExtNo;

    /**
     *状态
     */
    private Byte state;

    /**
     * 打款类型
     */
    private Byte mark;

    /**
     * 订单号
     */
    private String orderId;

    /**
     * 是否修改账户 默认不修改
     */
    private Boolean isUpdateAccount = false;

    /**
     * 备注
     */
    private String remark;

    /**
     * 付款标识
     */
    private String ifCode;

    /**
     * 改变金额
     */
    private Long changeAmt;

    /**
     * 扩展参数
     */
    private JSONObject extra;

    public AccountOperate(String accountNo, Long amount,byte changeType,byte changeMethod,String orderId,String remark) {
        this.accountNo = accountNo;
        this.amount = amount;
        this.changeType = changeType;
        this.changeMethod = changeMethod;
        this.orderId = orderId;
        this.remark = remark;
        this.finalAmt = amount;
    }


//    public AccountOperate(String accountNo, Long amount,byte changeType,byte changeMethod) {
//        this.accountNo = accountNo;
//        this.amount = amount;
//        this.chaneType = changeType;
//        this.changeMethod = changeMethod;
//    }

    public AccountOperate(String orderId,Boolean isUpdateAccount,Byte state) {
        this.orderId = orderId;
        this.isUpdateAccount = isUpdateAccount;
        this.state = state;
    }

    public void preRechargeCheck() {
        if(this.getChangeMethod() == 0){
            if(this.getAmount() < MIN_RECHARGE_AMT || this.getAmount() > MAX_RECHARGE_AMT){
                throw new BizException("充值金额范围区间为："+MIN_RECHARGE_AMT / 100+"-" + MAX_RECHARGE_AMT / 100 +"元之间");
            }
        }
    }

    public void preCheck(){
        if(StringUtils.isEmpty(this.getOrderId())){
            throw new BizException("流水号不能为空");
        }
    }

    public void preAccountCheck() {
        preCheck();
        if(StringUtils.isEmpty(this.getAccountNo())){
            throw new BizException("账户编号不能为空");
        }
        if(this.getAmount() == null){
            throw new BizException("金额不能为空");
        }
    }
}
