1
zj
3 days ago cd969de1f50903ca87deda67bff2d6fcc35b4107
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;
    }
}