Line data Source code
1 : /**
2 : * @file account_manager.cpp
3 : * @brief 账户管理模块实现
4 : */
5 :
6 : #include "app/manager/account_manager.hpp"
7 : #include "storage/store.hpp"
8 : #include <chrono>
9 :
10 : namespace fix40 {
11 :
12 : // =============================================================================
13 : // 构造函数
14 : // =============================================================================
15 :
16 544 : AccountManager::AccountManager()
17 544 : : store_(nullptr)
18 544 : {}
19 :
20 8 : AccountManager::AccountManager(IStore* store)
21 8 : : store_(store)
22 : {
23 8 : if (store_) {
24 : // 启动时从存储恢复账户状态(用于服务端重启后资金连续性)。
25 : // 若存储不可用/为空,则保持内存态为空,由后续登录自动开户。
26 8 : auto accounts = store_->loadAllAccounts();
27 8 : std::lock_guard<std::mutex> lock(mutex_);
28 8 : accounts_.clear();
29 10 : for (const auto& account : accounts) {
30 2 : if (!account.accountId.empty()) {
31 2 : accounts_[account.accountId] = account;
32 : }
33 : }
34 8 : }
35 8 : }
36 :
37 : // =============================================================================
38 : // 账户管理
39 : // =============================================================================
40 :
41 542 : Account AccountManager::createAccount(const std::string& accountId, double initialBalance) {
42 542 : std::lock_guard<std::mutex> lock(mutex_);
43 :
44 : // 检查账户是否已存在
45 542 : auto it = accounts_.find(accountId);
46 542 : if (it != accounts_.end()) {
47 1 : return it->second;
48 : }
49 :
50 : // 创建新账户
51 541 : Account account(accountId, initialBalance);
52 541 : accounts_[accountId] = account;
53 :
54 : // 持久化
55 541 : persistAccount(account);
56 :
57 541 : return account;
58 542 : }
59 :
60 644 : std::optional<Account> AccountManager::getAccount(const std::string& accountId) const {
61 644 : std::lock_guard<std::mutex> lock(mutex_);
62 :
63 644 : auto it = accounts_.find(accountId);
64 644 : if (it != accounts_.end()) {
65 633 : return it->second;
66 : }
67 11 : return std::nullopt;
68 644 : }
69 :
70 5 : bool AccountManager::hasAccount(const std::string& accountId) const {
71 5 : std::lock_guard<std::mutex> lock(mutex_);
72 10 : return accounts_.find(accountId) != accounts_.end();
73 5 : }
74 :
75 1 : std::vector<std::string> AccountManager::getAllAccountIds() const {
76 1 : std::lock_guard<std::mutex> lock(mutex_);
77 :
78 1 : std::vector<std::string> ids;
79 1 : ids.reserve(accounts_.size());
80 1 : for (const auto& pair : accounts_) {
81 0 : ids.push_back(pair.first);
82 : }
83 2 : return ids;
84 1 : }
85 :
86 5 : size_t AccountManager::size() const {
87 5 : std::lock_guard<std::mutex> lock(mutex_);
88 10 : return accounts_.size();
89 5 : }
90 :
91 : // =============================================================================
92 : // 保证金操作
93 : // =============================================================================
94 :
95 616 : bool AccountManager::freezeMargin(const std::string& accountId, double amount) {
96 616 : std::lock_guard<std::mutex> lock(mutex_);
97 :
98 616 : auto it = accounts_.find(accountId);
99 616 : if (it == accounts_.end()) {
100 1 : return false;
101 : }
102 :
103 615 : Account& account = it->second;
104 :
105 : // 检查可用资金是否足够
106 615 : if (account.available < amount) {
107 101 : return false;
108 : }
109 :
110 : // 冻结保证金
111 514 : account.available -= amount;
112 514 : account.frozenMargin += amount;
113 514 : account.updateTime = std::chrono::system_clock::now();
114 :
115 : // 持久化
116 514 : persistAccount(account);
117 :
118 514 : return true;
119 616 : }
120 :
121 106 : bool AccountManager::unfreezeMargin(const std::string& accountId, double amount) {
122 106 : std::lock_guard<std::mutex> lock(mutex_);
123 :
124 106 : auto it = accounts_.find(accountId);
125 106 : if (it == accounts_.end()) {
126 1 : return false;
127 : }
128 :
129 105 : Account& account = it->second;
130 :
131 : // 释放冻结保证金
132 105 : account.frozenMargin -= amount;
133 105 : account.available += amount;
134 105 : account.updateTime = std::chrono::system_clock::now();
135 :
136 : // 持久化
137 105 : persistAccount(account);
138 :
139 105 : return true;
140 106 : }
141 :
142 110 : bool AccountManager::confirmMargin(const std::string& accountId,
143 : double frozenAmount, double usedAmount) {
144 110 : std::lock_guard<std::mutex> lock(mutex_);
145 :
146 110 : auto it = accounts_.find(accountId);
147 110 : if (it == accounts_.end()) {
148 1 : return false;
149 : }
150 :
151 109 : Account& account = it->second;
152 :
153 : // 冻结转占用
154 109 : account.frozenMargin -= frozenAmount;
155 109 : account.usedMargin += usedAmount;
156 :
157 : // 多冻结的部分返还到可用资金
158 109 : double diff = frozenAmount - usedAmount;
159 109 : if (diff > 0) {
160 3 : account.available += diff;
161 : }
162 :
163 109 : account.updateTime = std::chrono::system_clock::now();
164 :
165 : // 持久化
166 109 : persistAccount(account);
167 :
168 109 : return true;
169 110 : }
170 :
171 104 : bool AccountManager::releaseMargin(const std::string& accountId, double amount) {
172 104 : std::lock_guard<std::mutex> lock(mutex_);
173 :
174 104 : auto it = accounts_.find(accountId);
175 104 : if (it == accounts_.end()) {
176 1 : return false;
177 : }
178 :
179 103 : Account& account = it->second;
180 :
181 : // 释放占用保证金
182 103 : account.usedMargin -= amount;
183 103 : account.available += amount;
184 103 : account.updateTime = std::chrono::system_clock::now();
185 :
186 : // 持久化
187 103 : persistAccount(account);
188 :
189 103 : return true;
190 104 : }
191 :
192 : // =============================================================================
193 : // 盈亏操作
194 : // =============================================================================
195 :
196 9 : bool AccountManager::updatePositionProfit(const std::string& accountId, double profit) {
197 9 : std::lock_guard<std::mutex> lock(mutex_);
198 :
199 9 : auto it = accounts_.find(accountId);
200 9 : if (it == accounts_.end()) {
201 1 : return false;
202 : }
203 :
204 8 : Account& account = it->second;
205 :
206 : // 计算盈亏变化量
207 8 : double profitDelta = profit - account.positionProfit;
208 :
209 : // 更新持仓盈亏和可用资金
210 8 : account.positionProfit = profit;
211 8 : account.available += profitDelta;
212 8 : account.updateTime = std::chrono::system_clock::now();
213 :
214 : // 持久化
215 8 : persistAccount(account);
216 :
217 8 : return true;
218 9 : }
219 :
220 107 : bool AccountManager::addCloseProfit(const std::string& accountId, double profit) {
221 107 : std::lock_guard<std::mutex> lock(mutex_);
222 :
223 107 : auto it = accounts_.find(accountId);
224 107 : if (it == accounts_.end()) {
225 1 : return false;
226 : }
227 :
228 106 : Account& account = it->second;
229 :
230 : // 记录平仓盈亏
231 106 : account.balance += profit;
232 106 : account.closeProfit += profit;
233 106 : account.available += profit;
234 106 : account.updateTime = std::chrono::system_clock::now();
235 :
236 : // 持久化
237 106 : persistAccount(account);
238 :
239 106 : return true;
240 107 : }
241 :
242 : // =============================================================================
243 : // 清理方法
244 : // =============================================================================
245 :
246 1 : void AccountManager::clear() {
247 1 : std::lock_guard<std::mutex> lock(mutex_);
248 1 : accounts_.clear();
249 1 : }
250 :
251 : // =============================================================================
252 : // 私有方法
253 : // =============================================================================
254 :
255 1486 : void AccountManager::persistAccount(const Account& account) {
256 1486 : if (!store_) return;
257 : // 注意:此处为 best-effort 持久化。存储失败时不抛异常,以免影响撮合主流程。
258 13 : store_->saveAccount(account);
259 : }
260 :
261 : } // namespace fix40
|