-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreaderwindow.cpp
More file actions
1527 lines (1321 loc) · 53.8 KB
/
Copy pathreaderwindow.cpp
File metadata and controls
1527 lines (1321 loc) · 53.8 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file readerwindow.cpp
* @brief 读者窗口类实现
*
* 实现ReaderWindow类的所有成员函数,包括图书查询、借阅管理、
* 预约管理、消息管理和个人信息等功能。
*/
#include "readerwindow.h"
#include "Reader.h"
#include "StudentReader.h"
#include "TeacherReader.h"
#include "ExternalReader.h"
#include "DataManager.h"
#include <QIcon>
#include <QDialog>
#include <QLabel>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QDateTime>
#include <QInputDialog>
/**
* @brief 构造函数
* @param user 当前登录的用户对象
* @param parent 父窗口
*
* 初始化读者窗口,设置窗口属性、工具栏和各个功能页面。
*/
ReaderWindow::ReaderWindow(User *user, QWidget *parent)
: QMainWindow(parent)
{
currentUser = user;
resize(800, 800);
setWindowIcon(QIcon(":/image/book.png"));
// 禁用关闭按钮
setWindowFlags(windowFlags() & ~Qt::WindowCloseButtonHint);
if (user)
{
QString title = QString("%1 - %2").arg(user->typeToIdentity()).arg(user->getName());
setWindowTitle(title);
}
setupCentralWidget();
}
/**
* @brief 析构函数
*/
ReaderWindow::~ReaderWindow()
{
}
/**
* @brief 设置中心窗口
*
* 初始化工具栏和堆叠窗口,创建各个功能页面并添加到堆叠窗口中。
*/
void ReaderWindow::setupCentralWidget()
{
stackedWidget = new QStackedWidget(this);
setCentralWidget(stackedWidget);
toolbar = new QToolBar("工具栏", this);
toolbar->setMovable(false);
toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
addToolBar(Qt::LeftToolBarArea, toolbar);
// 创建工具栏动作
QAction *bookSearchAction = new QAction(QIcon(":/image/book2.png"), "图书查询", this);
QAction *myBorrowAction = new QAction(QIcon(":/image/readerborrow.png"), "我的借阅", this);
QAction *myReservationAction = new QAction(QIcon(":/image/readerReservation.png"), "我的预约", this);
QAction *messageAction = new QAction(QIcon(":/image/message.png"), "消息管理", this);
QAction *infoAction = new QAction(QIcon(":/image/user.png"), "个人信息", this);
QAction *logoutAction = new QAction(QIcon(":/image/logout.png"), "退出登录", this);
// 添加动作到工具栏
toolbar->addAction(bookSearchAction);
toolbar->addAction(myBorrowAction);
toolbar->addAction(myReservationAction);
toolbar->addAction(messageAction);
toolbar->addAction(infoAction);
toolbar->addAction(logoutAction);
// 连接信号槽
connect(bookSearchAction, &QAction::triggered, this, &ReaderWindow::switchToBookSearch);
connect(myBorrowAction, &QAction::triggered, this, &ReaderWindow::switchToMyBorrow);
connect(myReservationAction, &QAction::triggered, this, &ReaderWindow::switchToMyReservation);
connect(messageAction, &QAction::triggered, this, &ReaderWindow::onCheckMessages);
connect(infoAction, &QAction::triggered, this, &ReaderWindow::switchToInfo);
connect(logoutAction, &QAction::triggered, this, &ReaderWindow::onLogout);
// 设置各个页面
setupBookSearchWidget();
setupMyBorrowWidget();
setupMyReservationWidget();
setupMessageWidget();
setupInfoWidget();
// 添加页面到堆叠窗口
stackedWidget->addWidget(bookSearchWidget);
stackedWidget->addWidget(myBorrowWidget);
stackedWidget->addWidget(myReservationWidget);
stackedWidget->addWidget(messageWidget);
stackedWidget->addWidget(infoWidget);
// 默认显示图书查询页面
stackedWidget->setCurrentIndex(0);
}
/**
* @brief 设置图书查询页面
*
* 创建图书查询界面,包括搜索输入框、操作按钮和图书表格。
*/
void ReaderWindow::setupBookSearchWidget()
{
bookSearchWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(bookSearchWidget);
// 搜索区域
QHBoxLayout *searchLayout = new QHBoxLayout();
// 操作按钮(预约、借书)
bookReserveBtn = new QPushButton("预约", this);
borrowBtn = new QPushButton("借书", this);
searchLayout->addWidget(bookReserveBtn);
searchLayout->addWidget(borrowBtn);
// 添加分隔线
QFrame *separator = new QFrame(this);
separator->setFrameShape(QFrame::VLine);
separator->setFrameShadow(QFrame::Sunken);
searchLayout->addWidget(separator);
// 输入框(无标签,使用placeholder提示)
bookISBNLineEdit = new QLineEdit(this);
bookISBNLineEdit->setPlaceholderText("ISBN");
bookISBNLineEdit->setFixedWidth(150);
bookTitleLineEdit = new QLineEdit(this);
bookTitleLineEdit->setPlaceholderText("书名");
bookTitleLineEdit->setFixedWidth(150);
bookAuthorLineEdit = new QLineEdit(this);
bookAuthorLineEdit->setPlaceholderText("作者");
bookAuthorLineEdit->setFixedWidth(120);
bookCategoryLineEdit = new QLineEdit(this);
bookCategoryLineEdit->setPlaceholderText("分类");
bookCategoryLineEdit->setFixedWidth(100);
searchLayout->addWidget(bookISBNLineEdit);
searchLayout->addWidget(bookTitleLineEdit);
searchLayout->addWidget(bookAuthorLineEdit);
searchLayout->addWidget(bookCategoryLineEdit);
// 查找按钮紧跟在分类输入框后面
bookSearchBtn = new QPushButton("查找", this);
searchLayout->addWidget(bookSearchBtn);
// 添加伸缩空间到末尾,保持控件紧凑
searchLayout->addStretch();
mainLayout->addLayout(searchLayout);
// 图书表格
bookSearchTable = new QTableWidget(this);
bookSearchTable->setColumnCount(10);
QStringList headers = {"ISBN", "书名", "作者", "分类", "库存", "入库时间", "借阅次数", "当前借出", "状态", "预约人数"};
bookSearchTable->setHorizontalHeaderLabels(headers);
bookSearchTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
bookSearchTable->setSelectionBehavior(QTableWidget::SelectRows);
mainLayout->addWidget(bookSearchTable);
// 连接信号槽
connect(bookSearchBtn, &QPushButton::clicked, this, &ReaderWindow::onBookSearch);
connect(bookReserveBtn, &QPushButton::clicked, this, &ReaderWindow::onBookReserve);
connect(borrowBtn, &QPushButton::clicked, this, &ReaderWindow::onBorrowBook);
// 初始化显示所有图书
onBookSearch();
}
/**
* @brief 设置我的借阅页面
*
* 创建借阅管理界面,包括操作按钮(还书、续借、支付罚款)、
* 查询输入框和借阅记录表格。
*/
void ReaderWindow::setupMyBorrowWidget()
{
myBorrowWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(myBorrowWidget);
// 顶部工具栏区域
QHBoxLayout *topLayout = new QHBoxLayout();
// 操作按钮
returnBtn = new QPushButton("还书", this);
renewBtn = new QPushButton("续借", this);
payFineBtn = new QPushButton("支付所有罚款", this);
topLayout->addWidget(returnBtn);
topLayout->addWidget(renewBtn);
topLayout->addWidget(payFineBtn);
// 添加分隔线
QFrame *separator = new QFrame(this);
separator->setFrameShape(QFrame::VLine);
separator->setFrameShadow(QFrame::Sunken);
topLayout->addWidget(separator);
// 查询输入框
borrowISBNEdit = new QLineEdit(this);
borrowISBNEdit->setPlaceholderText("ISBN");
borrowISBNEdit->setFixedWidth(120);
borrowTitleEdit = new QLineEdit(this);
borrowTitleEdit->setPlaceholderText("书名");
borrowTitleEdit->setFixedWidth(150);
borrowTimeEdit = new QLineEdit(this);
borrowTimeEdit->setPlaceholderText("借阅时间");
borrowTimeEdit->setFixedWidth(120);
borrowDueTimeEdit = new QLineEdit(this);
borrowDueTimeEdit->setPlaceholderText("应还时间");
borrowDueTimeEdit->setFixedWidth(120);
borrowReturnTimeEdit = new QLineEdit(this);
borrowReturnTimeEdit->setPlaceholderText("归还时间");
borrowReturnTimeEdit->setFixedWidth(120);
borrowStatusEdit = new QLineEdit(this);
borrowStatusEdit->setPlaceholderText("状态");
borrowStatusEdit->setFixedWidth(80);
borrowRenewCombo = new QComboBox(this);
borrowRenewCombo->addItem("");
borrowRenewCombo->addItem("无申请");
borrowRenewCombo->addItem("待审核");
borrowRenewCombo->addItem("已通过");
borrowRenewCombo->addItem("已拒绝");
borrowRenewCombo->setFixedWidth(80);
borrowFineCombo = new QComboBox(this);
borrowFineCombo->addItem("");
borrowFineCombo->addItem("未支付");
borrowFineCombo->addItem("已支付");
borrowFineCombo->setFixedWidth(80);
QPushButton *searchBorrowBtn = new QPushButton("查找", this);
topLayout->addWidget(borrowISBNEdit);
topLayout->addWidget(borrowTitleEdit);
topLayout->addWidget(borrowTimeEdit);
topLayout->addWidget(borrowDueTimeEdit);
topLayout->addWidget(borrowReturnTimeEdit);
topLayout->addWidget(borrowStatusEdit);
topLayout->addWidget(borrowRenewCombo);
topLayout->addWidget(borrowFineCombo);
topLayout->addWidget(searchBorrowBtn);
topLayout->addStretch();
mainLayout->addLayout(topLayout);
// 借阅记录表格
myBorrowTable = new QTableWidget(this);
myBorrowTable->setColumnCount(9);
QStringList headers = {"ISBN", "书名", "借阅时间", "应还时间", "状态", "续借状态", "续借次数", "罚款金额", "已支付罚款"};
myBorrowTable->setHorizontalHeaderLabels(headers);
myBorrowTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
myBorrowTable->setSelectionBehavior(QTableWidget::SelectRows);
mainLayout->addWidget(myBorrowTable);
// 连接信号槽
connect(returnBtn, &QPushButton::clicked, this, &ReaderWindow::onReturnBook);
connect(renewBtn, &QPushButton::clicked, this, &ReaderWindow::onRenewBook);
connect(payFineBtn, &QPushButton::clicked, this, &ReaderWindow::onPayAllFines);
connect(searchBorrowBtn, &QPushButton::clicked, this, &ReaderWindow::onSearchBorrow);
// 初始化显示借阅记录
displayMyBorrowRecords();
}
/**
* @brief 设置我的预约页面
*
* 创建预约管理界面,包括取消预约按钮、查询输入框和预约记录表格。
*/
void ReaderWindow::setupMyReservationWidget()
{
myReservationWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(myReservationWidget);
// 顶部按钮区域
QHBoxLayout *topLayout = new QHBoxLayout(this);
QPushButton *cancelReservationBtn = new QPushButton("取消预约", this);
topLayout->addWidget(cancelReservationBtn);
// 添加分隔线
QFrame *separator = new QFrame(this);
separator->setFrameShape(QFrame::VLine);
separator->setFrameShadow(QFrame::Sunken);
topLayout->addWidget(separator);
// 预约查找输入框
reservationTimeEdit = new QLineEdit(this);
reservationTimeEdit->setPlaceholderText("预约时间");
reservationTimeEdit->setFixedWidth(150);
reservationISBNEdit = new QLineEdit(this);
reservationISBNEdit->setPlaceholderText("ISBN");
reservationISBNEdit->setFixedWidth(150);
reservationTitleEdit = new QLineEdit(this);
reservationTitleEdit->setPlaceholderText("书名");
reservationTitleEdit->setFixedWidth(200);
reservationStatusCombo = new QComboBox(this);
reservationStatusCombo->addItem("");
reservationStatusCombo->addItem("待审核");
reservationStatusCombo->addItem("审核成功");
reservationStatusCombo->setFixedWidth(80);
QPushButton *searchReservationBtn = new QPushButton("查找预约", this);
topLayout->addWidget(reservationISBNEdit);
topLayout->addWidget(reservationTitleEdit);
topLayout->addWidget(reservationTimeEdit);
topLayout->addWidget(reservationStatusCombo);
topLayout->addWidget(searchReservationBtn);
topLayout->addStretch();
// 连接信号槽
connect(cancelReservationBtn, &QPushButton::clicked, this, &ReaderWindow::onCancelReservation);
connect(searchReservationBtn, &QPushButton::clicked, this, &ReaderWindow::onSearchReservation);
mainLayout->addLayout(topLayout);
// 预约记录表格
myReservationTable = new QTableWidget(this);
myReservationTable->setColumnCount(4);
QStringList headers = {"ISBN", "书名", "预约时间", "状态"};
myReservationTable->setHorizontalHeaderLabels(headers);
myReservationTable->horizontalHeader()->setStretchLastSection(true);
myReservationTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
myReservationTable->setSelectionBehavior(QTableWidget::SelectRows);
myReservationTable->setColumnWidth(0, 120);
myReservationTable->setColumnWidth(1, 250);
myReservationTable->setColumnWidth(2, 150);
myReservationTable->setColumnWidth(3, 80);
mainLayout->addWidget(myReservationTable);
// 初始化显示预约记录
displayMyReservations();
}
/**
* @brief 根据输入条件查询图书
*
* 从输入框获取ISBN、书名、作者、分类等条件,
* 调用读者的findBook方法进行查询,并显示结果。
*/
void ReaderWindow::onBookSearch()
{
QString isbn = bookISBNLineEdit->text();
QString title = bookTitleLineEdit->text();
QString author = bookAuthorLineEdit->text();
QString category = bookCategoryLineEdit->text();
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (reader)
{
std::vector<const Book *> books = reader->findBook(isbn, title, author, category);
displayBooks(books);
}
}
/**
* @brief 显示图书列表
* @param books 要显示的图书列表
*
* 将图书列表显示在图书查询表格中,包括ISBN、书名、作者、
* 分类、库存、入库时间、借阅次数、当前借出、状态和预约人数。
*/
void ReaderWindow::displayBooks(const std::vector<const Book *> &books)
{
bookSearchTable->setRowCount(0);
for (const auto *book : books)
{
int row = bookSearchTable->rowCount();
bookSearchTable->insertRow(row);
bookSearchTable->setItem(row, 0, new QTableWidgetItem(book->getISBN()));
bookSearchTable->setItem(row, 1, new QTableWidgetItem(book->getTitle()));
bookSearchTable->setItem(row, 2, new QTableWidgetItem(book->getAuthor()));
bookSearchTable->setItem(row, 3, new QTableWidgetItem(book->getCategory()));
bookSearchTable->setItem(row, 4, new QTableWidgetItem(QString::number(book->getStock())));
bookSearchTable->setItem(row, 5, new QTableWidgetItem(book->getInStockTime().toString("yyyy-MM-dd HH:mm:ss")));
bookSearchTable->setItem(row, 6, new QTableWidgetItem(QString::number(book->getBorrowCount())));
bookSearchTable->setItem(row, 7, new QTableWidgetItem(QString::number(book->getCurrentBorrowed())));
bookSearchTable->setItem(row, 8, new QTableWidgetItem(book->getStatus()));
bookSearchTable->setItem(row, 9, new QTableWidgetItem(QString::number(book->getReservationCount())));
}
}
/**
* @brief 显示我的借阅记录
*
* 获取当前读者的所有借阅记录,显示在借阅记录表格中,
* 包括ISBN、书名、借阅时间、应还时间、归还时间、状态、
* 续借状态、罚款金额、已支付罚款和罚款状态。
*/
void ReaderWindow::displayMyBorrowRecords()
{
myBorrowTable->setRowCount(0);
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (reader)
{
std::vector<BorrowRecord> records = reader->viewMyBorrowRecords();
for (const auto &record : records)
{
int row = myBorrowTable->rowCount();
myBorrowTable->insertRow(row);
myBorrowTable->setItem(row, 0, new QTableWidgetItem(record.getISBN()));
DataManager *dm = DataManager::getInstance();
Book *book = dm->findBookByISBN(record.getISBN());
QString bookTitle = book ? book->getTitle() : "未知";
myBorrowTable->setItem(row, 1, new QTableWidgetItem(bookTitle));
myBorrowTable->setItem(row, 2, new QTableWidgetItem(record.getBorrowTime().toString("yyyy-MM-dd HH:mm:ss")));
myBorrowTable->setItem(row, 3, new QTableWidgetItem(record.getDueTime().toString("yyyy-MM-dd HH:mm:ss")));
QString status = "借阅中";
if (record.calculateOverdueDays() > 0)
{
status = QString("逾期(%1天)").arg(record.calculateOverdueDays());
}
myBorrowTable->setItem(row, 4, new QTableWidgetItem(status));
QString renewStatus;
switch (record.getRenewStatus())
{
case BorrowRecord::RenewStatus::NONE:
renewStatus = "无申请";
break;
case BorrowRecord::RenewStatus::PENDING:
renewStatus = "待审核";
break;
case BorrowRecord::RenewStatus::APPROVED:
renewStatus = "已通过";
break;
case BorrowRecord::RenewStatus::REJECTED:
renewStatus = "已拒绝";
break;
default:
renewStatus = "未知";
}
myBorrowTable->setItem(row, 5, new QTableWidgetItem(renewStatus));
// 续借次数
myBorrowTable->setItem(row, 6, new QTableWidgetItem(QString::number(record.getRenewCount())));
// 罚款信息(使用读者策略的罚款标准)
double finePerDay = reader->getFinePerDay();
double fineAmount = record.calculateFine(finePerDay);
double paidFine = record.getPaidFine();
myBorrowTable->setItem(row, 7, new QTableWidgetItem(QString::number(fineAmount, 'f', 2) + "元"));
myBorrowTable->setItem(row, 8, new QTableWidgetItem(QString::number(paidFine, 'f', 2) + "元"));
}
}
}
/**
* @brief 显示我的预约记录
*
* 获取当前读者的所有预约记录,显示在预约记录表格中,
* 包括ISBN、书名、预约时间和状态。
*/
void ReaderWindow::displayMyReservations()
{
myReservationTable->setRowCount(0);
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (reader)
{
std::vector<Reservation> reservations = reader->viewMyReservations();
for (const auto &reservation : reservations)
{
int row = myReservationTable->rowCount();
myReservationTable->insertRow(row);
myReservationTable->setItem(row, 0, new QTableWidgetItem(reservation.getISBN()));
DataManager *dm = DataManager::getInstance();
Book *book = dm->findBookByISBN(reservation.getISBN());
QString bookTitle = book ? book->getTitle() : "未知";
myReservationTable->setItem(row, 1, new QTableWidgetItem(bookTitle));
myReservationTable->setItem(row, 2, new QTableWidgetItem(reservation.getReserveTime().toString("yyyy-MM-dd HH:mm:ss")));
myReservationTable->setItem(row, 3, new QTableWidgetItem(reservation.getStatusString()));
}
}
}
/**
* @brief 预约图书
*
* 弹出对话框输入ISBN,调用读者的reserveBook方法进行预约,
* 并根据返回结果显示相应的提示信息。
*/
void ReaderWindow::onBookReserve()
{
QPair<QString, bool> result = showInputDialog("预约图书", "请输入要预约的图书ISBN:");
if (result.second)
return;
QString isbn = result.first;
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (reader)
{
Reader::ReserveResult result = reader->reserveBook(isbn);
if (result == Reader::ReserveResult::SUCCESS)
{
QMessageBox::information(this, "成功", "预约成功!等待管理员审核。");
onBookSearch();
displayMyReservations();
}
else if (result == Reader::ReserveResult::BOOK_NOT_FOUND)
{
QMessageBox::warning(this, "失败", "预约失败!图书不存在。");
}
else if (result == Reader::ReserveResult::ALREADY_EXISTS)
{
QMessageBox::warning(this, "失败", "预约失败!您已预约或已借阅该图书。");
}
else if (result == Reader::ReserveResult::EXCEED_LIMIT)
{
QMessageBox::warning(this, "失败", "预约失败!该图书预约人数已达上限(预约量不超过库存的2倍)。");
}
else if (result == Reader::ReserveResult::LOW_CREDIT)
{
QMessageBox::warning(this, "失败", "您因信用分下降被限制操作,暂时无法预约。");
}
else if (result == Reader::ReserveResult::DEPOSIT_REQUIRED)
{
QMessageBox::warning(this, "失败", "预约失败!您尚未缴纳押金,请先在个人信息页面缴纳押金。");
}
}
}
/**
* @brief 取消预约
*
* 弹出对话框输入ISBN,调用读者的cancelReservation方法取消预约,
* 并根据返回结果显示相应的提示信息。
*/
void ReaderWindow::onCancelReservation()
{
QPair<QString, bool> result = showInputDialog("取消预约", "请输入要取消预约的图书ISBN:");
if (result.second)
return;
QString isbn = result.first;
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (reader)
{
bool success = reader->cancelReservation(isbn);
if (success)
{
QMessageBox::information(this, "成功", "取消预约成功!");
displayMyReservations();
onBookSearch();
}
else
{
QMessageBox::warning(this, "失败", "取消预约失败!预约记录不存在或已审核。");
}
}
}
/**
* @brief 查找预约
*
* 根据预约时间、ISBN、书名和状态等条件筛选预约记录,
* 并显示在预约记录表格中。
*/
void ReaderWindow::onSearchReservation()
{
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (!reader)
return;
QString timeFilter = reservationTimeEdit->text().trimmed();
QString isbnFilter = reservationISBNEdit->text().trimmed();
QString titleFilter = reservationTitleEdit->text().trimmed();
QString statusFilter = reservationStatusCombo->currentText();
std::vector<Reservation> allReservations = reader->viewMyReservations();
std::vector<Reservation> filteredReservations;
for (const auto &reservation : allReservations)
{
bool match = true;
if (!timeFilter.isEmpty())
{
if (!reservation.getReserveTime().toString("yyyy-MM-dd HH:mm:ss").contains(timeFilter))
match = false;
}
if (!isbnFilter.isEmpty())
{
if (!reservation.getISBN().contains(isbnFilter))
match = false;
}
if (!titleFilter.isEmpty())
{
DataManager *dm = DataManager::getInstance();
Book *book = dm->findBookByISBN(reservation.getISBN());
if (!book || !book->getTitle().contains(titleFilter))
match = false;
}
if (!statusFilter.isEmpty())
{
if (reservation.getStatusString() != statusFilter)
match = false;
}
if (match)
filteredReservations.push_back(reservation);
}
myReservationTable->setRowCount(0);
for (const auto &reservation : filteredReservations)
{
int row = myReservationTable->rowCount();
myReservationTable->insertRow(row);
myReservationTable->setItem(row, 0, new QTableWidgetItem(reservation.getISBN()));
DataManager *dm = DataManager::getInstance();
Book *book = dm->findBookByISBN(reservation.getISBN());
QString bookTitle = book ? book->getTitle() : "未知";
myReservationTable->setItem(row, 1, new QTableWidgetItem(bookTitle));
myReservationTable->setItem(row, 2, new QTableWidgetItem(reservation.getReserveTime().toString("yyyy-MM-dd HH:mm:ss")));
myReservationTable->setItem(row, 3, new QTableWidgetItem(reservation.getStatusString()));
}
myReservationTable->setColumnWidth(0, 120);
myReservationTable->setColumnWidth(1, 250);
myReservationTable->setColumnWidth(2, 150);
myReservationTable->setColumnWidth(3, 80);
}
/**
* @brief 借书
*
* 弹出对话框输入ISBN,调用读者的borrowBook方法进行借阅,
* 并根据返回结果显示相应的提示信息。
*/
void ReaderWindow::onBorrowBook()
{
QPair<QString, bool> result = showInputDialog("借书", "请输入要借阅的图书ISBN:");
if (result.second)
return;
QString isbn = result.first;
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (reader)
{
Reader::BorrowResult borrowResult = reader->borrowBook(isbn);
switch (borrowResult)
{
case Reader::BorrowResult::SUCCESS:
QMessageBox::information(this, "成功", "借书成功!");
onBookSearch();
displayMyBorrowRecords();
break;
case Reader::BorrowResult::BOOK_NOT_FOUND:
QMessageBox::warning(this, "失败", "借书失败!图书不存在。");
break;
case Reader::BorrowResult::NO_STOCK:
QMessageBox::warning(this, "失败", "借书失败!图书无库存。");
break;
case Reader::BorrowResult::HAS_OVERDUE:
QMessageBox::warning(this, "失败", "借书失败!您有逾期未还图书,请先归还。");
break;
case Reader::BorrowResult::EXCEED_LIMIT:
{
// (显示策略限制):从借阅策略获取最大借阅数量
int maxBooks = reader->getMaxBooks();
QMessageBox::warning(this, "失败", QString("借书失败!您已达到最大借阅数量(%1本)。").arg(maxBooks));
break;
}
case Reader::BorrowResult::ALREADY_BORROWED:
QMessageBox::warning(this, "失败", "借书失败!您已借阅该图书且尚未归还,不能重复借阅。");
break;
case Reader::BorrowResult::NO_VALID_RESERVATION:
QMessageBox::warning(this, "失败", "借书失败!您未预约成功该图书,请先预约并等待管理员审核。");
break;
case Reader::BorrowResult::LOW_CREDIT:
QMessageBox::warning(this, "失败", "您因信用分下降被限制操作,暂时无法借书。");
break;
case Reader::BorrowResult::DEPOSIT_REQUIRED:
QMessageBox::warning(this, "失败", "借书失败!您尚未缴纳押金,请先在个人信息页面缴纳押金。");
break;
}
}
}
/**
* @brief 还书
*
* 弹出对话框输入ISBN,调用读者的returnBook方法进行还书,
* 并根据返回结果显示相应的提示信息。
*/
void ReaderWindow::onReturnBook()
{
QPair<QString, bool> result = showInputDialog("还书", "请输入要归还的图书ISBN:");
if (result.second)
return;
QString isbn = result.first;
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (reader)
{
Reader::ReturnResult returnResult = reader->returnBook(isbn);
switch (returnResult)
{
case Reader::ReturnResult::SUCCESS:
QMessageBox::information(this, "成功", "还书成功!");
displayMyBorrowRecords();
onBookSearch();
break;
case Reader::ReturnResult::NOT_FOUND:
QMessageBox::warning(this, "失败", "还书失败!借阅记录不存在。");
break;
case Reader::ReturnResult::HAS_UNPAID_FINE:
QMessageBox::warning(this, "失败", "还书失败!该图书存在未支付罚款,请先支付罚款后再还书。");
break;
}
}
}
/**
* @brief 续借
*
* 弹出对话框输入ISBN,调用读者的renewBook方法申请续借,
* 并根据返回结果显示相应的提示信息。
*/
void ReaderWindow::onRenewBook()
{
QPair<QString, bool> result = showInputDialog("续借", "请输入要续借的图书ISBN:");
if (result.second)
return;
QString isbn = result.first;
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (reader)
{
Reader::RenewResult renewResult = reader->renewBook(isbn);
switch (renewResult)
{
case Reader::RenewResult::SUCCESS:
QMessageBox::information(this, "成功", "续借申请已提交,请等待管理员审核!");
displayMyBorrowRecords();
break;
case Reader::RenewResult::NOT_FOUND:
QMessageBox::warning(this, "失败", "续借失败!借阅记录不存在。");
break;
case Reader::RenewResult::ALREADY_PENDING:
QMessageBox::warning(this, "失败", "续借失败!已有待审核的续借申请。");
break;
case Reader::RenewResult::HAS_OVERDUE:
QMessageBox::warning(this, "失败", "续借失败!该图书已逾期,不能续借。");
break;
case Reader::RenewResult::EXCEED_LIMIT:
{
// (显示续借限制):续借后借期不得超过策略规定的最大天数
int maxRenewTimes = reader->getMaxRenewTimes();
QMessageBox::warning(this, "失败", QString("续借失败!续借次数已达上限(最多续借%1次)。").arg(maxRenewTimes));
break;
}
case Reader::RenewResult::LOW_CREDIT:
QMessageBox::warning(this, "失败", "您因信用分下降被限制操作,暂时无法续借。");
break;
}
}
}
/**
* @brief 查找借阅记录
*
* 根据ISBN、书名、借阅时间、应还时间、归还时间、状态、
* 续借状态和罚款状态等条件筛选借阅记录,并显示在表格中。
*/
void ReaderWindow::onSearchBorrow()
{
QString isbn = borrowISBNEdit->text();
QString title = borrowTitleEdit->text();
QString time = borrowTimeEdit->text();
QString dueTime = borrowDueTimeEdit->text();
QString returnTime = borrowReturnTimeEdit->text();
QString status = borrowStatusEdit->text();
QString fineStatus = borrowFineCombo->currentText();
QString renewStatus = borrowRenewCombo->currentText();
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (!reader)
return;
double finePerDay = reader->getFinePerDay(); // 获取读者策略的罚款标准
DataManager *dm = DataManager::getInstance();
std::vector<BorrowRecord> allRecords = dm->getBorrowRecords();
std::vector<BorrowRecord> filteredRecords;
for (const auto &record : allRecords)
{
if (record.getReaderID() != reader->getID())
continue;
bool match = true;
if (!isbn.isEmpty() && !record.getISBN().contains(isbn))
match = false;
if (!title.isEmpty())
{
Book *book = dm->findBookByISBN(record.getISBN());
if (!book || !book->getTitle().contains(title))
match = false;
}
if (!time.isEmpty() && !record.getBorrowTime().toString("yyyy-MM-dd").contains(time))
match = false;
if (!dueTime.isEmpty() && !record.getDueTime().toString("yyyy-MM-dd").contains(dueTime))
match = false;
// 归还记录会被删除,所以不需要搜索归还时间
if (!status.isEmpty())
{
QString recordStatus;
if (record.calculateOverdueDays() > 0)
recordStatus = QString("逾期%1天").arg(record.calculateOverdueDays());
else
recordStatus = "借阅中";
if (!recordStatus.contains(status))
match = false;
}
if (!fineStatus.isEmpty())
{
BorrowRecord::FineStatus fs = record.getFineStatus(finePerDay);
if (fineStatus == "未支付" && fs != BorrowRecord::FineStatus::UNPAID)
match = false;
if (fineStatus == "已支付" && fs != BorrowRecord::FineStatus::PAID)
match = false;
}
if (!renewStatus.isEmpty())
{
BorrowRecord::RenewStatus rs = record.getRenewStatus();
if (renewStatus == "无申请" && rs != BorrowRecord::RenewStatus::NONE)
match = false;
if (renewStatus == "待审核" && rs != BorrowRecord::RenewStatus::PENDING)
match = false;
if (renewStatus == "已通过" && rs != BorrowRecord::RenewStatus::APPROVED)
match = false;
if (renewStatus == "已拒绝" && rs != BorrowRecord::RenewStatus::REJECTED)
match = false;
}
if (match)
filteredRecords.push_back(record);
}
myBorrowTable->setRowCount(0);
for (const auto &record : filteredRecords)
{
Book *book = dm->findBookByISBN(record.getISBN());
int row = myBorrowTable->rowCount();
myBorrowTable->insertRow(row);
myBorrowTable->setItem(row, 0, new QTableWidgetItem(record.getISBN()));
myBorrowTable->setItem(row, 1, new QTableWidgetItem(book ? book->getTitle() : "未知"));
myBorrowTable->setItem(row, 2, new QTableWidgetItem(record.getBorrowTime().toString("yyyy-MM-dd hh:mm:ss")));
myBorrowTable->setItem(row, 3, new QTableWidgetItem(record.getDueTime().toString("yyyy-MM-dd hh:mm:ss")));
QString statusText;
if (record.calculateOverdueDays() > 0)
statusText = "逾期";
else
statusText = "正常";
myBorrowTable->setItem(row, 4, new QTableWidgetItem(statusText));
QString renewStatusText;
switch (record.getRenewStatus())
{
case BorrowRecord::RenewStatus::NONE:
renewStatusText = "无申请";
break;
case BorrowRecord::RenewStatus::PENDING:
renewStatusText = "待审核";
break;
case BorrowRecord::RenewStatus::APPROVED:
renewStatusText = "已通过";
break;
case BorrowRecord::RenewStatus::REJECTED:
renewStatusText = "已拒绝";
break;
default:
renewStatusText = "未知";
}
myBorrowTable->setItem(row, 5, new QTableWidgetItem(renewStatusText));
// 续借次数
myBorrowTable->setItem(row, 6, new QTableWidgetItem(QString::number(record.getRenewCount())));
double fineAmount = record.calculateFine(finePerDay);
double paidFine = record.getPaidFine();
myBorrowTable->setItem(row, 7, new QTableWidgetItem(QString::number(fineAmount, 'f', 2) + "元"));
myBorrowTable->setItem(row, 8, new QTableWidgetItem(QString::number(paidFine, 'f', 2) + "元"));
}
}
/**
* @brief 支付所有罚款
*
* 计算当前读者的所有未支付罚款总额,调用读者的payAllFines方法进行支付,
* 支付成功后发送消息通知读者,并更新借阅记录显示。
*/
void ReaderWindow::onPayAllFines()
{
Reader *reader = dynamic_cast<Reader *>(currentUser);
if (!reader)
return;
double finePerDay = reader->getFinePerDay(); // 获取读者策略的罚款标准
DataManager *dm = DataManager::getInstance();
std::vector<BorrowRecord> &allRecords = dm->getBorrowRecords();
double totalFine = 0;
for (auto &record : allRecords)
{
if (record.getReaderID() == reader->getID())
{
totalFine += record.calculateFine(finePerDay) - record.getPaidFine();
}
}
if (totalFine <= 0)
{
QMessageBox::information(this, "提示", "您当前没有需要支付的罚款。");
return;
}
QMessageBox::StandardButton reply = QMessageBox::question(this, "确认支付",
QString("您当前需要支付罚款总额:%1元,是否确认支付?").arg(totalFine),
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes)
{
for (auto &record : allRecords)
{
if (record.getReaderID() == reader->getID())
{
double remaining = record.calculateFine(finePerDay) - record.getPaidFine();
if (remaining > 0)
{
record.setPaidFine(record.calculateFine(finePerDay));
record.setFineStatus(BorrowRecord::FineStatus::PAID);
}
}
}
dm->writeBorrowRecord();
// 发送消息给自己
Message msg(reader->getID(), reader->getName(), QString("已成功支付所有罚款,共计:%1元").arg(totalFine));
reader->addMessage(msg);
dm->writeMessage();
QMessageBox::information(this, "成功", QString("成功支付罚款:%1元").arg(totalFine));
displayMyBorrowRecords();
}
}
/**