-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathWxUserDatabase.cs
More file actions
354 lines (280 loc) · 11.6 KB
/
WxUserDatabase.cs
File metadata and controls
354 lines (280 loc) · 11.6 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using DigitalPlatform.IO;
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 WxUserDatabase
{
private static readonly WxUserDatabase _db = new WxUserDatabase();
public static WxUserDatabase Current
{
get
{
return _db;
}
}
MongoClient _mongoClient = null;
IMongoDatabase _database = null;
string _wxUserDbName = "";
IMongoCollection<WxUserItem> _wxUserCollection = null;
public IMongoCollection<WxUserItem> wxUserCollection
{
get
{
return this._wxUserCollection;
}
}
// 初始化
public void Open(
string strMongoDbConnStr,
string strInstancePrefix)
{
if (string.IsNullOrEmpty(strMongoDbConnStr) == true)
throw new ArgumentException("strMongoDbConnStr 参数值不应为空");
if (string.IsNullOrEmpty(strInstancePrefix) == false)
strInstancePrefix = strInstancePrefix + "_";
_wxUserDbName = strInstancePrefix + "user";
this._mongoClient = new MongoClient(strMongoDbConnStr);
this._database = this._mongoClient.GetDatabase(this._wxUserDbName);
//图书馆点对点账号
_wxUserCollection = this._database.GetCollection<WxUserItem>("item");
// todo 创建索引
bool bExist = false;
var indexes = _wxUserCollection.Indexes.ListAsync().Result.ToListAsync().Result;
foreach (BsonDocument doc in indexes)
{
}
// _logCollection.DropAllIndexes();
if (bExist == false)
{
CreateIndex();
}
}
// 创建索引
public void CreateIndex()
{
var options = new CreateIndexOptions() { Unique = false }; //不唯一,一个微信用户可能对应多个读者
_wxUserCollection.Indexes.CreateOne(
Builders<WxUserItem>.IndexKeys.Ascending("weixinId"),
options);
}
// 清除集合内的全部内容
public async Task Clear()
{
if (_wxUserCollection == null)
{
throw new Exception("访问日志 mongodb 集合尚未初始化");
}
// https://docs.mongodb.org/getting-started/csharp/remove/
var filter = new BsonDocument();
await _wxUserCollection.DeleteManyAsync(filter);
CreateIndex();
}
/*
/// <summary>
/// 根据微信号与图书馆代码查
/// </summary>
/// <param name="weixinId"></param>
/// <param name="libCode"></param>
/// <returns></returns>
public WxUserItem GetActive(string weixinId,string libCode)
{
var filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId)
& Builders<WxUserItem>.Filter.Eq("libCode", libCode)
& Builders<WxUserItem>.Filter.Eq("isActive", 1);
List<WxUserItem> list = this.wxUserCollection .Find(filter).ToList();
if (list.Count > 0)
return list[0];
return null;
}
*/
public WxUserItem GetActiveOrFirst(string weixinId, string libCode)
{
// 先查active的
var filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId)
& Builders<WxUserItem>.Filter.Eq("libCode", libCode)
& Builders<WxUserItem>.Filter.Eq("isActive", 1);
List<WxUserItem> list = this.wxUserCollection.Find(filter).ToList();
if (list.Count > 0)
return list[0];
// 没有查first
filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId)
& Builders<WxUserItem>.Filter.Eq("libCode", libCode);
list = this.wxUserCollection.Find(filter).ToList();
if (list.Count > 0)
return list[0];
return null;
}
public WxUserItem GetOneOrEmptyPatron(string weixinId, string libCode,string readerBarcode)
{
// 先查到weixinId+libCode+readerBarcode唯一的记录
var filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId)
& Builders<WxUserItem>.Filter.Eq("libCode", libCode)
& Builders<WxUserItem>.Filter.Eq("readerBarcode", readerBarcode);
List<WxUserItem> list = this.wxUserCollection.Find(filter).ToList();
if (list.Count >= 1)
return list[0];
// 未找到查weixinId+libCode,readerBarcoe为空的记录
filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId)
& Builders<WxUserItem>.Filter.Eq("libCode", libCode)
& Builders<WxUserItem>.Filter.Eq("readerBarcode", "");
list = this.wxUserCollection.Find(filter).ToList();
if (list.Count >= 1)
return list[0];
return null;
}
public WxUserItem GetActive(string weixinId)
{
var filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId)
& Builders<WxUserItem>.Filter.Eq("isActive", 1);
List<WxUserItem> list= this.wxUserCollection.Find(filter).ToList();//.ToListAsync().Result;
if (list.Count > 1)
throw new Exception("程序异常:微信号活动读者数量有" +list.Count+"个");
if (list.Count == 1)
return list[0];
return null;
}
public List<WxUserItem> GetByWeixinId(string weixinId)
{
var filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId);
return this.wxUserCollection.Find(filter).ToList();//.ToListAsync().Result;
}
public WxUserItem GetOneByWeixinId(string weixinId)
{
var filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId);
List<WxUserItem>list= this.wxUserCollection.Find(filter).ToList();//.ToListAsync().Result;
if (list.Count > 0)
return list[0];
return null;
}
/// <summary>
/// 查找所有用户
/// </summary>
/// <returns></returns>
public List<WxUserItem> GetUsers()
{
IFindFluent<WxUserItem, WxUserItem> f = this.wxUserCollection.Find(new BsonDocument());
if (f != null)
return f.ToList();
return null;
}
public WxUserItem Add(WxUserItem item)
{
//item.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
this.wxUserCollection.InsertOne(item);
return item;
}
// 更新
public long Update(WxUserItem item)
{
IMongoCollection<WxUserItem> collection = this.wxUserCollection;
var filter = Builders<WxUserItem>.Filter.Eq("id", item.id);
var update = Builders<WxUserItem>.Update
.Set("weixinId", item.weixinId)
.Set("readerBarcode", item.readerBarcode)
.Set("readerName", item.readerName)
.Set("libCode", item.libCode)
.Set("libUserName", item.libUserName)
.Set("libName", item.libName)
.Set("createTime", item.createTime)
.Set("updateTime", item.updateTime)
.Set("xml", item.xml)
.Set("refID", item.refID)
.Set("isActive", item.isActive);
UpdateResult ret = collection.UpdateOne(filter, update);
return ret.ModifiedCount;
}
public WxUserItem GetById(String id)
{
if (string.IsNullOrEmpty(id) == true || id == "null")
return null;
IMongoCollection<WxUserItem> collection = this.wxUserCollection;
var filter = Builders<WxUserItem>.Filter.Eq("id", id);
List<WxUserItem> list = this.wxUserCollection.Find(filter).ToList();
if (list.Count > 0)
{
return list[0];
}
return null;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="item"></param>
public void Delete(String id)
{
if (string.IsNullOrEmpty(id) == true || id=="null")
return;
IMongoCollection<WxUserItem> collection = this.wxUserCollection;
var filter = Builders<WxUserItem>.Filter.Eq("id", id);
// 检查一下是否被删除读者是否为默认读者,如果是,把自动将默认值设了第一个读者上。
List<WxUserItem> list = this.wxUserCollection.Find(filter).ToList();
string weixinId = "";
if (list.Count > 0)
{
weixinId = list[0].weixinId;
}
// 先删除
collection.DeleteOne(filter);
// 自动将第一个设为默认的
WxUserItem newUserItem = this.GetOneByWeixinId(weixinId);
if (newUserItem != null)
this.SetActive(newUserItem);
}
public void SetActive(WxUserItem item)
{
this.SetActive(item.weixinId, item.id);
}
public void SetActive(string weixinId,string id)
{
if (string.IsNullOrEmpty(weixinId) == true || weixinId=="null")
return;
if (string.IsNullOrEmpty(weixinId) == true || id=="null")
return;
IMongoCollection<WxUserItem> collection = this.wxUserCollection;
// 先将该微信用户的所有绑定读者都设为非活动
var filter = Builders<WxUserItem>.Filter.Eq("weixinId", weixinId);
var update = Builders<WxUserItem>.Update
.Set("isActive", 0)
.Set("updateTime", DateTimeUtil.DateTimeToString(DateTime.Now));
UpdateResult ret = collection.UpdateMany(filter, update);
// 再将参数传入的记录设为活动状态
filter = Builders<WxUserItem>.Filter.Eq("id",id);
update = Builders<WxUserItem>.Update
.Set("isActive", 1)
.Set("updateTime", DateTimeUtil.DateTimeToString(DateTime.Now));
ret = collection.UpdateMany(filter, update);
}
}
public class WxUserItem
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string id { get; private set; }
public string weixinId { get; set; } // 绑定必备
public string readerBarcode { get; set; }
public string readerName { get; set; }
public string libCode { get; set; } // 绑定必备
public string libUserName { get; set; }// 绑定必备
public string libName { get; set; }// 绑定必备
public string createTime { get; set; } // 创建时间
public string updateTime { get; set; } // 更校报时间
public string xml { get; set; }
public string refID { get; set; }
public int isActive = 0;
// 绑定必备
public string prefix { get; set; } //必须设为属性,才能在前端传值。
public string word { get; set; }
public string password { get; set; }
public string fullWord { get; set; } // 服务器用fullWord将strPrefix:strWord存在一起
}
}