-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLibDatabase.cs
More file actions
289 lines (242 loc) · 8.21 KB
/
LibDatabase.cs
File metadata and controls
289 lines (242 loc) · 8.21 KB
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
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dp2Command.Service
{
/// <summary>
/// 用户数据库
/// </summary>
public sealed class LibDatabase
{
// 饿汉模式
private static readonly LibDatabase repo = new LibDatabase();
public static LibDatabase Current
{
get
{
return repo;
}
}
MongoClient _mongoClient = null;
IMongoDatabase _database = null;
string _libDbName = "";
IMongoCollection<LibItem> _libCollection = null;
public IMongoCollection<LibItem> LibCollection
{
get
{
return this._libCollection;
}
}
// 初始化
public void Open(
string strMongoDbConnStr,
string strInstancePrefix)
{
if (string.IsNullOrEmpty(strMongoDbConnStr) == true)
throw new ArgumentException("strMongoDbConnStr 参数值不应为空");
if (string.IsNullOrEmpty(strInstancePrefix) == false)
strInstancePrefix = strInstancePrefix + "_";
_libDbName = strInstancePrefix + "lib";
this._mongoClient = new MongoClient(strMongoDbConnStr);
this._database = this._mongoClient.GetDatabase(this._libDbName);
//图书馆点对点账号
_libCollection = this._database.GetCollection<LibItem>("item");
/*
// todo 创建索引
bool bExist = false;
var indexes = _libCollection.Indexes.ListAsync().Result.ToListAsync().Result;
foreach (BsonDocument doc in indexes)
{
}
// _logCollection.DropAllIndexes();
if (bExist == false)
{
//await CreateIndex();
}
*/
}
// 创建索引
public async Task CreateIndex()
{
/*
var options = new CreateIndexOptions() { Unique = true };
await _libCollection.Indexes.CreateOneAsync(
Builders<LibItem>.IndexKeys.Ascending("libCode"),
options);
*/
}
// 清除集合内的全部内容
public async Task Clear()
{
if (_libCollection == null)
{
throw new Exception("访问日志 mongodb 集合尚未初始化");
}
// https://docs.mongodb.org/getting-started/csharp/remove/
var filter = new BsonDocument();
await _libCollection.DeleteManyAsync(filter);
//await CreateIndex();
}
public LibItem GetLibById(string id)
{
var filter = Builders<LibItem>.Filter.Eq("id", id);
var list = this.LibCollection.Find(new BsonDocument("id", id)).ToListAsync().Result;
if (list.Count > 0)
return list[0];
return null;
}
public LibItem GetLibByLibCode(string libCode)
{
var filter = Builders<LibItem>.Filter.Eq("libCode", libCode);
List<LibItem> list = this.LibCollection.Find(filter).ToList();
if (list.Count > 0)
return list[0];
return null;
}
public List<LibItem> GetLibs()
{
return this.LibCollection.Find(new BsonDocument()).ToListAsync().Result;
}
/// <summary>
/// 根据libCode获得图书馆对象
/// </summary>
/// <param name="libCode">*获取全部</param>
/// <param name="start">0</param>
/// <param name="count">-1</param>
/// <returns></returns>
public async Task<List<LibItem>> GetLibs(string libCode,
int start,
int count)
{
IMongoCollection<LibItem> collection = this.LibCollection;
List<LibItem> results = new List<LibItem>();
var filter = Builders<LibItem>.Filter.Eq("libCode", libCode);
var index = 0;
using (var cursor = await collection.FindAsync(
libCode == "*" ? new BsonDocument() : filter
))
{
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach (var document in batch)
{
if (count != -1 && index - start >= count)
break;
if (index >= start)
results.Add(document);
index++;
}
}
}
return results;
}
public LibItem Add(LibItem item)
{
item.OperTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
this.LibCollection.InsertOne(item);
return item;
}
// 更新
public async Task<long> Update(LibItem item)
{
IMongoCollection<LibItem> collection = this.LibCollection;
var filter = Builders<LibItem>.Filter.Eq("id", item.id);
//var filter = Builders<LibItem>.Filter.Eq("libCode", item.libCode);
var update = Builders<LibItem>.Update
.Set("libCode", item.libCode)
.Set("libName", item.libName)
.Set("libP2PAccount", item.libUserName)
.Set("comment", item.comment)
.Set("OperTime", item.OperTime);
UpdateResult ret = await collection.UpdateOneAsync(filter, update);
return ret.ModifiedCount;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="item"></param>
public void Delete(String id)
{
IMongoCollection<LibItem> collection = this.LibCollection;
var filter = Builders<LibItem>.Filter.Eq("id", id);
//var filter = Builders<LibItem>.Filter.Eq("libCode", item.libCode);
collection.DeleteOne(filter);
}
}
public class LibItem
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string id { get; private set; }
public string libCode { get; set; }
public string libName { get; set; }
public string libUserName { get; set; }
public string comment { get; set; } // 注释
public string OperTime { get; set; } // 操作时间
}
/*
public class LibraryRespository
{
private static LibraryRespository repo = new LibraryRespository();
public static LibraryRespository Current
{
get
{
return repo;
}
}
private List<LibItem> data = new List<LibItem> {
new LibItem {
id="001", libCode = "lib1", libName = "图书馆1", libP2PAccount = "a1"},
new LibItem {
id="002",libCode = "lib2", libName = "图书馆2", libP2PAccount = "a2"},
new LibItem {
id="003",libCode = "lib3", libName = "图书馆3", libP2PAccount = "a3"},
};
public IEnumerable<LibItem> GetAll()
{
return data;
}
public LibItem Get(string libId)
{
return data.Where(r => r.id == libId).FirstOrDefault();
}
public LibItem Add(LibItem item)
{
// id取guid todo
item.id = Guid.NewGuid().ToString();
data.Add(item);
return item;
}
public void Remove(string libId)
{
LibItem item = Get(libId);
if (item != null)
{
data.Remove(item);
}
}
public bool Update(LibItem item)
{
LibItem storedItem = Get(item.libCode);
if (storedItem != null)
{
storedItem.libName = item.libName;
storedItem.libP2PAccount = item.libP2PAccount;
return true;
}
else
{
return false;
}
}
}
*/
}