-
Notifications
You must be signed in to change notification settings - Fork 0
/
adminui.cpp
449 lines (398 loc) · 15.7 KB
/
adminui.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "adminui.h"
#include "ui_adminui.h"
#include<QPixmap>
#include <QTableView>
#include<QStandardItemModel>
#include"logindialog.h"
#include<QMessageBox>
#include<QDebug>
#include<QSqlError>
#include<QSqlTableModel>
#include<QSqlQuery>
#include<QSqlQueryModel>
#include<QDateTime>
#include<QDebug>
AdminUi::AdminUi(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AdminUi)
{
ui->setupUi(this);
ui->label_5->setAlignment(Qt::AlignCenter);
ui->label_6->setAlignment(Qt::AlignCenter);
//表格1设计
ui->tableView->horizontalHeader()->setStyleSheet(R"(
QHeaderView::section {
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(51, 153, 255,1), stop:1 rgba(52, 246, 177,1));
color: white;
border: none;
font-size: 20px;
font-family: "微软雅黑";
padding-top: 5;
padding-bottom: 5;
}
)");
ui->tableView->verticalHeader()->setStyleSheet(R"(
QHeaderView::section {
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(51, 153, 255,1), stop:1 rgba(52, 246, 177,1));
color: white;
border: none;
font-size: 20px;
font-family: "微软雅黑";
}
)");
ui->tableView->viewport()->setStyleSheet("background-color: lightblue;");
ui->tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->tableView->resizeRowsToContents();
ui->tableView->resizeColumnsToContents();
ui->tableView->horizontalHeader()->setStretchLastSection(true);
//表格2
ui->tableView_2->horizontalHeader()->setStyleSheet(R"(
QHeaderView::section {
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(51, 153, 255,1), stop:1 rgba(52, 246, 177,1));
color: white;
border: none;
font-size: 20px;
font-family: "微软雅黑";
padding-top: 5;
padding-bottom: 5;
}
)");
ui->tableView_2->verticalHeader()->setStyleSheet(R"(
QHeaderView::section {
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(51, 153, 255,1), stop:1 rgba(52, 246, 177,1));
color: white;
border: none;
font-size: 20px;
font-family: "微软雅黑";
}
)");
ui->tableView_2->viewport()->setStyleSheet("background-color: lightblue;");
ui->tableView_2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->tableView_2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->tableView_2->resizeRowsToContents();
ui->tableView_2->resizeColumnsToContents();
ui->tableView_2->horizontalHeader()->setStretchLastSection(true);
//表格3
ui->tableView_3->setStyleSheet(
"QHeaderView::section {"
"color: white;"
"border: none;"
"font-size: 20px;"
"font-family: '微软雅黑';"
"background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:1, y2:0, stop:0 rgba(51, 153, 255,1), stop:1 rgba(52, 246, 177,1));"
"padding-top: 5;"
"padding-bottom: 5;"
"}"
"QTableView {"
"background-color: rgba(224, 224, 224, 0.5);"
"selection-background-color: rgba(76, 175, 80, 0.5);"
"}"
"QTableView::item {"
"padding: 5px;"
"}"
);
ui->tableView_3->verticalHeader()->setVisible(false);
ui->tableView_3->horizontalHeader()->setStretchLastSection(true);
ui->tableView_3->horizontalHeader()->setHighlightSections(false);
ui->tableView_3->setShowGrid(false);
ui->tableView_3->setFrameStyle(QFrame::NoFrame);
ui->tableView_3->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//初始化数据库
db2_ = QSqlDatabase::addDatabase("QMYSQL");
//连接数据库
db2_.setHostName("127.0.0.1");
db2_.setUserName("HWH");
db2_.setPassword("@MySql668931hwh");
db2_.setDatabaseName("login");
if(!db2_.open())//数据库打开失败
{
QMessageBox::warning(this,"错误",db2_.lastError().text());
qDebug()<<db2_.lastError().text();
return;
}
model = new QSqlTableModel(this,db2_);
model->setTable("books");
ui->tableView->setModel(model);
model->select();
//查询功能
//页面跳转
connect(ui->pushButton, &QPushButton::clicked, [this]()
{
ui->stackedWidget->setCurrentIndex(0);
});
// 设置下拉列表的内容
ui->comboBox->addItems(QStringList() << "书名" << "作者名" << "ISBN" << "索书号" << "出版社"<<"丛书名");
// 连接查询按钮的点击事件到查询函数
connect(ui->pushButton_8, &QPushButton::clicked, this, &AdminUi::searchBook);
//借书模块
//翻页
connect(ui->pushButton_2, &QPushButton::clicked, [this]()
{
ui->stackedWidget->setCurrentIndex(1);
});
connect(ui->pushButton_11, &QPushButton::clicked, this, &AdminUi::borrowBook);
//还书
connect(ui->pushButton_12, &QPushButton::clicked, this, &AdminUi::returnBook);
//查看及修改预约信息
connect(ui->pushButton_3, &QPushButton::clicked, [this]()
{
ui->stackedWidget->setCurrentIndex(2);
});
model2 = new QSqlTableModel(this,db2_);
model2->setTable("reserve");
ui->tableView_2->setModel(model2);
model2->select();
connect(ui->pushButton_13, &QPushButton::clicked, this, &AdminUi::Delete);
connect(ui->pushButton_14, &QPushButton::clicked, this, &AdminUi::confirm);
connect(ui->pushButton_15, &QPushButton::clicked, this, &AdminUi::cancel);
//
connect(ui->pushButton_10, &QPushButton::clicked, [this]()
{
ui->stackedWidget->setCurrentIndex(3);
});
//查看并修改所有违约记录
model3 = new QSqlTableModel(this,db2_);
model3->setTable("violation");
ui->tableView_3->setModel(model3);
model3->select();
connect(ui->pushButton_16, &QPushButton::clicked, this, &AdminUi::add);
connect(ui->pushButton_17, &QPushButton::clicked, this, &AdminUi::Delete);
connect(ui->pushButton_18, &QPushButton::clicked, this, &AdminUi::confirm);
connect(ui->pushButton_19, &QPushButton::clicked, this, &AdminUi::cancel);
}
void AdminUi::setInfo(const QString&Id,const QString&password)
{
this->Id = Id;
this->password = password;
}
void AdminUi::searchBook()
{
QString searchType = ui->comboBox->currentText();
QString searchText = ui->lineEdit->text();
QString queryString;
if (searchType == "书名") {
queryString = "书名 LIKE '%" + searchText + "%'";
} else if (searchType == "作者名") {
queryString = "作者名 LIKE '%" + searchText + "%'";
} else if (searchType == "书目编号") {
queryString = "书目编号 = '" + searchText + "'";
} else if (searchType == "索书号") {
queryString = "索书号 LIKE '%" + searchText + "%'";
} else if (searchType == "出版社") {
queryString = "出版社 LIKE '%" + searchText + "%'";
}else if (searchType == "丛书名") {
queryString = " 丛书名 LIKE '%" + searchText + "%'";
}
model->setFilter(queryString);
model->select();
}
void AdminUi::add()
{
int newRow = model->rowCount();
model->insertRow(newRow);
ui->tableView->selectRow(newRow);
ui->tableView->setFocus();
}
void AdminUi::Delete()
{
QItemSelectionModel *select = ui->tableView->selectionModel();
if (select->hasSelection())
{
model->removeRow(select->currentIndex().row());
}
}
void AdminUi::confirm()
{
model->submitAll();
}
void AdminUi::cancel()
{
model->revertAll();
}
void AdminUi::borrowBook()
{
QString bookID = ui->lineEdit_2->text();
QString studentID = ui->lineEdit_3->text();
QString studentName;
QString bookName;
int bookStatus;
query = new QSqlQuery(db2_);
query->prepare("SELECT 书名, 状态 FROM books WHERE 条码号 = :bookID");
query->bindValue(":bookID", bookID);
if (!query->exec() || !query->next())
{
QMessageBox::warning(this, "错误", query->lastError().text());
return;
}
bookName = query->value(0).toString();
bookStatus = query->value(1).toInt();
// 检查学生信息
QSqlQuery studentQuery(db2_);
studentQuery.prepare("SELECT 学生姓名 FROM stuinfo WHERE 一卡通号 = :studentID");
studentQuery.bindValue(":studentID", studentID);
if (!studentQuery.exec() || !studentQuery.next())
{
QMessageBox::warning(this, "错误", "找不到学生信息!");
return;
}
studentName = studentQuery.value(0).toString();
if (bookStatus == 1)
{
// 书存在
QSqlQuery updateQuery(db2_);
updateQuery.prepare("UPDATE books SET 状态 = 0 WHERE 条码号 = :bookID"); // 状态为0
updateQuery.bindValue(":bookID", bookID);
if (!updateQuery.exec())
{
QMessageBox::warning(this, "错误", updateQuery.lastError().text());
return;
}
// 减少库存
QSqlQuery decreaseInventoryQuery(db2_);
decreaseInventoryQuery.prepare("UPDATE books SET 现有数 = 现有数 - 1 WHERE 书名 = :bookName AND 现有数 > 0");
decreaseInventoryQuery.bindValue(":bookName", bookName);
if (!decreaseInventoryQuery.exec())
{
QMessageBox::warning(this, "错误", decreaseInventoryQuery.lastError().text());
return;
}
}
else if (bookStatus == 0)
{
// 检查是否预定
QSqlQuery reserveQuery(db2_);
reserveQuery.prepare("SELECT 一卡通号 FROM reserve WHERE 书名 = :bookName");
reserveQuery.bindValue(":bookName", bookName);
if (!reserveQuery.exec() || !reserveQuery.next())
{
QMessageBox::warning(this, "错误", "这本书不可借阅!");
return;
}
QString reservedStudentId = reserveQuery.value(0).toString();
if (reservedStudentId != studentID)
{
QMessageBox::warning(this, "错误", "这本书已被其他学生预约!");
return;
}
// 更新书的状态
QSqlQuery updateQuery(db2_);
updateQuery.prepare("UPDATE books SET 状态 = 0 WHERE 条码号 = :bookID"); // Setting status to borrowed
updateQuery.bindValue(":bookID", bookID);
if (!updateQuery.exec())
{
QMessageBox::warning(this, "错误", updateQuery.lastError().text());
return;
}
// 减少库存
QSqlQuery decreaseInventoryQuery(db2_);
decreaseInventoryQuery.prepare("UPDATE books SET 现有数 = 现有数 - 1 WHERE 书名 = :bookName AND 现有数 > 0");
decreaseInventoryQuery.bindValue(":bookName", bookName);
if (!decreaseInventoryQuery.exec())
{
QMessageBox::warning(this, "错误", decreaseInventoryQuery.lastError().text());
return;
}
}
else
{
QMessageBox::warning(this, "错误", "这本书不可借阅!");
return;
}
// 储存相关信息
QDateTime currentDateTime = QDateTime::currentDateTime();
QDateTime returnDateTime = currentDateTime.addDays(30); // 借书时间是30天
QSqlQuery insertQuery(db2_);
insertQuery.prepare("INSERT INTO stucurrent (一卡通号, 学生姓名, 借阅时间, 应还时间, 书名, 条码号,可续借次数) "
"VALUES (:studentID, :studentName, :borrowDate, :returnDate, :bookName, :bookCode, :time)");
insertQuery.bindValue(":studentID", studentID);
insertQuery.bindValue(":studentName", studentName);
insertQuery.bindValue(":borrowDate", currentDateTime);
insertQuery.bindValue(":returnDate", returnDateTime);
insertQuery.bindValue(":bookName", bookName);
insertQuery.bindValue(":bookCode", bookID);
insertQuery.bindValue(":time", 4);
if (!insertQuery.exec())
{
QMessageBox::warning(this, "错误", insertQuery.lastError().text());
return;
}
QMessageBox::information(this, "成功", "借书成功!");
}
void AdminUi::returnBook()
{
QString bookID = ui->lineEdit_4->text();
QString studentID = ui->lineEdit_5->text();
// 检查学生是否借书了
QSqlQuery checkBorrowedQuery(db2_);
checkBorrowedQuery.prepare("SELECT 应还时间, 学生姓名 FROM stucurrent WHERE 一卡通号 = :studentID AND 条码号 = :bookID");
checkBorrowedQuery.bindValue(":studentID", studentID);
checkBorrowedQuery.bindValue(":bookID", bookID);
if (!checkBorrowedQuery.exec())
{
QMessageBox::warning(this, "错误", checkBorrowedQuery.lastError().text());
return;
}
if (!checkBorrowedQuery.next())
{
QMessageBox::warning(this, "错误", "该学生没有借阅这本书或已还书!");
return;
}
QDateTime returnDate = checkBorrowedQuery.value(0).toDateTime();
QString studentName = checkBorrowedQuery.value(1).toString();
QDateTime currentDate = QDateTime::currentDateTime();
// 判断是否超期
if (currentDate > returnDate)
{
int lateDays = returnDate.daysTo(currentDate);
double fine = lateDays * 1.0; // 每天罚款1元
// 在violation表中增加违约记录
QSqlQuery violationQuery(db2_);
violationQuery.prepare("INSERT INTO violation (一卡通号, 学生姓名, 违约时间, 违约原因) "
"VALUES (:studentID, :studentName, :currentDate, '超期还书')");
violationQuery.bindValue(":studentID", studentID);
violationQuery.bindValue(":studentName", studentName);
violationQuery.bindValue(":currentDate", currentDate);
if (!violationQuery.exec())
{
qDebug() << "在violation表中增加记录错误:" << violationQuery.lastError().text();
}
QMessageBox::warning(this, "欠款提示", QString("你已超期%1天,需缴纳罚款%2元。").arg(lateDays).arg(fine));
}
// 更新books表中的状态
QSqlQuery updateStatusQuery(db2_);
updateStatusQuery.prepare("UPDATE books SET 状态 = 1 WHERE 条码号 = :bookID");
updateStatusQuery.bindValue(":bookID", bookID);
if (!updateStatusQuery.exec())
{
QMessageBox::warning(this, "错误", updateStatusQuery.lastError().text());
return;
}
// 更新books表中的现有数
QSqlQuery increaseInventoryQuery(db2_);
increaseInventoryQuery.prepare("UPDATE books SET 现有数 = 现有数 + 1 WHERE 条码号 = :bookID");
increaseInventoryQuery.bindValue(":bookID", bookID);
if (!increaseInventoryQuery.exec())
{
QMessageBox::warning(this, "错误", increaseInventoryQuery.lastError().text());
return;
}
// 从stucurrent表中删除记录
QSqlQuery deleteRecordQuery(db2_);
deleteRecordQuery.prepare("DELETE FROM stucurrent WHERE 一卡通号 = :studentID AND 条码号 = :bookID");
deleteRecordQuery.bindValue(":studentID", studentID);
deleteRecordQuery.bindValue(":bookID", bookID);
if (!deleteRecordQuery.exec())
{
QMessageBox::warning(this, "错误", deleteRecordQuery.lastError().text());
return;
}
QMessageBox::information(this, "成功", "还书成功!");
}
AdminUi::~AdminUi()
{
delete ui;
if(model)delete model;
if(query)delete query;
if(query2)delete query2;
if(query3)delete query3;
}