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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
| using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
namespace MongoTest
{
class Program
{
static void Main(string[] args)
{
var model = new TransModel();
model.RunAsync().Wait();
Console.WriteLine("Done");
}
}
public class AccountEntry
{
public string Uuid { get; set; }
public decimal Balance { get; set; }
public DateTime CreateTime { get; set; }
public DateTime UpdateTime { get; set; }
}
public class TransEntry
{
public string Tid { get; set; }
public string Uuid { get; set; }
public decimal Amount { get; set; }
public decimal NewBalance { get; set; }
public TransType Type { get; set; }
public DateTime CreateTime { get; set; }
public DateTime UpdateTime { get; set; }
}
public enum TransType
{
None = 0,
Recharge = 1,
Purchase = 2,
}
public class TransResult
{
public string Tid { get; set; }
public decimal NewBalance { get; set; }
public TransCode Code { get; set; }
}
public enum TransCode
{
Success = 0,
DuplicateTrans = 1,
Purchase = 2,
InsufficientBalance = 3
}
public class TransModel
{
private readonly IMongoClient _mongoClient;
private readonly IMongoCollection<AccountEntry> _accountCollection;
private readonly IMongoCollection<TransEntry> _transCollection;
private const string DatabaseName = "Transaction";
private const string AccountCollectionName = "Account";
private const string TransCollectionName = "Trans";
public const string ConnectionString = "";
public TransModel()
{
_mongoClient = new MongoClient(ConnectionString);
var database = _mongoClient.GetDatabase(DatabaseName);
var ignoreExtraElementsConvention = new ConventionPack { new IgnoreExtraElementsConvention(true) };
ConventionRegistry.Register("IgnoreExtraElements", ignoreExtraElementsConvention, type => true);
_accountCollection = database.GetCollection<AccountEntry>(AccountCollectionName);
_transCollection = database.GetCollection<TransEntry>(TransCollectionName);
}
public static string GenerateTid() => $"T_{Guid.NewGuid().ToString("N").ToUpper()}";
public static string GenerateAccountUuid() => $"Acc_{Guid.NewGuid().ToString("N").ToUpper()}";
public async Task<AccountEntry> CreateAccountAsync(string uuid, CancellationToken cancellationToken = default)
{
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
{
var newAccount = new AccountEntry
{
Uuid = uuid,
Balance = 0,
CreateTime = DateTime.UtcNow,
UpdateTime = DateTime.UtcNow
};
await _accountCollection.InsertOneAsync(newAccount, cancellationToken: cts.Token);
return newAccount;
}
}
public async Task<decimal> GetBalanceAsync(string uuid, CancellationToken ct, IClientSessionHandle s = null)
{
AccountEntry account;
if (s == null)
{
account = await _accountCollection.Find(x => x.Uuid == uuid).FirstOrDefaultAsync(ct);
}
else
{
account = await _accountCollection.Find(s, x => x.Uuid == uuid).FirstOrDefaultAsync(ct);
}
if (account == null)
{
throw new KeyNotFoundException($"Account '{uuid}' does not exist.");
}
return account.Balance;
}
private async Task<AccountEntry> AddBalanceAsync(string accountId, decimal amount, CancellationToken ct, IClientSessionHandle s)
{
if (amount <= 0)
{
throw new ArgumentException("Amount should > 0");
}
var filter = Builders<AccountEntry>.Filter.Eq(x => x.Uuid, accountId);
var update = Builders<AccountEntry>.Update
.Inc(x => x.Balance, amount)
.Set(x => x.UpdateTime, DateTime.UtcNow);
var options = new FindOneAndUpdateOptions<AccountEntry> { ReturnDocument = ReturnDocument.After, IsUpsert = true };
return await _accountCollection.FindOneAndUpdateAsync(s, filter, update, options, cancellationToken: ct);
}
private async Task<AccountEntry> DecreaseBalanceAsync(string accountId, decimal amount, CancellationToken ct, IClientSessionHandle s)
{
if (amount <= 0)
{
throw new ArgumentException("Amount should > 0");
}
var filter = Builders<AccountEntry>.Filter.And(
Builders<AccountEntry>.Filter.Eq(x => x.Uuid, accountId),
Builders<AccountEntry>.Filter.Gte(x => x.Balance, amount)
);
var update = Builders<AccountEntry>.Update
.Inc(x => x.Balance, -amount)
.Set(x => x.UpdateTime, DateTime.UtcNow);
var options = new FindOneAndUpdateOptions<AccountEntry> { ReturnDocument = ReturnDocument.After };
return await _accountCollection.FindOneAndUpdateAsync(s, filter, update, options, cancellationToken: ct);
}
public async Task<TransEntry> GetTransactionByTidAsync(string tid, CancellationToken ct, IClientSessionHandle s = null)
{
if (s == null)
{
return await _transCollection.Find(x => x.Tid == tid).FirstOrDefaultAsync(ct);
}
return await _transCollection.Find(s, x => x.Tid == tid).FirstOrDefaultAsync(ct);
}
public async Task<List<TransEntry>> GetTransactionsAsync(string accountId, CancellationToken ct, int n = 10)
{
var result = await _transCollection.Find(x => x.Uuid == accountId).SortBy(x => x.CreateTime).Limit(n).ToListAsync(ct);
return result;
}
public async Task<TransResult> RechargeAsync(string accountId, string tid, decimal amount)
{
using (var session = await _mongoClient.StartSessionAsync())
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
{
return await session.WithTransactionAsync(
async (s, ct) =>
{
var existingTransEntry = await GetTransactionByTidAsync(tid, ct, s);
if (existingTransEntry != null)
{
return new TransResult { Code = TransCode.DuplicateTrans };
}
var newBalanceEntry = await AddBalanceAsync(accountId, amount, ct, s);
var newTransEntry = new TransEntry
{
Tid = GenerateTid(),
Uuid = accountId,
Amount = amount,
NewBalance = newBalanceEntry.Balance,
Type = TransType.Recharge,
CreateTime = DateTime.UtcNow,
UpdateTime = DateTime.UtcNow,
};
await _transCollection.InsertOneAsync(s, newTransEntry, cancellationToken: ct);
return new TransResult
{
NewBalance = newTransEntry.NewBalance,
Tid = newTransEntry.Tid
};
}, cancellationToken: cts.Token);
}
}
public async Task<TransResult> PurchaseAsync(string accountId, string tid, decimal amount)
{
using (var session = await _mongoClient.StartSessionAsync())
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
{
return await session.WithTransactionAsync(
async (s, ct) =>
{
var existingTransEntry = await GetTransactionByTidAsync(tid, ct, s);
if (existingTransEntry != null)
{
return new TransResult { Code = TransCode.DuplicateTrans };
}
var newBalanceEntry = await DecreaseBalanceAsync(accountId, amount, ct, s);
if (newBalanceEntry == null)
{
return new TransResult { Code = TransCode.InsufficientBalance };
}
var newTransEntry = new TransEntry
{
Tid = GenerateTid(),
Uuid = accountId,
Amount = amount,
NewBalance = newBalanceEntry.Balance,
Type = TransType.Purchase,
CreateTime = DateTime.UtcNow,
UpdateTime = DateTime.UtcNow,
};
await _transCollection.InsertOneAsync(s, newTransEntry, cancellationToken: ct);
return new TransResult
{
NewBalance = newTransEntry.NewBalance,
Tid = newTransEntry.Tid
};
},
cancellationToken: cts.Token);
}
}
public async Task RunAsync()
{
var random = new Random(42);
// Create 10 accounts
var accountIds = new List<string>();
for (int i = 0; i < 10; i++)
{
var accountId = GenerateAccountUuid();
var account = await CreateAccountAsync(accountId);
accountIds.Add(account.Uuid);
await RechargeAsync(account.Uuid, GenerateTid(), 1000);
Console.WriteLine($"Created account {account.Uuid} with initial balance of 1000.");
}
Console.WriteLine("------------------------------------------------------------------------------");
foreach (var accountId in accountIds)
{
for (int i = 0; i < 5; i++)
{
var isRecharge = random.Next(0, 2) == 0;
var amount = (decimal)(random.NextDouble() * 300);
var tid = GenerateTid();
if (isRecharge)
{
var result = await RechargeAsync(accountId, tid, amount);
if (result.Code == TransCode.Success)
{
Console.WriteLine($"Recharge: Account {accountId}, Amount: {Math.Round(amount, 2)}, New Balance: {Math.Round(result.NewBalance, 2)}, Tid: {tid}");
}
else
{
Console.WriteLine($"Recharge failed: {result.Code}");
}
}
else
{
var result = await PurchaseAsync(accountId, tid, amount);
if (result.Code == TransCode.Success)
{
Console.WriteLine($"Purchase: Account {accountId}, Amount: {Math.Round(amount, 2)}, New Balance: {Math.Round(result.NewBalance, 2)}, Tid: {tid}");
}
else
{
Console.WriteLine($"Purchase failed: {result.Code}");
}
}
}
}
Console.WriteLine("------------------------------------------------------------------------------");
foreach (var accountId in accountIds)
{
var balance = await GetBalanceAsync(accountId, CancellationToken.None);
var transactions = await GetTransactionsAsync(accountId, CancellationToken.None);
Console.WriteLine($"Account {accountId} Balance: {Math.Round(balance, 2)}");
Console.WriteLine("Recent Transactions:");
foreach (var transaction in transactions)
{
Console.WriteLine($"Tid: {transaction.Tid}, Type: {transaction.Type}, Amount: {Math.Round(transaction.Amount, 2)}, New Balance: {Math.Round(transaction.NewBalance, 2)}, Time: {transaction.CreateTime}");
}
Console.WriteLine("****************************************************************************");
}
}
}
}
|