-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_db.cpp
296 lines (270 loc) · 9.27 KB
/
utils_db.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
#include "utils_db.h"
#include "classroom.h"
utils_db::utils_db()
{
db.setHostName(QString::fromStdString(hostname));
db.setPort(3306);
db.setDatabaseName(QString::fromStdString(databaseName));
db.setUserName(QString::fromStdString(databaseUsername));
db.setPassword(QString::fromStdString(databasePassword));
bool connectState = db.open();
if (connectState){
qDebug() << "success to connect databse system_db";
}
else {
qDebug() << "fail to connect databse system_db";
}
}
QSqlDatabase utils_db::getDatabase()
{
return db;
}
bool utils_db::queryUser(QString username)
{
QSqlQuery query(db);
query.prepare("select * from user_teacher where username = ?");
query.addBindValue(username);
query.exec();
return query.first();
}
bool utils_db::queryUser(QString username, QString password, string type)
{
QSqlQuery query(db);
if(type.compare("admin") == 0)
{
query.prepare("select * from user_admin where username = ? and password = ?");
}
else if(type.compare("teacher") == 0){
query.prepare("select * from user_teacher where username = ? and password = ?");
}
else{
return false;
}
query.addBindValue(username);
query.addBindValue(password);
query.exec();
return query.first();
}
bool utils_db::insertUser(QString username, QString password)
{
QSqlQuery insert(db);
if(username == nullptr)
{
return false;
}
if(!queryUser(username, password, "teacher"))
{
insert.prepare("insert into user_teacher (username, password) values (?, ?)");
insert.addBindValue(username);
insert.addBindValue(password);
return insert.exec();
}
return false;
}
bool utils_db::deleteUser(QString username)
{
QSqlQuery del(db);
del.prepare("delete from user_teacher where username = ?");
del.addBindValue(username);
return del.exec();
}
bool utils_db::updateUser(QString username, QString password)
{
QSqlQuery update(db);
if(!queryUser(username, password, "teacher"))
{
update.prepare("update user_teacher set username = ? , password = ? where username = ?");
update.addBindValue(username);
update.addBindValue(password);
update.addBindValue(username);
return update.exec();
}
return false;
}
bool utils_db::delAdminCheckedReservation(QString classroomId, QString teacherId)
{
QSqlQuery del(db);
del.prepare("delete from admin_reservation where classroomId = ? and teacherId = ?");
del.addBindValue(classroomId);
del.addBindValue(teacherId);
return del.exec();
}
bool utils_db::updateClassroomState(QString classroomId, QString reserveTime, QString type)
{
QSqlQuery update(db);
update.prepare("update classroom set state = ? where id = ? and reserveTime = ?");
update.addBindValue(type);
update.addBindValue(classroomId);
update.addBindValue(reserveTime);
return update.exec();
}
bool utils_db::delClassroom(QString classroomId, QString reserveTime)
{
QSqlQuery del(db);
del.prepare("delete from classroom where id = ? and reserveTime = ?");
del.addBindValue(classroomId);
del.addBindValue(reserveTime);
return del.exec();
}
bool utils_db::setAllClassroomsEmpty()
{
QSqlQuery update(db);
update.prepare("update classroom set state = '空置'");
return update.exec();
}
bool utils_db::setAllClassroomsReserve()
{
QSqlQuery update(db);
update.prepare("update classroom set state = '已预约'");
return update.exec();
}
QVariantList utils_db::queryAllClassrooms()
{
QVariantList classrooms;
QSqlQuery query(db);
query.prepare("select * from classroom");
query.exec();
while(query.next())
{
QVariant id = query.value("id");
QVariant className = query.value("className");
QVariant classroomType = query.value("classroomType");
QVariant maxPeople = query.value("maxPeople");
QVariant aircondition = query.value("aircondition");
QVariant camera = query.value("camera");
QVariant state = query.value("state");
classroom classroom(id, className, classroomType, maxPeople, aircondition, camera, state);
classrooms.append(QVariant::fromValue(classroom));
}
return classrooms;
}
bool utils_db::uppdateAdmin_ReservationTableState(QString classroomId, QString teacherId, QString reserveTime, QString type)
{
QSqlQuery update(db);
update.prepare("update admin_reservation set state = ? where classroomId = ? and teacherId = ? and reserveTime = ?");
update.addBindValue(type);
update.addBindValue(classroomId);
update.addBindValue(teacherId);
update.addBindValue(reserveTime);
return update.exec();
}
bool utils_db::teacherUpdatehisReserveTime(QString classroomId, QString reserveTime, QString upDateTime, QString state)
{
QSqlQuery update(db);
if(teacherUpdatehisReserveCheck(classroomId, upDateTime, "空置"))
{
classroomStateUpdate(classroomId, upDateTime, "已预约");
classroomStateUpdate(classroomId, reserveTime, "空置");
update.prepare("update admin_reservation set reserveTime = ? where classroomId = ? and reserveTime = ? and state = ?");
update.addBindValue(upDateTime);
update.addBindValue(classroomId);
update.addBindValue(reserveTime);
update.addBindValue(state);
return update.exec();
}
return false;
}
bool utils_db::teacherUpdatehisReserveCheck(QString classroomId, QString reserveTime, QString state)
{
QSqlQuery check(db);
check.prepare("select * from classroom where id = ? and reserveTime = ? and state = ?");
check.addBindValue(classroomId);
check.addBindValue(reserveTime);
check.addBindValue(state);
check.exec();
return check.first();
}
bool utils_db::teacherUpdatehisReserveClassroom(QString classroomId, QString reserveTime, QString upClassroomId, QString state)
{
QSqlQuery update(db);
if(teacherUpdatehisReserveCheck(upClassroomId, reserveTime, "空置"))
{
classroomStateUpdate(upClassroomId, reserveTime, "已预约");
classroomStateUpdate(classroomId, reserveTime, "空置");
update.prepare("update admin_reservation set classroomId = ? where classroomId = ? and reserveTime = ? and state = ?");
update.addBindValue(upClassroomId);
update.addBindValue(classroomId);
update.addBindValue(reserveTime);
update.addBindValue(state);
return update.exec();
}
return false;
}
bool utils_db::teacherDelSingleReservatiom(QString id, QString classroomId, QString reserveTime)
{
QSqlQuery del(db);
del.prepare("delete from admin_reservation where teacherId = ? and classroomId = ? and reserveTime = ?");
del.addBindValue(id);
del.addBindValue(classroomId);
del.addBindValue(reserveTime);
return del.exec();
}
bool utils_db::classroomStateUpdate(QString classroomId, QString reserveTime, QString state)
{
QSqlQuery update(db);
update.prepare("update classroom set state = ? where id = ? and reserveTime = ?");
update.addBindValue(state);
update.addBindValue(classroomId);
update.addBindValue(reserveTime);
return update.exec();
}
bool utils_db::addReservation(QString classroomId, QString teacherId, QString teacherName, QString submitTime, QString reserveTime)
{
QSqlQuery add(db);
add.prepare("insert into admin_reservation (classroomId, teacherId, teacherName, submitTime, reserveTime, state) values (?, ?, ?, ?, ?, '待审核')");
add.addBindValue(classroomId);
add.addBindValue(teacherId);
add.addBindValue(teacherName);
add.addBindValue(submitTime);
add.addBindValue(reserveTime);
return add.exec();
}
QString utils_db::searchNameById(QString teacherId)
{
QSqlQuery query(db);
query.prepare("select name from teacher where id = ?");
query.addBindValue(teacherId);
query.exec();
QSqlRecord rec = query.record();
QString name;
while(query.next())
{
rec = query.record();
name = query.value("name").toString();
}
return name;
}
QStringList utils_db::queryALLClassroomIdByTimeandState(QString time, QString state)
{
QSqlQuery query(db);
query.prepare("select id from classroom where reserveTime = ? and state = ?");
query.addBindValue(time);
query.addBindValue(state);
query.exec();
QSqlRecord rec = query.record();
QString name;
QStringList ids;
while(query.next())
{
rec = query.record();
ids.append(query.value("id").toString());
}
return ids;
}
QStringList utils_db::queryAbleReserveTimeByClassroomId(QString id, QString state)
{
QSqlQuery query(db);
query.prepare("select reserveTime from classroom where id = ? and state = ?");
query.addBindValue(id);
query.addBindValue(state);
query.exec();
QSqlRecord rec = query.record();
QString name;
QStringList reserveTime;
while(query.next())
{
rec = query.record();
reserveTime.append(query.value("reserveTime").toString());
}
return reserveTime;
}