package com.nq.utils;
|
|
import com.nq.pojo.User;
|
|
import java.math.BigDecimal;
|
|
public final class UserFundUtil {
|
|
private UserFundUtil() {
|
}
|
|
/** T+0:充值/入款时同步增加总资金、可用、可取 */
|
public static void creditUserBalance(User user, BigDecimal amt) {
|
if (user == null || amt == null || amt.compareTo(BigDecimal.ZERO) <= 0) {
|
return;
|
}
|
BigDecimal userAmt = user.getUserAmt() == null ? BigDecimal.ZERO : user.getUserAmt();
|
BigDecimal enableAmt = user.getEnableAmt() == null ? BigDecimal.ZERO : user.getEnableAmt();
|
BigDecimal withdrawAmt = user.getEnaleWithdrawAmt() == null ? BigDecimal.ZERO : user.getEnaleWithdrawAmt();
|
user.setUserAmt(userAmt.add(amt));
|
user.setEnableAmt(enableAmt.add(amt));
|
user.setEnaleWithdrawAmt(withdrawAmt.add(amt));
|
}
|
|
/** T+0:扣款时同步减少可用与可取(可取不低于 0) */
|
public static void debitUserBalance(User user, BigDecimal amt) {
|
if (user == null || amt == null || amt.compareTo(BigDecimal.ZERO) <= 0) {
|
return;
|
}
|
BigDecimal enableAmt = user.getEnableAmt() == null ? BigDecimal.ZERO : user.getEnableAmt();
|
BigDecimal withdrawAmt = user.getEnaleWithdrawAmt() == null ? BigDecimal.ZERO : user.getEnaleWithdrawAmt();
|
user.setEnableAmt(enableAmt.subtract(amt));
|
BigDecimal newWithdraw = withdrawAmt.subtract(amt);
|
user.setEnaleWithdrawAmt(newWithdraw.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : newWithdraw);
|
}
|
|
/** T+0:仅同步可用与可取(不动总资金),用于账户间划转等 */
|
public static void creditEnableAndWithdraw(User user, BigDecimal amt) {
|
if (user == null || amt == null || amt.compareTo(BigDecimal.ZERO) <= 0) {
|
return;
|
}
|
BigDecimal enableAmt = user.getEnableAmt() == null ? BigDecimal.ZERO : user.getEnableAmt();
|
BigDecimal withdrawAmt = user.getEnaleWithdrawAmt() == null ? BigDecimal.ZERO : user.getEnaleWithdrawAmt();
|
user.setEnableAmt(enableAmt.add(amt));
|
user.setEnaleWithdrawAmt(withdrawAmt.add(amt));
|
}
|
|
/** T+0:仅扣减可用与可取(不动总资金) */
|
public static void debitEnableAndWithdraw(User user, BigDecimal amt) {
|
debitUserBalance(user, amt);
|
}
|
|
/** T+0:可用变动时同步调整可取(平仓/追加保证金等) */
|
public static void applyEnableDelta(User user, BigDecimal enableBefore, BigDecimal enableAfter) {
|
if (user == null || enableBefore == null || enableAfter == null) {
|
return;
|
}
|
user.setEnableAmt(enableAfter);
|
BigDecimal delta = enableAfter.subtract(enableBefore);
|
BigDecimal withdrawAmt = user.getEnaleWithdrawAmt() == null ? BigDecimal.ZERO : user.getEnaleWithdrawAmt();
|
BigDecimal newWithdraw = withdrawAmt.add(delta);
|
if (newWithdraw.compareTo(BigDecimal.ZERO) < 0) {
|
newWithdraw = BigDecimal.ZERO;
|
}
|
user.setEnaleWithdrawAmt(newWithdraw);
|
}
|
|
/** T+0:扣款时同步扣减可用与可取,可取不足时归零 */
|
public static void deductEnableAndWithdraw(User user, BigDecimal amt) {
|
if (user == null || amt == null || amt.compareTo(BigDecimal.ZERO) <= 0) {
|
return;
|
}
|
BigDecimal enableAmt = user.getEnableAmt() == null ? BigDecimal.ZERO : user.getEnableAmt();
|
user.setEnableAmt(enableAmt.subtract(amt));
|
BigDecimal withdrawAmt = user.getEnaleWithdrawAmt() == null ? BigDecimal.ZERO : user.getEnaleWithdrawAmt();
|
if (withdrawAmt.compareTo(amt) < 0) {
|
user.setEnaleWithdrawAmt(BigDecimal.ZERO);
|
} else {
|
user.setEnaleWithdrawAmt(withdrawAmt.subtract(amt));
|
}
|
}
|
|
/** T+0:退款时同步增加可用与可取 */
|
public static void refundEnableAndWithdraw(User user, BigDecimal amt) {
|
creditEnableAndWithdraw(user, amt);
|
}
|
|
/**
|
* T+0 可取资金解析:可用减去待审核提现冻结为下限,避免 T+1 遗留数据导致可取为 0。
|
*/
|
public static BigDecimal resolveWithdrawAmt(BigDecimal enableAmt, BigDecimal withdrawAmt, BigDecimal pendingWithdrawAmt) {
|
BigDecimal enable = enableAmt == null ? BigDecimal.ZERO : enableAmt;
|
BigDecimal withdraw = withdrawAmt == null ? BigDecimal.ZERO : withdrawAmt;
|
BigDecimal pending = pendingWithdrawAmt == null ? BigDecimal.ZERO : pendingWithdrawAmt;
|
BigDecimal floor = enable.subtract(pending);
|
if (floor.compareTo(BigDecimal.ZERO) < 0) {
|
floor = BigDecimal.ZERO;
|
}
|
if (withdraw.compareTo(floor) < 0) {
|
withdraw = floor;
|
}
|
if (withdraw.compareTo(enable) > 0) {
|
withdraw = enable;
|
}
|
return withdraw;
|
}
|
}
|