-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrarianinterface.cpp
More file actions
508 lines (422 loc) · 17 KB
/
librarianinterface.cpp
File metadata and controls
508 lines (422 loc) · 17 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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include "librarianinterface.h"
#include "ui_librarianinterface.h"
#include <QMessageBox>
#include <QDate>
#include "bookmanagement.h"
namespace LibrarianUtils {
QString switchCategoryEnumToQString(enum Category category) {
qDebug() << category;
switch(category) {
case Category::Aerospace :
return "Aerospace";
case Category::Argiculture:
return "Argiculture";
case Category::Art :
return "Art";
case Category::BiologicalScience:
return "BiologicalScience";
case Category::Comprehensive:
return "Comprehensive";
case Category::Economic:
return "Economic";
case Category::Education:
return "Education";
case Category::HistoryAndGeography:
return "HistoryAndGeography";
case Category::IndustrialTechnology:
return "IndustrialTechnology";
case Category::LanguageAndWriting:
return "LanguageAndWriting";
case Category::Literature:
return "Literature";
case Category::MathematicalAndChemistry:
return "MathematicalAndChemistry";
case Category::Military:
return "Military";
case Category::Philosophy:
return "Philosophy";
case Category::ScienceFiction:
return "ScienceFiction";
case Category::SocialScience:
return "SocialScience";
case Category::PoliticalAndLaw:
return "PoliticalAndLaw";
case Category::Transportation:
return "Transportation";
case Category::MedicineAndHealth:
return "MedicineAndHealth";
case Category::EnvironmentScience:
return "EnvironmentScience";
default:
return "";
};
}
enum Category switchCategoryQStringToEnum(QString category) {
if(category == "Aerospace")
return Category::Aerospace;
else if(category == "Argiculture")
return Category::Argiculture;
else if(category == "Art")
return Category::Art ;
else if(category == "BiologicalScience")
return Category::BiologicalScience;
else if(category == "Comprehensive")
return Category::Comprehensive;
else if(category == "Economic")
return Category::Economic;
else if(category == "Education")
return Category::Education;
else if(category == "HistoryAndGeography")
return Category::HistoryAndGeography;
else if(category == "IndustrialTechnology")
return Category::IndustrialTechnology;
else if(category == "LanguageAndWriting")
return Category::LanguageAndWriting;
else if(category == "Literature")
return Category::Literature;
else if(category == "MathematicalAndChemistry")
return Category::MathematicalAndChemistry;
else if(category == "Military")
return Category::Military;
else if(category == "Philosophy")
return Category::Philosophy;
else if(category == "ScienceFiction")
return Category::ScienceFiction;
else if(category == "SocialScience")
return Category::SocialScience;
else if(category == "PoliticalAndLaw")
return Category::PoliticalAndLaw;
else if(category == "Transportation")
return Category::Transportation;
else if(category == "MedicineAndHealth")
return Category::MedicineAndHealth;
else if(category == "EnvironmentScience")
return Category::EnvironmentScience;
else
return Category::Comprehensive;
}
}
LibrarianInterface::LibrarianInterface(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LibrarianInterface)
{
ui->setupUi(this);
model = new QSqlQueryModel;
bookDetail = new BookDetail;
ui->bookNameRadio->setChecked(true);
// 设置列表不可编辑,只能选择一行,表头自适应
// 搜索列表
ui->bookList->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->bookList->setSelectionMode(QAbstractItemView::SingleSelection);
ui->bookList->setEditTriggers(QAbstractItemView::NoEditTriggers);
QHeaderView *bookInfoHeaderView = ui->bookList->horizontalHeader();
bookInfoHeaderView->setSectionResizeMode(QHeaderView::Stretch);
ui->deleteBookList->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->deleteBookList->setSelectionMode(QAbstractItemView::SingleSelection);
ui->deleteBookList->setEditTriggers(QAbstractItemView::NoEditTriggers);
bookInfoHeaderView = ui->deleteBookList->horizontalHeader();
bookInfoHeaderView->setSectionResizeMode(QHeaderView::Stretch);
ui->requestList->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->requestList->setSelectionMode(QAbstractItemView::SingleSelection);
ui->requestList->setEditTriggers(QAbstractItemView::NoEditTriggers);
bookInfoHeaderView = ui->requestList->horizontalHeader();
bookInfoHeaderView->setSectionResizeMode(QHeaderView::Stretch);
ui->returnBookList->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->returnBookList->setSelectionMode(QAbstractItemView::SingleSelection);
ui->returnBookList->setEditTriggers(QAbstractItemView::NoEditTriggers);
bookInfoHeaderView = ui->returnBookList->horizontalHeader();
bookInfoHeaderView->setSectionResizeMode(QHeaderView::Stretch);
// 设置图书管理员个人信息的ID无法修改
ui->librarianId->setEnabled(false);
// 设置发布公告页的信息无法编辑
ui->publishTime->setEnabled(false);
ui->publisherName->setEnabled(false);
// 拉取图书信息
pullBookInfoList();
// 获取请求列表
getRequestList();
}
LibrarianInterface::~LibrarianInterface()
{
delete ui;
}
void LibrarianInterface::setUserID(QString userID)
{
this->userID = userID;
}
void LibrarianInterface::pullBookInfoList()
{
QString sqlStr = QString("select %1, %2, %3, %4, %5 from book").arg("isbn", "b_name", "author", "category", "press");
model->setQuery(sqlStr);
// 填充数据
for(int i = 0; i < model->rowCount(); i++) {
ui->bookList->insertRow(ui->bookList->rowCount());
for(int j = 0; j < model->columnCount(); j++) {
QModelIndex item_index = model->index(i, j);
ui->bookList->setItem(i, j, new QTableWidgetItem(model->data(item_index).toString()));
ui->bookList->item(i, j)->setTextAlignment(Qt::AlignCenter); // 设置文字居中
}
}
}
bool LibrarianInterface::checkInput()
{
return true;
}
void LibrarianInterface::modifyUserInfo()
{
QString name = ui->librarianName->text();
QString passwd = ui->librarianPassword->text();
QString mail = ui->librarianMail->text();
if(checkInput()) {
QString sqlStr = QString("update librarian set l_name = '%1', l_password = '%2', l_mail = '%3' where l_id = %4")
.arg(name, passwd, mail, userID);
model->setQuery(sqlStr);
getUserInfo();
QMessageBox::information(this, "修改信息", "修改成功", QMessageBox::Ok);
}
}
void LibrarianInterface::getUserInfo()
{
QString sqlStr = QString("select * from librarian where l_id = '%1'").arg(userID);
model->setQuery(sqlStr);
// 从数据库获取个人信息并显示到UI上
QModelIndex index = model->index(0, 0);
ui->librarianId->setText(model->data(index).toString());
index = model->index(0, 1);
ui->librarianName->setText(model->data(index).toString());
index = model->index(0, 2);
ui->librarianMail->setText(model->data(index).toString());
index = model->index(0, 3);
ui->librarianPassword->setText(model->data(index).toString());
}
void LibrarianInterface::searchAndShow(QWidget *inputUI, QWidget *showUI, SearchWay way)
{
bookManagement = new BookManagement;
auto show = static_cast<QTableWidget*>(showUI);
auto inputText = qobject_cast<QLineEdit*>(inputUI)->text();
// 清空列表
while(show->rowCount() > 0)
show->removeRow(0);
if(inputText != nullptr) {
bookManagement->searchBook(inputText, way);
// 显示搜索出来的信息
for(int i = 0; i < bookManagement->bookList.length(); i++) {
QString temp = LibrarianUtils::switchCategoryEnumToQString(bookManagement->bookList.at(i)->category);
show->insertRow(show->rowCount());
show->setItem(i, 0, new QTableWidgetItem(bookManagement->bookList.at(i)->ISBN));
show->setItem(i, 1, new QTableWidgetItem(bookManagement->bookList.at(i)->bookName));
show->setItem(i, 2, new QTableWidgetItem(bookManagement->bookList.at(i)->author));
show->setItem(i, 3, new QTableWidgetItem(temp));
show->setItem(i, 4, new QTableWidgetItem(bookManagement->bookList.at(i)->press));
// 设置文字居中
for(int j = 0; j < ui->bookList->columnCount(); j++) {
show->item(i, j)->setTextAlignment(Qt::AlignCenter);
}
}
}
}
// 获取公告发布者姓名
QString LibrarianInterface::getPublisherName()
{
QString sqlStr = QString("select l_name from librarian where l_id = '%1'").arg(userID);
model->setQuery(sqlStr);
QModelIndex index = model->index(0, 0);
return model->data(index).toString();
}
void LibrarianInterface::publishAnnouncement()
{
QString date = ui->publishTime->text();
QString content = ui->announcementContent->toPlainText();
QString sqlStr = QString("insert into announcement (a_time, a_p_id, content) "
"values "
"('%1', '%2', '%3')")
.arg(date, userID, content);
if(content != nullptr) {
model->setQuery(sqlStr);
QMessageBox::information(this, "message", "发布成功");
ui->announcementContent->clear();
}
else
QMessageBox::critical(this, "Error", "公告内容不得为空");
}
void LibrarianInterface::addNewBook()
{
// 获取界面输入的信息
QString isbn = ui->addISBN->text();
QString bookName = ui->addBookName->text();
QString author = ui->addAuthor->text() != nullptr ? ui->addAuthor->text() : "佚名";
QString shelfLife = ui->dateEdit->date().toString("yy-MM-dd");
QString price = ui->addPrice->text();
QString category = ui->addCategory->currentText().split("(")[0];
QString press = ui->addPress->text();
QString inventory = ui->addInventory->text();
// 将数据插入图书信息表
QString sqlStr = QString("insert into book (isbn, inventory, b_name, author, shelf_life, price, category, press) "
"values "
"('%1', %2, '%3', '%4', '%5', %6, '%7', '%8')")
.arg(isbn, inventory, bookName, author, shelfLife, price, category, press);
qDebug() << sqlStr;
model->setQuery(sqlStr);
QMessageBox::information(this, "Message", "添加成功");
clearAddInfo();
}
void LibrarianInterface::clearAddInfo()
{
ui->addISBN->clear();
ui->addBookName->clear();
ui->addAuthor->clear();
ui->addPrice->clear();
ui->addPress->clear();
ui->addInventory->clear();
ui->addCategory->setCurrentIndex(0);
ui->dateEdit->setDate(QDate::currentDate());
}
void LibrarianInterface::showDetails(QString isbn)
{
bookDetail->setWindowTitle("书本详情");
bookDetail->setIsbn(isbn);
bookDetail->getDetail(false);
bookDetail->showEditMode();
bookDetail->show();
}
void LibrarianInterface::getRequestList()
{
QString sqlStr = QString("select bb_isbn, bb_name, bb_book_name, bb_start_time, bb_deadline, bb_renew_count from borrow_books "
"where bb_status = 0");
model->setQuery(sqlStr);
while(ui->requestList->rowCount() > 0)
ui->requestList->removeRow(0);
QModelIndex index;
for(int i = 0; i < model->rowCount(); i++) {
ui->requestList->insertRow(ui->requestList->rowCount());
for(int j = 0; j < model->columnCount(); j++) {
index = model->index(i, j);
ui->requestList->setItem(i, j, new QTableWidgetItem(model->data(index).toString()));
ui->requestList->item(i, j)->setTextAlignment(Qt::AlignCenter);
}
}
}
void LibrarianInterface::confirmRequest(QString isbn)
{
QString sqlStr = QString("update borrow_books set bb_status = 1 where bb_isbn = '%1'").arg(isbn);
model->setQuery(sqlStr);
getRequestList();
QMessageBox::information(this, "Message", "已通过请求");
}
void LibrarianInterface::getReturnBookList()
{
while(ui->returnBookList->rowCount() > 0)
ui->returnBookList->removeRow(0);
QString userID = ui->returnUserIDInput->text();
if(userID != nullptr) {
QString sqlStr = QString("select bb_isbn, bb_name, bb_book_name, bb_renew_count, bb_start_time, bb_deadline from borrow_books "
"where bb_name = (select u_name from user where u_id = '%1')").arg(userID);
qDebug() << sqlStr;
model->setQuery(sqlStr);
QModelIndex index;
QDate startTime;
QDate deadline;
for(int i = 0; i < model->rowCount(); i++) {
ui->returnBookList->insertRow(ui->returnBookList->rowCount());
for(int j = 0; j < model->columnCount(); j++) {
index = model->index(i, j);
if(j != 4 && j != 5) {
qDebug() << model->data(index).toString();
ui->returnBookList->setItem(i, j, new QTableWidgetItem(model->data(index).toString()));
} else if(j == 4) {
startTime = QDate::fromString(model->data(index).toString(), "yyyy-MM-dd");
} else if(j == 5) {
deadline = QDate::fromString(model->data(index).toString(), "yyyy-MM-dd");
QString time = QString("%1").arg((deadline.day() - startTime.day()) - (deadline.day() - QDate::currentDate().day()));
ui->returnBookList->setItem(i, j - 1, new QTableWidgetItem(time));
}
}
}
} else {
QMessageBox::critical(this, "Error", "查询信息不能为空");
}
}
void LibrarianInterface::returnBook(QString isbn)
{
QString sqlStr = QString("delete from borrow_books where bb_isbn = '%1'").arg(isbn);
model->setQuery(sqlStr);
QMessageBox::information(this, "Message", "删除成功");
getReturnBookList();
}
void LibrarianInterface::on_tabWidget_tabBarClicked(int index)
{
// 获取个人信息
getUserInfo();
// 设置发布公告基本信息
ui->publishTime->setText(QDate::currentDate().toString("yyyy-MM-dd"));
ui->publisherName->setText(getPublisherName());
pullBookInfoList();
}
void LibrarianInterface::on_editInfoBtn_clicked()
{
modifyUserInfo();
}
void LibrarianInterface::on_publishBtn_clicked()
{
publishAnnouncement();
}
void LibrarianInterface::on_bookSearchBtn_clicked()
{
if(ui->bookNameRadio->isChecked())
searchAndShow(ui->bookSearchInput, ui->bookList, SearchWay::ByName);
else if(ui->authorRadio->isChecked())
searchAndShow(ui->bookSearchInput, ui->bookList, SearchWay::ByAuthor);
else if(ui->isbnRadio->isChecked())
searchAndShow(ui->bookSearchInput, ui->bookList, SearchWay::ByBookISBN);
}
void LibrarianInterface::on_addBookBtn_clicked()
{
addNewBook();
}
void LibrarianInterface::on_bookInfoClear_clicked()
{
clearAddInfo();
}
void LibrarianInterface::on_deleteSearchBtn_clicked()
{
searchAndShow(ui->deleteSearchInput, ui->deleteBookList, SearchWay::ByBookISBN);
}
void LibrarianInterface::on_deleteBookBtn_clicked()
{
int currentRowIndex = ui->deleteBookList->currentRow();
auto currentRow = ui->deleteBookList->selectedItems();
QString isbn = currentRow.at(currentRowIndex)->text();
QString sqlStr = QString("delete from book where isbn = '%1'").arg(isbn);
model->setQuery(sqlStr);
// 清空查询列表数据
while(ui->deleteBookList->rowCount() > 0)
ui->deleteBookList->removeRow(0);
QMessageBox::information(this, "Message", "删除成功");
}
void LibrarianInterface::on_bookList_clicked(const QModelIndex &index)
{
int currentRow = ui->bookList->selectionModel()->currentIndex().row();
QString isbn = ui->bookList->item(currentRow, 0)->text();
showDetails(isbn);
}
void LibrarianInterface::on_pushButton_clicked()
{
requestRowIndex = ui->requestList->currentRow();
auto item = ui->requestList->selectedItems();
if(!item.isEmpty())
confirmRequest(item.at(0)->text());
else
QMessageBox::critical(this, "Error", "没有选中图书");
}
void LibrarianInterface::on_returnUserIDSearchBtn_clicked()
{
getReturnBookList();
}
void LibrarianInterface::on_returnBookBtn_clicked()
{
auto item = ui->returnBookList->selectedItems();
if(!item.isEmpty())
returnBook(item.at(0)->text());
else
QMessageBox::critical(this, "Error", "未选中书籍");
}