-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.cpp
More file actions
1704 lines (1606 loc) · 64.3 KB
/
Copy pathdatabase.cpp
File metadata and controls
1704 lines (1606 loc) · 64.3 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
#include "database.h"
#include<QSqlError>
#include<mainwindow.h>
#include<QString>
#include<QVariantList>
#include<QDebug>
#include <QDateTime>
#include <QTimer>
#include <QDesktopWidget>
#include<QFileDialog>
database::database(QObject *parent) : QObject(parent){ }
void database::run(){
SetWindowLayout();
if(!connectdb("demo")){
QMessageBox::critical(window,"Error","连接数据库失败,请检查数据库连接!",QMessageBox::Yes);
}
}
database::~database(){ }
void database::SetWindowLayout(){
//添加计时器 3-26
timer = new QTimer();
connect(timer,&QTimer::timeout,this,&database::currentTime);
timer->start(1000);
SetMainWindow();
SetCoreWindow();
}
void database::SetMainWindow(){
window=new QWidget();
window->setWindowTitle(tr("BOOK MANAGER"));
registerButton=new QPushButton(tr("注册"));
loginUserButton=new QPushButton(tr("用户登录"));
loginManagerButton=new QPushButton(tr("管理员登录"));
registerButton->setFixedSize(QSize(60,30));//如果不设置在总窗口变化大小时会跟着变化
loginUserButton->setFixedSize(QSize(90,30));
loginManagerButton->setFixedSize(QSize(100,30));
hboxLayout01=new QHBoxLayout();
hboxLayout00=new QHBoxLayout();
hboxLayout02=new QHBoxLayout();
vboxLayout00=new QVBoxLayout();
vboxLayout01=new QVBoxLayout();
spaceLabel00=new QLabel();
spaceLabel01=new QLabel("\n\n");
spaceLabel02=new QLabel();
titleBar=new QWidget();
searchWindowTab=new QTabWidget();
logoutButton = NULL;
currentUser = VISITOR;
spaceLabel00->setText("您好!登录状态:未登录");
hboxLayout00->addWidget(spaceLabel00);//左边加了一小条空白 ->改成了登录状态 3-26
hboxLayout00->addStretch();//加这句更新后界面不会乱掉 3-26
hboxLayout00->addWidget(loginUserButton);
hboxLayout00->addWidget(loginManagerButton);
hboxLayout00->addWidget(registerButton);
hboxLayout02->addStretch();
hboxLayout02->addWidget(spaceLabel02);//时间显示 3-26
vboxLayout00->addWidget(spaceLabel01);
vboxLayout00->addLayout(hboxLayout00);
vboxLayout00->addLayout(hboxLayout02);
hboxLayout01->addLayout(vboxLayout00);
titleBar->setLayout(hboxLayout01);
vboxLayout01->addWidget(titleBar);
vboxLayout01->addWidget(searchWindowTab);
window->setLayout(vboxLayout01);
window->setFixedSize(1024*1.2,768*1.2);//设置总窗口的大小
window->show();
connect(registerButton,&QPushButton::clicked,this,&database::showRegisterWindow);
connect(loginManagerButton,&QPushButton::clicked,this,&database::showLoginManagerWindow);
connect(loginUserButton,&QPushButton::clicked,this,&database::showLoginUserWindow);
}
void database::SetCoreWindow(){
const int bookTypeNumber=22;
const int bookSearchNumber=8;
//书号, 类别, 书名, 出版社, 年份, 作者, 价格(区间)0-2000, 总藏书量(区间)0-50, 库存(区间)0-5000
const int maxPrice=2000;
// const int maxBookNumber=50;
// const int maxStock=5000;
const int bookMessageNumber=9;
//书号, 类别, 书名, 出版社, 年份, 作者, 价格, 总藏书量, 库存
// const int rowsEachPage=10;
searchBookWindow=new QWidget();
hboxLayout30=new QHBoxLayout();
hboxLayout31=new QHBoxLayout();
hboxLayout32=new QHBoxLayout();
hboxLayout33=new QHBoxLayout();
hboxLayout34=new QHBoxLayout();
hboxLayout35=new QHBoxLayout();
vboxLayout30=new QVBoxLayout();
vboxLayout31=new QVBoxLayout();
//vboxLayout32=new QVBoxLayout();
gridLayout30=new QGridLayout();
gridLayout31=new QGridLayout();
groupBox30=new QGroupBox();
groupBox31=new QGroupBox();
groupBox32=new QGroupBox();
groupBox33=new QGroupBox();
bookId=new QLineEdit();
bookName=new QLineEdit();
yearLow=new QComboBox();//addItem添加东西
yearHigh=new QComboBox();
press=new QLineEdit();
author=new QLineEdit();
priceLow=new QComboBox();
priceHigh=new QComboBox();
bookType=new QComboBox();
//排序
firstSequence=new QComboBox();
secondSequence=new QComboBox();
/* totalNumLow=new QComboBox();
totalNumHigh=new QComboBox();
stockLow=new QComboBox();
stockHigh=new QComboBox();*/
//显示书籍信息表格
bookTable = new QTableWidget();
//翻页
lastPageButton = new QPushButton(tr("上一页"));
nextPageButton = new QPushButton(tr("下一页"));
searchButton30=new QPushButton(tr("搜索"));
clearButton30=new QPushButton(tr("清空"));
sortButton=new QPushButton(tr("排序"));
borrowBookButton=new QPushButton(tr("借书"));
//searchButton30->setFixedSize(120,35);
//clearButton30->setFixedSize(120,35);
label30=new QLabel[bookTypeNumber];
// label31=new QLabel[bookMessageNumber];
label32=new QLabel[bookSearchNumber];
// label33=new QLabel[rowsEachPage];
label34=new QLabel[2];
currentPage = maxPage = 0;
borrowFlag = false;
//其他设置
QString bookMessage30[bookTypeNumber]={"马列毛邓","哲学、宗教","社会科学总论","政治、法律","军事","经济",
"教科文体","语言、文字","文学","艺术","历史、地理","自然科学总论","数理科学",
"天文学、地球科学","生物科学","医药、卫生","农业科学","工业技术","交通运输","航空、航天",
"环境、安全科学","综合性图书"};
QString bookMessage31[bookSearchNumber]={"书名","作者","年份","--","出版社","类别","价格","--"};
QString bookMessage32[bookMessageNumber]={"书号","类别","书名","出版社","年份","作者","价格","总藏书量","库存"};
// QString bookMessage33[rowsEachPage]={"1","2","3","4","5","6","7","8","9","10"};
yearLow->addItem(" ");
yearHigh->addItem(" ");
for(int i=2017;i>=1949;i--){
yearLow->addItem(QString::number(i));
yearHigh->addItem(QString::number(i));
}
priceLow->addItem(" ");
priceHigh->addItem(" ");
for(int i=0;i<=maxPrice;i++){
priceLow->addItem(QString::number(i));
priceHigh->addItem(QString::number(i));
}
bookType->addItem(" ");
for(int i=0;i<bookTypeNumber;i++){
bookType->addItem(bookMessage30[i]);
}
firstSequence->addItem(bookMessage32[0]);
secondSequence->addItem(" ");
for(int i=1; i<bookMessageNumber;i++){
firstSequence->addItem(bookMessage32[i]);
secondSequence->addItem(bookMessage32[i]);
}
/* totalNumLow->addItem(" ");
totalNumHigh->addItem(" ");
for(int i=0;i<=maxBookNumber;i++){
totalNumLow->addItem(QString::number(i));
totalNumHigh->addItem(QString::number(i));
}
stockLow->addItem(" ");
stockHigh->addItem(" ");
for(int i=0;i<=maxStock;i++){
stockLow->addItem(QString::number(i));
stockHigh->addItem(QString::number(i));
}*/
//书籍信息显示
showBookTable(VISITOR);
//分配关系
/* vboxLayout30->addStretch();
for(int i=0;i<bookTypeNumber;i++){
label30[i].setText(bookMessage30[i]);
vboxLayout30->addWidget(label30+i);
}
vboxLayout30->addStretch();*/
/*for(int i=0;i<bookMessageNumber;i++){
label31[i].setText(bookMessage32[i]);
gridLayout30->addWidget(label31+i,1,i+2);
}
for(int i=0;i<rowsEachPage;i++){
label33[i].setText(bookMessage33[i]);
gridLayout30->addWidget(label33+i,i+2,1);
}*/
gridLayout30->addWidget(bookTable);
for(int i=0;i<bookSearchNumber;i++){
label32[i].setText(bookMessage31[i]);
gridLayout31->addWidget(label32+i,i/4+1,(i%4)*2+1);
}
label34[0].setText("首排序:");
label34[1].setText("次排序:");
// gridLayout31->addWidget(bookId,1,2);
gridLayout31->addWidget(bookName,1,2);
gridLayout31->addWidget(author,1,4);
gridLayout31->addWidget(yearLow,1,6);
gridLayout31->addWidget(yearHigh,1,8);
gridLayout31->addWidget(press,2,2);
gridLayout31->addWidget(bookType,2,4);
gridLayout31->addWidget(priceLow,2,6);
gridLayout31->addWidget(priceHigh,2,8);
/* gridLayout31->addWidget(totalNumLow,3,2);
gridLayout31->addWidget(totalNumHigh,3,4);
gridLayout31->addWidget(stockLow,3,6);
gridLayout31->addWidget(stockHigh,3,8);*/
//布局
hboxLayout35->addStretch();
hboxLayout35->addWidget(searchButton30);
hboxLayout35->addWidget(clearButton30);
hboxLayout35->addWidget(borrowBookButton);
hboxLayout35->addStretch();
hboxLayout33->addStretch();
hboxLayout33->addLayout(hboxLayout35);
hboxLayout33->addStretch();
groupBox30->setLayout(hboxLayout33);
groupBox31->setLayout(gridLayout31);
groupBox30->setFixedSize(300,120);
groupBox31->setFixedSize(600,120);
hboxLayout31->addWidget(groupBox31);
hboxLayout31->addSpacing(20);
hboxLayout31->addWidget(groupBox30);
groupBox32->setLayout(gridLayout30);
groupBox32->setFixedSize(1000,500);
vboxLayout31->addLayout(hboxLayout31);
vboxLayout31->addWidget(groupBox32);
hboxLayout34->addWidget(label34);
hboxLayout34->addWidget(firstSequence);
hboxLayout34->addWidget(label34+1);
hboxLayout34->addWidget(secondSequence);
hboxLayout34->addWidget(sortButton);
hboxLayout34->addStretch();
// hboxLayout34->addWidget(lastPageButton);
// hboxLayout34->addWidget(nextPageButton);
vboxLayout31->addLayout(hboxLayout34);
hboxLayout30->addStretch();
hboxLayout30->addLayout(vboxLayout30);
hboxLayout30->addLayout(vboxLayout31);
hboxLayout30->addStretch();
searchBookWindow->setLayout(hboxLayout30);
searchWindowTab->addTab(searchBookWindow,"图书搜索");
connect(searchButton30,&QPushButton::clicked,this,&database::searchBook);
connect(sortButton,&QPushButton::clicked,this,&database::searchBook);
connect(clearButton30,&QPushButton::clicked,this,&database::clearInput);
connect(lastPageButton,&QPushButton::clicked,this,&database::lastPage);
connect(nextPageButton,&QPushButton::clicked,this,&database::nextPage);
connect(borrowBookButton,&QPushButton::clicked,this,&database::borrowBook);
managerWindow=new QWidget();
batchProcessButton50=new QPushButton(tr("打开文件"));
addOneBookConfirmButton50 =new QPushButton(tr("确认添加"));
searchUserButton50=new QPushButton(tr("搜索"));
label50=new QLabel(tr("单册添加书籍"));
label51=new QLabel[9];
label52=new QLabel(tr("批量导入书籍"));
label51[0].setText(tr("书号"));
label51[1].setText(tr("类型"));
label51[2].setText(tr("书名"));
label51[3].setText(tr("出版社"));
label51[4].setText(tr("时间"));
label51[5].setText(tr("作者"));
label51[6].setText(tr("价格"));
label51[7].setText(tr("总量"));
label51[8].setText(tr("库存"));
label53=new QLabel(tr("用户管理"));
label54=new QLabel(tr("用户名"));
authorizationButton50=new QPushButton(tr("授权"));
revokeButton50=new QPushButton(tr("撤回"));
changeButton50=new QPushButton(tr("对立更改"));
deleteUserButton50=new QPushButton(tr("删除用户"));
bookId50=new QLineEdit();
bookType50=new QComboBox();
bookName50=new QLineEdit();
press50=new QLineEdit();
year50=new QLineEdit();
author50=new QLineEdit();
price50=new QLineEdit();
total50=new QLineEdit();
stock50=new QLineEdit();
userName50=new QLineEdit();
hboxLayout50=new QHBoxLayout();
hboxLayout51=new QHBoxLayout();
hboxLayout52=new QHBoxLayout();
hboxLayout53=new QHBoxLayout();
hboxLayout54=new QHBoxLayout();
hboxLayout55=new QHBoxLayout();
hboxLayout56=new QHBoxLayout();
hboxLayout57=new QHBoxLayout();
hboxLayout58=new QHBoxLayout();
vboxLayout50=new QVBoxLayout();
vboxLayout51=new QVBoxLayout();
vboxLayout52=new QVBoxLayout();
vboxLayout53=new QVBoxLayout();
gridLayout50=new QGridLayout();
groupBox50=new QGroupBox();
groupBox51=new QGroupBox();
groupBox52=new QGroupBox();
userTable=new QTableWidget();
bookType50->addItem(" ");
for(int i=0;i<bookTypeNumber;i++){
bookType50->addItem(bookMessage30[i]);
}
showUserTable(VISITOR);
gridLayout50->addWidget(&label51[0],1,1);
gridLayout50->addWidget(bookId50,1,2);
gridLayout50->addWidget(&label51[1],1,3);
gridLayout50->addWidget(bookType50,1,4);
gridLayout50->addWidget(&label51[2],1,5);
gridLayout50->addWidget(bookName50,1,6);
gridLayout50->addWidget(&label51[3],2,1);
gridLayout50->addWidget(press50,2,2);
gridLayout50->addWidget(&label51[4],2,3);
gridLayout50->addWidget(year50,2,4);
gridLayout50->addWidget(&label51[5],2,5);
gridLayout50->addWidget(author50,2,6);
gridLayout50->addWidget(&label51[6],3,1);
gridLayout50->addWidget(price50,3,2);
gridLayout50->addWidget(&label51[7],3,3);
gridLayout50->addWidget(total50,3,4);
gridLayout50->addWidget(&label51[8],3,5);
gridLayout50->addWidget(stock50,3,6);
hboxLayout50->addStretch();
hboxLayout50->addWidget(label50);
hboxLayout50->addStretch();
hboxLayout51->addStretch();
hboxLayout51->addWidget(addOneBookConfirmButton50);
hboxLayout51->addStretch();
hboxLayout52->addStretch();
hboxLayout52->addWidget(batchProcessButton50);
hboxLayout52->addStretch();
hboxLayout54->addStretch();
hboxLayout54->addWidget(label52);
hboxLayout54->addStretch();
hboxLayout55->addStretch();
hboxLayout55->addWidget(label54);
hboxLayout55->addWidget(userName50);
hboxLayout55->addWidget(searchUserButton50);
hboxLayout55->addStretch();
hboxLayout56->addStretch();
hboxLayout56->addWidget(label53);
hboxLayout56->addStretch();
hboxLayout58->addStretch();
hboxLayout58->addWidget(authorizationButton50);
hboxLayout58->addWidget(revokeButton50);
hboxLayout58->addWidget(changeButton50);
hboxLayout58->addWidget(deleteUserButton50);
hboxLayout58->addStretch();
hboxLayout57->addStretch();
hboxLayout57->addWidget(userTable);
hboxLayout57->addStretch();
vboxLayout51->addLayout(hboxLayout56);
vboxLayout51->addLayout(hboxLayout55);
vboxLayout51->addLayout(hboxLayout57);
vboxLayout51->addLayout(hboxLayout58);
groupBox50->setLayout(vboxLayout51);
vboxLayout52->addLayout(hboxLayout50);
vboxLayout52->addLayout(gridLayout50);
vboxLayout52->addLayout(hboxLayout51);
groupBox51->setLayout(vboxLayout52);
vboxLayout53->addLayout(hboxLayout54);
vboxLayout53->addLayout(hboxLayout52);
groupBox52->setLayout(vboxLayout53);
vboxLayout50->addStretch();
vboxLayout50->addWidget(groupBox51);
vboxLayout50->addWidget(groupBox52);
vboxLayout50->addWidget(groupBox50);
vboxLayout50->addStretch();
hboxLayout53->addStretch();
hboxLayout53->addLayout(vboxLayout50);
hboxLayout53->addStretch();
managerWindow->setLayout(hboxLayout53);
searchWindowTab->addTab(managerWindow,"管理员界面");
for(int i=0;i<10;i++){
item50[i]=new QTableWidgetItem();
}
for(int i=0;i<10;i++){
item70[i]=new QTableWidgetItem();
}
connect(batchProcessButton50,&QPushButton::clicked,this,&database::batchProcess);
connect(addOneBookConfirmButton50,&QPushButton::clicked,this,&database::addOneBook);
connect(searchUserButton50,&QPushButton::clicked,this,&database::searchUser);
connect(authorizationButton50,&QPushButton::clicked,this,&database::giveAuthorization);
connect(revokeButton50,&QPushButton::clicked,this,&database::revokeAuthorization);
connect(changeButton50,&QPushButton::clicked,this,&database::changeAuthorization);
connect(deleteUserButton50,&QPushButton::clicked,this,&database::deleteUser);
//users' Home
userHomeWindow= new QWidget();
userBorrowTable=new QTableWidget();
changePasswordAffirmButton=new QPushButton(tr("确定"));
returnBookButton=new QPushButton(tr("还书"));
hboxLayout70=new QHBoxLayout();
hboxLayout71=new QHBoxLayout();
hboxLayout72=new QHBoxLayout();
hboxLayout73=new QHBoxLayout();
vboxLayout72=new QVBoxLayout();
vboxLayout71=new QVBoxLayout();
vboxLayout70=new QVBoxLayout();
gridLayout70=new QGridLayout();
groupBox70=new QGroupBox();
groupBox71=new QGroupBox();
label70=new QLabel[3];
label71=new QLabel();
oldPassword70=new QLineEdit();
newPassword70=new QLineEdit();
newPassword71=new QLineEdit();
oldPassword70->setEchoMode(QLineEdit::Password);
newPassword70->setEchoMode(QLineEdit::Password);
newPassword71->setEchoMode(QLineEdit::Password);
label70[0].setText(tr("原密码"));
label70[1].setText(tr("新密码"));
label70[2].setText(tr("再次输入"));
label71->setText(tr("更改密码"));
hboxLayout71->addStretch();
hboxLayout71->addWidget(label71);
hboxLayout71->addStretch();
gridLayout70->addWidget(&label70[0],1,1);
gridLayout70->addWidget(&label70[1],2,1);
gridLayout70->addWidget(&label70[2],3,1);
gridLayout70->addWidget(oldPassword70,1,2);
gridLayout70->addWidget(newPassword70,2,2);
gridLayout70->addWidget(newPassword71,3,2);
hboxLayout72->addStretch();
hboxLayout72->addLayout(gridLayout70);
hboxLayout72->addSpacing(20);
hboxLayout72->addWidget(changePasswordAffirmButton);
hboxLayout72->addStretch();
hboxLayout73->addStretch();
hboxLayout73->addWidget(returnBookButton);
hboxLayout73->addStretch();
vboxLayout71->addStretch();
vboxLayout71->addLayout(hboxLayout71);
vboxLayout71->addLayout(hboxLayout72);
vboxLayout71->addStretch();
groupBox70->setLayout(vboxLayout71);
vboxLayout72->addWidget(userBorrowTable);
vboxLayout72->addLayout(hboxLayout73);
groupBox71->setLayout(vboxLayout72);
vboxLayout70->addStretch();
vboxLayout70->addWidget(groupBox70);
vboxLayout70->addWidget(groupBox71);
vboxLayout70->addStretch();
hboxLayout70->addStretch();
hboxLayout70->addLayout(vboxLayout70);
hboxLayout70->addStretch();
userHomeWindow->setLayout(hboxLayout70);
searchWindowTab->addTab(userHomeWindow,"个人中心");
searchWindowTab->removeTab(0);
searchWindowTab->removeTab(0);
searchWindowTab->removeTab(0);
searchWindowTab->insertTab(0,searchBookWindow,"图书搜索");
connect(changePasswordAffirmButton,&QPushButton::clicked,this,&database::changePassword);
connect(returnBookButton,&QPushButton::clicked,this,&database::returnBook);
}
void database::showBorrowTable(){
QStringList header;
//清除表格信息
userBorrowTable->clear();
//设置表格为不可选择、不可修改
userBorrowTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
userBorrowTable->setSelectionMode(QAbstractItemView::NoSelection);
userBorrowTable->setFixedSize(725,300);
userBorrowTable->setRowCount(10);
userBorrowTable->setColumnCount(5);
header<<" "<<"书名"<<"作者"<<"借书时间"<<"应还时间";
userBorrowTable->setHorizontalHeaderLabels(header);
for(int i=0;i<5;i++){
if(i == 0) userBorrowTable->setColumnWidth(i,25);
else if(i==1 || i==2) userBorrowTable->setColumnWidth(i,110);
else userBorrowTable->setColumnWidth(i,220);
}
//查询
QSqlQuery *borrowRecordQuery;
borrowRecordQuery=new QSqlQuery();
//qDebug()<<nameLine40->text();
if(!borrowRecordQuery->exec("select title, author, borrowDate, returnDate, bookID from books natural join borrow where username = '"+nameLine40->text()+"' and returnDate is null")){
QMessageBox::critical(window,"Error",borrowRecordQuery->lastError().text());
return;
}
else{
if(!borrowRecordQuery->next()){
item71[0][0] = new QTableWidgetItem("暂无记录");
item71[0][0]->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
userBorrowTable->setItem(0,3,item71[0][0]);
return;
}
}
//显示书籍信息
borrowRecordQuery->seek(0);
for(int i=0;i<10;i++){
item70[i]=new QTableWidgetItem();
}
int count = 0;
do{
int i;
item70[count]->setCheckState(Qt::Unchecked);
userBorrowTable->setItem(count,0,item70[count]);
for(i=0; i<3; i++){
item71[count][i] = new QTableWidgetItem(borrowRecordQuery->value(i).toString());
// qDebug()<<item71[count][i]->text();
item71[count][i]->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
userBorrowTable->setItem(count,i+1,item71[count][i]);
}
QDateTime returnTime;
returnTime = QDateTime::fromString(borrowRecordQuery->value(2).toString(),"yyyy-MM-dd hh:mm:ss").addDays(60);
item71[count][3] = new QTableWidgetItem(returnTime.toString("yyyy-MM-dd hh:mm:ss"));
item71[count][3]->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
userBorrowTable->setItem(count,4,item71[count][3]);
item71[count][4] = new QTableWidgetItem(borrowRecordQuery->value(4).toString());
count++;
if(count == 10) {
break;
}
}while(borrowRecordQuery->next());
}
void database::changePassword(){
//passwordLine40
// if(currentUser == VISITOR) {
// QMessageBox::critical(window,"提示","请先登录再修改!");
// return;
// }
// if(currentUser == MANAGER){
// QMessageBox::critical(window,"提示","管理员不能修改密码!");
// return;
// }
QString rightPassword;
QSqlQuery tmpQuery;
if(!tmpQuery.exec("select password from users where username='" + nameLine40->text() +"'")){
QMessageBox::critical(userHomeWindow,"Error",tmpQuery.lastError().text());
return;
}
if(!tmpQuery.next()){
QMessageBox::critical(userHomeWindow,"Error","该用户名不存在",QMessageBox::Yes);
return;
}
rightPassword=tmpQuery.value(0).toString();
QVariantList nameList70,passwordList70;
if(oldPassword70->text().compare(rightPassword)!=0){
QMessageBox::critical(userHomeWindow,"Error","密码错误");
return;
}
if(newPassword70->text().compare(newPassword71->text())!=0){
QMessageBox::critical(userHomeWindow,"Error","两次密码输入不一致!");
return;
}
tmpQuery.prepare("update users set password = ? where username = ?");
nameList70<<nameLine40->text();
passwordList70<<newPassword70->text();
tmpQuery.addBindValue(passwordList70);
tmpQuery.addBindValue(nameList70);
if(!tmpQuery.execBatch()){
QMessageBox::critical(userHomeWindow,"Error",tmpQuery.lastError().text());
return;
}else{
QMessageBox::about(userHomeWindow,"database","修改成功,请重新登陆");
oldPassword70->clear();
newPassword70->clear();
newPassword71->clear();
logout();
}
}
void database::returnBook(){
//nameLine40
const int max=10;
int flag=0;
// QString name="";
QSqlQuery tmpQuery;
QVariantList usernameList,borrowTimeList,returnTimeList,bookIDList;
tmpQuery.prepare("update borrow set returnDate = ? where borrowDate = ? and username = ? and bookID = ?");
for(int i=0;i<max;i++){
if(item70[i]->checkState() == Qt::Checked){
borrowTimeList<<item71[i][2]->text();
returnTimeList<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
bookIDList<<item71[i][4]->text();
usernameList<<nameLine40->text();
flag=1;
}
}
if(!flag){
QMessageBox::critical(window,"error","您尚未选择任何书籍!");
return;
}
tmpQuery.addBindValue(returnTimeList);
tmpQuery.addBindValue(borrowTimeList);
tmpQuery.addBindValue(usernameList);
tmpQuery.addBindValue(bookIDList);
if(!tmpQuery.execBatch()){
QMessageBox::critical(userHomeWindow,"Error",tmpQuery.lastError().text());
return;
}
QMessageBox::about(window,"database","success");
tmpQuery.prepare("update books set stock = stock +1 where bookID = ?");
tmpQuery.addBindValue(bookIDList);
if(!tmpQuery.execBatch()){
QMessageBox::critical(userHomeWindow,"Error",tmpQuery.lastError().text());
return;
//没有回滚过程
}
tmpQuery.finish();
// showUserTable(USER);
showBorrowTable();
showBookTable(VISITOR);
return;
}
void database::deleteUser(){
const int max=10;
int flag=0;
QString name="";
QSqlQuery tmpQuery;
for(int i=0;i<max;i++){
if(item50[i]->checkState() == Qt::Checked){
if(flag==0){
name+=" username = ";
}else{
name+=" or username = ";
}
name+="'"+item51[i][0]->text()+"'";
flag = 1;
}
}
if(!flag){
QMessageBox::critical(window,"error","您尚未选择任何用户!");
return;
}
if(!tmpQuery.exec("delete from users where "+name)){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
return;
}
QMessageBox::about(window,"database","success");
tmpQuery.finish();
showUserTable(USER);
return;
}
void database::borrowBook(){
if(currentUser == VISITOR) {
QMessageBox::critical(window,"提示","请先登录再借书!");
return;
}
if(currentUser == MANAGER){
QMessageBox::critical(window,"提示","管理员不能借书!");
return;
}
if(!borrowFlag){
QMessageBox::critical(window,"提示","您尚未选择任何书籍!");
return;
}
const int max=50;
int flag=0,count=0;
QString queryText="";
QSqlQuery tmpQuery;
QSqlQuery returnDateQuery;
QString bookID[max];
QVariantList bookIDList;
QString earlistTime;
for(int i=0;i<max;i++){
if(item30[i]->checkState() == Qt::Checked){
if(!item31[i][9]->text().compare("unavailable")){
returnDateQuery.prepare("select min(borrowDate) from borrow where bookID = ?");
bookIDList<<item31[i][0]->text();
returnDateQuery.addBindValue(bookIDList);
if(!returnDateQuery.execBatch()){
QMessageBox::critical(window,"Error",returnDateQuery.lastError().text());
return;
}else{
returnDateQuery.next();
QDateTime earlistReturnTime;
if(returnDateQuery.value(0).toString().compare("null")){
QMessageBox::critical(window,"提示",item31[i][2]->text() + "已无库存!");
return;
}
earlistReturnTime=QDateTime::fromString(returnDateQuery.value(0).toString(),"yyyy-MM-dd hh:mm:ss").addDays(60);
earlistTime=earlistReturnTime.toString("yyyy-MM-dd hh:mm:ss");
}
QMessageBox::critical(window,"提示",item31[i][2]->text() + "暂无库存!\n建议您"+ earlistTime +"再借!");
return;
}
if(flag==0){
queryText += " bookID = ";
}else{
queryText += " or bookID = ";
}
queryText +="'"+item31[i][0]->text()+"'";
bookID[count++] = item31[i][0]->text();
flag = 1;
}
}
if(!flag){
QMessageBox::critical(window,"Error","您尚未选择任何书籍!");
return;
}
int notreturnBookNumber;
QVariantList username;
username<<nameLine40->text();
tmpQuery.prepare("select count(*) from borrow where username = ? and returnDate is null");
tmpQuery.addBindValue(username);
if(!tmpQuery.execBatch()){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
return;
}else{
tmpQuery.next();
notreturnBookNumber=tmpQuery.value(0).toInt();
if(notreturnBookNumber + count > 10){
QMessageBox::critical(window,"Error","一个人不能借多于十本书!");
return;
}
}
tmpQuery.prepare("select isLent from users where username = ?");
tmpQuery.addBindValue(username);
// qDebug()<<username;
if(!tmpQuery.execBatch()){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
return;
}else{
tmpQuery.next();
// qDebug()<<tmpQuery.value(0).toString();
if(tmpQuery.value(0).toString().compare("false")==0){
QMessageBox::critical(window,"Error","您没有借书权限,请向管理员申请!");
return;
}
}
if(!tmpQuery.exec("update books set stock = stock - 1 where "+queryText)){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
return;
}
// if(!tmpQuery.exec("update books set total = total - 1 where "+queryText)){
// QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
// tmpQuery.exec("update books set stock = stock + 1 where "+queryText);
// return;
// }
QDateTime dateTime = QDateTime::currentDateTime();//获取系统时间
for(int i=0; i<count; i++){
if(!tmpQuery.exec("insert into borrow values('" + nameLine40->text() + "','" + bookID[i] + "','"
+ dateTime.toString("yyyy-MM-dd hh:mm:ss") + "',null)")){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
tmpQuery.exec("update books set stock = stock + 1 where "+queryText);
//tmpQuery.exec("update books set total = total where "+queryText);
tmpQuery.exec("delete from borrow where username = '" + nameLine40->text() + "' and borrowDate = '"
+ dateTime.toString("yyyy-MM-dd hh:mm:ss") + "'");
return;
}
}
QMessageBox::about(window,"database","success");
tmpQuery.finish();
showBookTable(USER);
showBorrowTable();
return;
}
void database::searchUser(){
//检查输入信息
if(userName50->text().isEmpty()){
QMessageBox::critical(window,"Error","用户名不能为空!",QMessageBox::Yes);
return;
}
userQueryText = userName50->text();
showUserTable(USER);
}
void database::showUserTable(type type){
//用户信息显示
QStringList header;
//清除表格信息
userTable->clear();
//设置表格为不可选择、不可修改
userTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
userTable->setSelectionMode(QAbstractItemView::NoSelection);
userTable->setRowCount(10);
userTable->setColumnCount(4);
userTable->setFixedWidth(500);
header<<""<<"用户名"<<"密码"<<"权限"/*<<"操作"*/;
userTable->setHorizontalHeaderLabels(header);
for(int i=0;i<4;i++){
if(i==0) userTable->setColumnWidth(i,40);
else if(i == 2) userTable->setColumnWidth(i,200);
else userTable->setColumnWidth(i,110);
}
if(type == VISITOR) return;
userQuery=new QSqlQuery();
if(!userQuery->exec("select * from users where username like '%" + userQueryText + "%'")){
QMessageBox::critical(window,"Error",userQuery->lastError().text());
}
else{
if(!userQuery->next()){
QMessageBox::critical(window,"database","无搜索结果");
}
else{
userQuery->previous();
}
}
//用户信息
// QTableWidgetItem *item50[10];
// QTableWidgetItem *item51[10][3];
//删去有bug
for(int i=0;i<10;i++){
item50[i]=new QTableWidgetItem();
}
int i = 0;
// item50=new QTableWidgetItem[10];
while(userQuery->next()){
// item50[i]=new QTableWidgetItem();
item50[i]->setCheckState(Qt::Unchecked);
// item51[i]=new QTableWidgetItem[3];
// QTableWidgetItem *item[3];
userTable->setItem(i,0,item50[i]);
for(int j=0; j<3; j++){
item51[i][j] = new QTableWidgetItem(userQuery->value(j).toString());
item51[i][j]->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
userTable->setItem(i,j+1,item51[i][j]);
}
i++;
if(i == 10) {
break;
}
}
}
void database::giveAuthorization(){
const int max=10;
int flag=0;
QString name="";
QSqlQuery tmpQuery;
for(int i=0;i<max;i++){
if(item50[i]->checkState() == Qt::Checked){
if(flag==0){
name+=" username = ";
}else{
name+=" or username = ";
}
name+="'"+item51[i][0]->text()+"'";
flag = 1;
}
}
if(!flag){
QMessageBox::critical(window,"error","您尚未选择任何用户!");
return;
}
if(!tmpQuery.exec("update users set isLent =1 where "+name)){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
return;
}
QMessageBox::about(window,"database","success");
tmpQuery.finish();
showUserTable(USER);
return;
}
void database::revokeAuthorization(){
const int max=10;
int flag=0;
QString name="";
QSqlQuery tmpQuery;
tmpQuery.clear();
for(int i=0;i<max;i++){
if(item50[i]->checkState() == Qt::Checked){
if(flag==0){
name+=" username = ";
}else{
name+=" or username = ";
}
name+="'"+item51[i][0]->text()+"'";
flag = 1;
}
}
if(!flag){
QMessageBox::critical(window,"error","您尚未选择任何用户!");
return;
}
if(!tmpQuery.exec("update users set isLent = 2 where "+name)){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
return;
}else{
QMessageBox::about(window,"database","success");
showUserTable(USER);
return;
}
}
void database::changeAuthorization(){
const int max=10;
int flag1=0,flag2=0;
QString name1="",name2="";
QSqlQuery tmpQuery;
for(int i=0;i<max;i++){
if(item50[i]->checkState() == Qt::Checked){
if(item51[i][2]->text().compare("true")==0){
if(flag1==0){
name1+=" username = ";
}else{
name1+=" or username = ";
}
name1+="'"+item51[i][0]->text()+"'";
flag1 = 1;
}else{
if(flag2==0){
name2+=" username = ";
}else{
name2+=" or username = ";
}
name2+="'"+item51[i][0]->text()+"'";
flag2 = 1;
}
}
}
if(flag1==0&&flag2==0){
QMessageBox::critical(window,"error","您尚未选择任何用户!");
return;
}
if(flag1){
if(!tmpQuery.exec("update users set isLent = 2 where "+name1)){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
return;
}
}
if(flag2){
if(!tmpQuery.exec("update users set isLent = 1 where "+name2)){
QMessageBox::critical(window,"Error",tmpQuery.lastError().text());
}
}
QMessageBox::about(window,"database","success");
showUserTable(USER);
}
void database::addOneBook(){
if(bookId50->text().isEmpty() || bookType50->currentText().isEmpty() || bookName50->text().isEmpty()
|| press50->text().isEmpty() || year50->text().isEmpty() || author50->text().isEmpty()
|| price50->text().isEmpty() || total50->text().isEmpty() || stock50->text().isEmpty()){
QMessageBox::critical(window,"Error","填写信息不全!","Ok");
return;
}
QSqlQuery tmpQuery;
QVariantList bookIDList,typeList,titleList,pressList,yearList,authorList,priceList,totalNumList,stockList;
bookIDList<<bookId50->text();
typeList<<bookType50->currentText();
titleList<<bookName50->text();
pressList<<press50->text();
yearList<<year50->text();
authorList<<author50->text();
priceList<<price50->text();
totalNumList<<total50->text();
stockList<<stock50->text();
tmpQuery.prepare("insert into books values(?,?,?,?,?,?,?,?,?)");
tmpQuery.addBindValue(bookIDList);
tmpQuery.addBindValue(typeList);
tmpQuery.addBindValue(titleList);
tmpQuery.addBindValue(pressList);
tmpQuery.addBindValue(yearList);
tmpQuery.addBindValue(authorList);