-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataManager.cpp
341 lines (296 loc) · 8.35 KB
/
DataManager.cpp
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
#pragma once
#include "DataManager.h"
#include "json/json.h"
using namespace std;
DataManager::DataManager() {}
DataManager::DataManager(string usrDataName, string pkgDataName) {
setUsrFile(usrDataName);
setPkgFile(pkgDataName);
setAdminFile("./data/admin.dat");
}
DataManager::~DataManager() {
UsrFile.close();
PkgFile.close();
AdminFile.close();
}
int DataManager::setUsrFile(string name) {
UsrFile.open(name, ios::in | ios::out | ios::binary);
if (UsrFile) {
User tmp;
int n = 0;
UsrFile.seekg(0, ios::beg);
while (!UsrFile.eof() && UsrFile.peek() != EOF) {
UsrFile.read((char*)&tmp, sizeof(User));
users.push_back(tmp);
int n = users.size() - 1;//vector下标从0开始
UsrMap[tmp.userName] = n;
UsrMap[tmp.UUID] = n;
}
}
else {
cout << "usrFileNotFound,CreatNewOne.\n" << endl;
UsrFile.open(name, ios::out | ios::binary);
UsrFile.close(); UsrFile.clear();
UsrFile.open(name, ios::in | ios::out | ios::binary);
}
UsrFile.seekg(0);
if (!UsrFile) { return -1; }
return 0;
}
int DataManager::setPkgFile(string name) {
PkgFile.open(name, ios::in | ios::out | ios::binary);
if (PkgFile) {
Package tmp;
PkgFile.seekg(0);
while (!PkgFile.eof() && PkgFile.peek() != EOF) {
PkgFile.read((char*)&tmp, sizeof(Package));
packages.push_back(tmp);
int n = packages.size() - 1;//vector下标从0开始
PkgMap[tmp.UUID] = n;
PkgFromMap[tmp.from].push_back(n);
PkgToMap[tmp.to].push_back(n);
}
}
else {
cout << "pkgFileNotFound,CreatNewOne.\n" << endl;
PkgFile.open(name, ios::out | ios::binary);
PkgFile.close(); PkgFile.clear();
PkgFile.open(name, ios::in | ios::out | ios::binary);
}
PkgFile.seekg(0);
if (!PkgFile) { return -1; }
return 0;
}
int DataManager::setAdminFile(string name) {
AdminFile.open(name, ios::in | ios::out | ios::binary);
if (AdminFile) {
AdminFile.seekg(0);
while (!AdminFile.eof() && AdminFile.peek() != EOF) {
AdminFile.read((char*)&admin, sizeof(Admin));
}
}
else {
cout << "adminFileNotFound,CreatNewOne.\n" << endl;
AdminFile.open(name, ios::out | ios::binary);
strcpy_s(admin.pwd, 17, "12345678");
cout << "admin defaut pwd:12345678\n" << endl;
admin.ballance = 0;
AdminFile.write((char*)&admin, sizeof(Admin));
AdminFile.close(); AdminFile.clear();
AdminFile.open(name, ios::in | ios::out | ios::binary);
}
AdminFile.seekg(0);
if (!AdminFile) { return -1; }
return 0;
}
int DataManager::checkExistUsr(string usrName) {
if (!UsrFile) { return -1; }
map<string, int>::iterator iter;
iter = UsrMap.find(usrName);
if (iter == UsrMap.end()) { return -404; }
else { return UsrMap[usrName]; }
}
int DataManager::verifyUsr(string usrName, string pwd) {
if (!UsrFile) { return -1; }
int id = checkExistUsr(usrName);
if (id < 0) { return -404; }
else {
if (strcmp(users[id].password, pwd.c_str()) == 0) {
return 0;
}
else { return -403; }
}
}
int DataManager::changePwd(string usrName, string newpwd) {
if (strcmp(usrName.c_str(), "admin") == 0) {
if (checkPwd(newpwd, 0) == 0) {
strcpy_s(admin.pwd, 17, newpwd.c_str());
AdminFile.seekp(0);
AdminFile.write((char*)&admin, sizeof(Admin));
return 0;
}
else {
return -400;
}
}
int seq = checkExistUsr(usrName);
int res = users[seq].changePwd(newpwd);
if (res == 0) {
UsrFile.seekp(seq * sizeof(User), ios::beg);
UsrFile.write((char*)&users[seq], sizeof(User));
return 0;
}
else { return -400; }
}
int DataManager::addUsr(User& tmp) {
if (!UsrFile) { return -1; }
int id = checkExistUsr(tmp.userName);
if (id != -404) {
return -2;
}
users.push_back(tmp);
int n = users.size() - 1;//vector下标从0开始
UsrMap[tmp.userName] = n;
UsrMap[tmp.UUID] = n;
UsrFile.seekp(0, ios::end);
UsrFile.write((char*)&tmp, sizeof(User));
return 0;
}
int DataManager::charge(string usr, double amount) {
if (!UsrFile) { return -1; }
int seq = UsrMap[usr];
if (seq < 0) { return -404; }
else {
int res = users[seq].charge(amount);
UsrFile.seekp(seq * sizeof(User), ios::beg);
UsrFile.write((char*)&users[seq], sizeof(User));
return res;
}
}
Json::Value DataManager::getUsrData(string usr) {
Json::Value j;
if (strcmp(usr.c_str(), "admin") == 0) {
for (int seq = 0; seq < users.size(); ++seq) {
Json::Value res;
res["usrname"] = users[seq].userName;
res["pwd"] = users[seq].password;
res["realName"] = users[seq].realName;
res["address"] = users[seq].address;
res["tel"] = users[seq].tel;
res["ballance"] = users[seq].AccBalance;
res["uuid"] = users[seq].UUID;
j.append(res);
}
}
else {
int seq = UsrMap[usr];
if (seq >= 0) {
Json::Value res;
res["ballance"] = users[seq].AccBalance;
res["usrname"] = users[seq].userName;
res["pwd"] = users[seq].password;
res["realName"] = users[seq].realName;
res["address"] = users[seq].address;
res["tel"] = users[seq].tel;
res["uuid"] = users[seq].UUID;
j.append(res);
}
}
return j;
}
//包裹
int DataManager::checkExistPkg(string uuid) {
if (!PkgFile) { return -1; }
map<string, int>::iterator iter;
iter = PkgMap.find(uuid);
if (iter == PkgMap.end()) { return -404; }
else { return PkgMap[uuid]; }
}
int DataManager::_addPkg(Package& tmp) {
if (!PkgFile || !UsrFile) { return -1; }
int id = checkExistPkg(tmp.UUID);
if (id != -404) {
return -500;
}
packages.push_back(tmp);
int n = packages.size() - 1;//vector下标从0开始
PkgMap[tmp.UUID] = n;
PkgFromMap[tmp.from].push_back(n);
PkgToMap[tmp.to].push_back(n);
PkgFile.seekp(0, ios::end);
PkgFile.write((char*)&tmp, sizeof(Package));
return 0;
}
int DataManager::addPkg(User& f, User& t, double amount, int type, string d) {
if (!PkgFile || !UsrFile) { return -1; }
if (type != 2 && type != 5 && type != 8) {
return -400;
}
if (f.AccBalance < amount * type) { return -402; }//没钱
else {
f.AccBalance -= amount * type;
admin.ballance += amount * type;
return _addPkg(Package(f, t, type, d));
}
}
Json::Value DataManager::getPkgData(string usr, char w) {
Json::Value res;
if ((w == 'a' || w == 'A') && (strcmp(usr.c_str(), "admin") == 0)) {
//admin、all,返回全部包裹信息
for (int seq = 0; seq < packages.size(); ++seq) {
Json::Value j;
j["from"] = packages[seq].from;
j["fAddr"] = packages[seq].fAddress;
j["to"] = packages[seq].to;
j["tAddr"] = packages[seq].tAddress;
j["sendTime"] = packages[seq].sendTime;
j["rcvTime"] = packages[seq].rcvTime;
j["type"] = packages[seq].type;
j["description"] = packages[seq].description;
j["UUID"] = packages[seq].UUID;
j["status"] = packages[seq].status;
res.append(j);
}
}
if (w == 's' || w == 'S') {//send
for (int i = 0; i < PkgFromMap[usr].size(); ++i) {
int seq = PkgFromMap[usr][i];
Json::Value j;
j["from"] = packages[seq].from;
j["fAddr"] = packages[seq].fAddress;
j["to"] = packages[seq].to;
j["tAddr"] = packages[seq].tAddress;
j["sendTime"] = packages[seq].sendTime;
j["rcvTime"] = packages[seq].rcvTime;
j["type"] = packages[seq].type;
j["description"] = packages[seq].description;
j["UUID"] = packages[seq].UUID;
j["status"] = packages[seq].status;
res.append(j);
}
}
if (w == 'r' || w == 'R') {//收件
for (int i = 0; i < PkgToMap[usr].size(); ++i) {
int seq = PkgToMap[usr][i];
Json::Value j;
j["from"] = packages[seq].from;
j["fAddr"] = packages[seq].fAddress;
j["to"] = packages[seq].to;
j["tAddr"] = packages[seq].tAddress;
j["sendTime"] = packages[seq].sendTime;
j["rcvTime"] = packages[seq].rcvTime;
j["type"] = packages[seq].type;
j["description"] = packages[seq].description;
j["UUID"] = packages[seq].UUID;
j["status"] = packages[seq].status;
res.append(j);
}
}
if (w == 'u' || w == 'U') {//uuid
int seq = PkgMap[usr];
Json::Value j;
j["from"] = packages[seq].from;
j["fAddr"] = packages[seq].fAddress;
j["to"] = packages[seq].to;
j["tAddr"] = packages[seq].tAddress;
j["sendTime"] = packages[seq].sendTime;
j["rcvTime"] = packages[seq].rcvTime;
j["type"] = packages[seq].type;
j["description"] = packages[seq].description;
j["UUID"] = packages[seq].UUID;
j["status"] = packages[seq].status;
res.append(j);
}
return res;
}
int DataManager::receiptPkg(string usr, string pkgUid) {
int seq = PkgMap[pkgUid];
if (seq < 0) { return -404; }
if (strcmp(packages[seq].to, usr.c_str()) != 0) {
return -400;
}
int res = packages[seq].receiption();
PkgFile.seekp(seq * sizeof(Package), ios::beg);
PkgFile.write((char*)&packages[seq], sizeof(Package));
return res;
}