-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
726 lines (619 loc) · 27.8 KB
/
api.py
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
from flask import Blueprint, request
from decorators import login_required, permission_check
from sqlalchemy.exc import OperationalError, IntegrityError, ProgrammingError
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from models import TBReader, TBReaderType, TBBook, TBBorrow, db_session, engine
from random import randint
import datetime
api = Blueprint('api', __name__)
@api.route('/user/login', methods=['POST'])
def login():
uid = request.form.get('uid', False)
pwd = request.form.get('pwd', False)
if not (uid and pwd):
return {'status': 0, 'message': '未输入用户ID或密码'}
try:
r: TBReader = TBReader.query.filter(TBReader.rdID == uid, TBReader.rdPwd == pwd).one()
return {'status': 1, 'message': '登录成功',
'uid': r.rdID, 'pwd': pwd, 'username': r.rdName}
except NoResultFound:
return {'status': 0, 'message': '用户ID或密码错误'}
except MultipleResultsFound:
return {'status': 0, 'message': '用户数据异常'}
@api.route('/user/changePwd', methods=['POST'])
@login_required
def changePwd():
uid = request.form.get('uid', False)
old_pwd = request.form.get('old_pwd', False)
new_pwd = request.form.get('new_pwd', False)
if not (uid and old_pwd and new_pwd):
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == uid, TBReader.rdPwd == old_pwd).one()
r.rdPwd = new_pwd
session.commit()
session.close()
except NoResultFound:
return {'status': 0, 'message': '旧密码错误'}
except MultipleResultsFound:
return {'status': 0, 'message': '用户数据异常'}
else:
return {'status': 1, 'message': '密码修改成功, 请重新登录'}
@api.route('/reader/type/add', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def addReaderType():
rdType = request.form.get('rdType', False)
rdTypeName = request.form.get('rdTypeName', False)
CanLendQty = request.form.get('CanLendQty', False)
CanLendDay = request.form.get('CanLendDay', False)
CanContinueTimes = request.form.get('CanContinueTimes', False)
PunishRate = request.form.get('PunishRate', False)
DateValid = request.form.get('DateValid', False)
if not (rdType and rdTypeName and CanLendQty and CanLendDay and CanContinueTimes and PunishRate and DateValid):
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
rt = TBReaderType(rdType=rdType, rdTypeName=rdTypeName, CanLendQty=CanLendQty, CanLendDay=CanLendDay,
CanContinueTimes=CanContinueTimes, PunishRate=PunishRate, DateValid=DateValid)
session.add(rt)
session.commit()
session.close()
except OperationalError:
return {'status': 0, 'message': '输入的数据类型可能有误'}
except IntegrityError:
return {'status': 0, 'message': '读者类型编号或读者类型名重复'}
else:
return {'status': 1, 'message': '新的读者类型添加成功'}
@api.route('/reader/type/find', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def findReaderType():
rdType = request.form.get('rdType', False)
rdTypeName = request.form.get('rdTypeName', False)
if rdType is False and rdTypeName is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
if rdType and rdTypeName:
rt: TBReaderType = TBReaderType.query.filter(TBReaderType.rdType == rdType,
TBReaderType.rdTypeName == rdTypeName).one()
if rdType and rdTypeName is False:
rt: TBReaderType = TBReaderType.query.filter(TBReaderType.rdType == rdType).one()
if rdType is False and rdTypeName:
rt: TBReaderType = TBReaderType.query.filter(TBReaderType.rdTypeName == rdTypeName).one()
return {'status': 1, 'message': '已找到一个读者类型', 'rdType': rt.rdType,
'rdTypeName': rt.rdTypeName, 'CanLendQty': rt.CanLendQty,
'CanLendDay': rt.CanLendDay, 'CanContinueTimes': rt.CanContinueTimes,
'PunishRate': rt.PunishRate, 'DateValid': rt.DateValid}
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的读者类型'}
except MultipleResultsFound:
return {'status': 0, 'message': '读者类型数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据类型可能有误'}
@api.route('/reader/type/change', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def changeReaderType():
rdType = request.form.get('rdType', False)
rdTypeName = request.form.get('rdTypeName', False)
CanLendQty = request.form.get('CanLendQty', False)
CanLendDay = request.form.get('CanLendDay', False)
CanContinueTimes = request.form.get('CanContinueTimes', False)
PunishRate = request.form.get('PunishRate', False)
DateValid = request.form.get('DateValid', False)
if not (rdType and rdTypeName and CanLendQty and CanLendDay and CanContinueTimes and PunishRate and DateValid):
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
rt: TBReaderType = session.query(TBReaderType).filter(TBReaderType.rdType == rdType).one()
rt.rdTypeName = rdTypeName
rt.CanLendQty = CanLendQty
rt.CanLendDay = CanLendDay
rt.CanContinueTimes = CanContinueTimes
rt.PunishRate = PunishRate
rt.DateValid = DateValid
session.commit()
session.close()
except NoResultFound:
return {'status': 0, 'message': '未找到指定的读者类型'}
except MultipleResultsFound:
return {'status': 0, 'message': '读者类型数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据类型可能有误'}
else:
return {'status': 1, 'message': '指定的读者类型修改成功'}
@api.route('/reader/type/delete', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def deleteReaderType():
rdType = request.form.get('rdType', False)
rdTypeName = request.form.get('rdTypeName', False)
if rdType is False and rdTypeName is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
if rdType and rdTypeName:
rt: TBReaderType = session.query(TBReaderType).filter(TBReaderType.rdType == rdType,
TBReaderType.rdTypeName == rdTypeName).one()
if rdType and rdTypeName is False:
rt: TBReaderType = session.query(TBReaderType).filter(TBReaderType.rdType == rdType).one()
if rdType is False and rdTypeName:
rt: TBReaderType = session.query(TBReaderType).filter(TBReaderType.rdTypeName == rdTypeName).one()
session.delete(rt)
session.commit()
session.close()
except NoResultFound:
return {'status': 0, 'message': '未找到指定的读者类型'}
except MultipleResultsFound:
return {'status': 0, 'message': '读者类型数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据类型可能有误'}
else:
return {'status': 1, 'message': '指定的读者类型删除成功'}
@api.route('/reader/add', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def addReader():
rdID = request.form.get('rdID', False)
rdName = request.form.get('rdName', False)
rdSex = request.form.get('rdSex', False)
rdType = request.form.get('rdType', False)
rdDept = request.form.get('rdDept', False)
rdPhone = request.form.get('rdPhone', False)
rdEmail = request.form.get('rdEmail', False)
if not (rdID and rdName and rdSex and rdType and rdDept and rdPhone and rdEmail):
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
r = TBReader(rdID=rdID, rdName=rdName, rdSex=rdSex, rdType=rdType,
rdDept=rdDept, rdPhone=rdPhone, rdEmail=rdEmail)
session.add(r)
session.commit()
session.close()
except IntegrityError:
return {'status': 0, 'message': '已存在相同的借书证编号, 无法重新创建'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
else:
return {'status': 1, 'message': '新借书证创建成功'}
@api.route('/reader/add/status', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def addReaderStatus():
rdID = request.form.get('rdID', False)
if rdID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
TBReader.query.filter(TBReader.rdID == rdID).one()
except NoResultFound:
return {'status': 1, 'message': '此人可以办理借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据类型可能有误'}
else:
return {'status': 0, 'message': '该人已办理借书证, 无需再次办理'}
@api.route('/reader/change', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def changeReader():
rdID = request.form.get('rdID', False)
rdName = request.form.get('rdName', False)
rdSex = request.form.get('rdSex', False)
rdType = request.form.get('rdType', False)
rdDept = request.form.get('rdDept', False)
rdPhone = request.form.get('rdPhone', False)
rdEmail = request.form.get('rdEmail', False)
if not (rdID and rdName and rdSex and rdType and rdDept and rdPhone and rdEmail):
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == rdID).one()
r.rdName = rdName
r.rdSex = rdSex
r.rdType = rdType
r.rdDept = rdDept
r.rdPhone = rdPhone
r.rdEmail = rdEmail
session.commit()
session.close()
except NoResultFound:
return {'status': 0, 'message': '未找到指定的借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
else:
return {'status': 1, 'message': '指定的借书证修改成功'}
@api.route('/reader/loss/report', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def lossReport():
rdID = request.form.get('rdID', False)
if rdID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == rdID).one()
if r.rdStatus == '有效':
r.rdStatus = '挂失'
session.commit()
session.close()
return {'status': 1, 'message': '挂失办理成功'}
else:
session.close()
return {'status': 0, 'message': '该借书证状态不是有效, 无法办理挂失'}
except NoResultFound:
return {'status': 0, 'message': '未找到指定的借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/reader/loss/report/status', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def lossReportStatus():
rdID = request.form.get('rdID', False)
if rdID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
r: TBReader = TBReader.query.filter(TBReader.rdID == rdID).one()
if r.rdStatus == '有效':
return {'status': 1, 'message': '该借书证状态有效, 可以办理挂失'}
else:
return {'status': 0, 'message': '该借书证状态不是有效, 无法办理挂失'}
except NoResultFound:
return {'status': 0, 'message': '未找到指定的借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/reader/loss/cancel', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def lossCancel():
rdID = request.form.get('rdID', False)
if rdID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == rdID).one()
if r.rdStatus == '挂失':
r.rdStatus = '有效'
session.commit()
session.close()
return {'status': 1, 'message': '解除挂失办理成功'}
else:
session.close()
return {'status': 0, 'message': '该借书证状态不是挂失, 无法办理解除挂失'}
except NoResultFound:
return {'status': 0, 'message': '未找到指定的借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/reader/loss/cancel/status', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def lossCancelStatus():
rdID = request.form.get('rdID', False)
if rdID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
r: TBReader = TBReader.query.filter(TBReader.rdID == rdID).one()
if r.rdStatus == '挂失':
return {'status': 1, 'message': '该借书证状态为挂失, 可以办理解除挂失'}
else:
return {'status': 0, 'message': '该借书证状态不是挂失, 无法办理解除挂失'}
except NoResultFound:
return {'status': 0, 'message': '未找到指定的借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/reader/replace', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def replaceReader():
rdID = request.form.get('rdID', False)
if rdID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == rdID).one()
if r.rdStatus == '挂失':
r.rdStatus = '注销'
rdID_n = rdID + str(randint(0, 10))
rn = TBReader(rdID=rdID_n, rdName=r.rdName, rdSex=r.rdSex, rdType=r.rdType,
rdDept=r.rdDept, rdPhone=r.rdPhone, rdEmail=r.rdEmail,
rdDateReg=r.rdDateReg, rdPhoto=r.rdPhoto, rdBorrowQty=r.rdBorrowQty,
rdPwd=r.rdPwd, rdAdminRoles=r.rdAdminRoles)
session.add(rn)
session.commit()
bl = session.query(TBBorrow).filter(TBBorrow.rdID == rdID).all()
for i in bl:
i.rdID = rdID_n
session.commit()
session.close()
return {'status': 1, 'message': '补办成功', 'new_rdID': rdID_n}
else:
session.close()
return {'status': 0, 'message': '该借书证状态不是挂失, 无法办理补办'}
except NoResultFound:
return {'status': 0, 'message': '未找到指定的借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
except IntegrityError:
return {'status': 0, 'message': '随机产生的新编号与数据库现有的借书证号出现重复, 请再次点击办理按钮'}
@api.route('/reader/delete', methods=['POST'])
@login_required
@permission_check(0b0001, True)
def deleteReader():
rdID = request.form.get('rdID', False)
if rdID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == rdID).one()
if r.rdBorrowQty > 0:
session.close()
return {'status': 0, 'message': '有未归还图书, 不能办理注销'}
else:
r.rdStatus = '注销'
session.commit()
session.close()
return {'status': 1, 'message': '注销成功'}
except NoResultFound:
return {'status': 0, 'message': '未找到指定的借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/book/add', methods=['POST'])
@login_required
@permission_check(0b0010)
def addBook():
bkName = request.form.get('bkName', False)
bkAuthor = request.form.get('bkAuthor', False)
bkPress = request.form.get('bkPress', False)
bkNum = request.form.get('bkNum', False)
bkNum = int(bkNum)
if not (bkName and bkAuthor and bkPress):
return {'status': 0, 'message': '传入数据不全'}
if bkNum <= 0:
return {'status': 0, 'message': '新书数量为零或负数'}
try:
con = engine.connect()
for i in range(bkNum):
con.execute(f"insert into TB_Book (bkName, bkAuthor, bkPress, bkStatus, bkPrice) values ('{bkName}', '{bkAuthor}', '{bkPress}', '在馆', 20)")
con.close()
except ProgrammingError:
return {'status': 0, 'message': '输入的数据可能有误'}
else:
return {'status': 1, 'message': '新书入库成功'}
@api.route('/book/find', methods=['POST'])
@login_required
@permission_check(0b0010)
def findBook():
bkID = request.form.get('bkID', False)
if bkID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
b: TBBook = TBBook.query.filter(TBBook.bkID == bkID).one()
return {'status': 1, 'message': '已找到一本图书', 'bkID': b.bkID,
'bkName': b.bkName, 'bkAuthor': b.bkAuthor, 'bkPress': b.bkPress}
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的图书'}
except MultipleResultsFound:
return {'status': 0, 'message': '图书数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/book/change', methods=['POST'])
@login_required
@permission_check(0b0010)
def changeBook():
bkID = request.form.get('bkID', False)
bkName = request.form.get('bkName', False)
bkAuthor = request.form.get('bkAuthor', False)
bkPress = request.form.get('bkPress', False)
if not (bkName and bkAuthor and bkPress and bkID):
return {'status': 0, 'message': '传入数据不全'}
try:
session = db_session()
b: TBBook = session.query(TBBook).filter(TBBook.bkID == bkID).one()
b.bkName = bkName
b.bkAuthor = bkAuthor
b.bkPress = bkPress
session.commit()
session.close()
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的图书'}
except MultipleResultsFound:
return {'status': 0, 'message': '图书数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
else:
return {'status': 1, 'message': '修改成功'}
@api.route('/book/delete', methods=['POST'])
@login_required
@permission_check(0b0010)
def deleteBook():
bkID = request.form.get('bkID', False)
if bkID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
b: TBBook = session.query(TBBook).filter(TBBook.bkID == bkID).one()
session.delete(b)
session.commit()
session.close()
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的图书'}
except MultipleResultsFound:
return {'status': 0, 'message': '图书数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
else:
return {'status': 1, 'message': '指定的图书删除成功'}
@api.route('/borrow/add', methods=['POST'])
@login_required
@permission_check(0b0100)
def addBorrow():
rdID = request.form.get('rdID', False)
bkID = request.form.get('bkID', False)
op = request.cookies.get('uid', False)
if rdID is False and bkID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
b = TBBorrow(rdID=rdID, bkID=bkID, OperatorLend=op, IdContinueTimes=0)
r: TBReader = session.query(TBReader).filter(TBReader.rdID == rdID).one()
rt: TBReaderType = session.query(TBReaderType).filter(TBReaderType.rdType == r.rdType).one()
b.IdDateOut = datetime.datetime.now()
b.IdDateRetPlan = datetime.datetime.now() + datetime.timedelta(days=rt.CanLendDay)
session.add(b)
r.rdBorrowQty += 1
bk: TBBook = session.query(TBBook).filter(TBBook.bkID == bkID).one()
bk.bkStatus = '借出'
session.commit()
session.close()
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
except (NoResultFound, MultipleResultsFound):
return {'status': 0, 'message': '后台数据异常'}
else:
return {'status': 1, 'message': '借书办理成功'}
@api.route('/borrow/add/status/reader', methods=['POST'])
@login_required
@permission_check(0b0100)
def reader_status():
rdID = request.form.get('rdID', False)
if rdID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
r: TBReader = TBReader.query.filter(TBReader.rdID == rdID).one()
if r.rdStatus in ('挂失', '注销'):
return {'status': 0, 'message': '借书证状态为挂失或注销, 不能借书'}
rt: TBReaderType = TBReaderType.query.filter(TBReaderType.rdType == r.rdType).one()
if rt.CanLendQty <= r.rdBorrowQty:
return {'status': 0, 'message': '可借书数量不足, 不能借书'}
b: TBBorrow = TBBorrow.query.filter(TBBorrow.rdID == rdID).all()
for i in b:
if i.IsHasReturn == 0 and str(datetime.datetime.now()) > str(i.IdDateRetPlan):
return {'status': 0, 'message': '存在超期未归还图书, 不能借书'}
return {'status': 1, 'message': '该读者可以借书'}
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的借书证'}
except MultipleResultsFound:
return {'status': 0, 'message': '借书证数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/borrow/add/status/book', methods=['POST'])
@login_required
@permission_check(0b0100)
def book_status():
bkID = request.form.get('bkID', False)
if bkID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
b: TBBook = TBBook.query.filter(TBBook.bkID == bkID).one()
if b.bkStatus != '在馆':
return {'status': 0, 'message': '该图书不在馆, 不能借书'}
return {'status': 1, 'message': '该图书在馆, 可以借书'}
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的图书'}
except MultipleResultsFound:
return {'status': 0, 'message': '图书数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/borrow/continue', methods=['POST'])
@login_required
@permission_check(0b0100)
def continueBorrow():
bkID = request.form.get('bkID', False)
if bkID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
bo: TBBorrow = session.query(TBBorrow).filter(TBBorrow.bkID == bkID, TBBorrow.IsHasReturn == 0).one()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == bo.rdID).one()
rt: TBReaderType = TBReaderType.query.filter(TBReaderType.rdType == r.rdType).one()
bo.IdContinueTimes += 1
bo.IdDateRetPlan += datetime.timedelta(days=rt.CanLendDay)
session.commit()
session.close()
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的借阅记录'}
except MultipleResultsFound:
return {'status': 0, 'message': '借阅数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
else:
return {'status': 1, 'message': '续借办理成功'}
@api.route('/borrow/continue/status', methods=['POST'])
@login_required
@permission_check(0b0100)
def continue_status():
bkID = request.form.get('bkID', False)
if bkID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
bk: TBBook = session.query(TBBook).filter(TBBook.bkID == bkID).one()
if bk.bkStatus != '借出':
return {'status': 0, 'message': '图书不是借出状态, 不能办理续借'}
bo: TBBorrow = session.query(TBBorrow).filter(TBBorrow.bkID == bkID, TBBorrow.IsHasReturn == 0).one()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == bo.rdID).one()
if r.rdStatus in ('挂失', '注销'):
return {'status': 0, 'message': '借书证状态为挂失或注销, 不能办理续借'}
rt: TBReaderType = TBReaderType.query.filter(TBReaderType.rdType == r.rdType).one()
if bo.IdContinueTimes >= rt.CanContinueTimes:
return {'status': 0, 'message': '续借次数已用完, 不能办理续借'}
session.close()
return {'status': 1, 'message': '该书可以办理续借, 借书证为: ' + str(r.rdID) + ', 请借阅管理员检查读者出示的借书证是否一致'}
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的借阅记录'}
except MultipleResultsFound:
return {'status': 0, 'message': '借阅数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
@api.route('/borrow/return', methods=['POST'])
@login_required
@permission_check(0b0100)
def returnBorrow():
bkID = request.form.get('bkID', False)
rdID = request.form.get('rdID', False)
op = request.cookies.get('uid', False)
if bkID is False:
return {'status': 0, 'message': '传入数据不完整'}
try:
session = db_session()
bk: TBBook = session.query(TBBook).filter(TBBook.bkID == bkID).one()
bo: TBBorrow = session.query(TBBorrow).filter(TBBorrow.bkID == bkID, TBBorrow.rdID == rdID, TBBorrow.IsHasReturn == 0).one()
r: TBReader = session.query(TBReader).filter(TBReader.rdID == rdID).one()
rt: TBReaderType = TBReaderType.query.filter(TBReaderType.rdType == r.rdType).one()
bo.IdDateRetAct = datetime.datetime.now()
overdate = datetime.datetime.now() - bo.IdDateRetPlan
overdays = overdate.days
if overdays > 0:
bo.IdOverDay = overdays
bo.IdOverMoney = overdays * rt.PunishRate
bo.IdPunishMoney = overdays * rt.PunishRate
bo.OperatorRet = op
bo.IsHasReturn = 1
bk.bkStatus = '在馆'
r.rdBorrowQty -= 1
session.commit()
session.close()
except NoResultFound:
return {'status': 0, 'message': '没有查询到相应的借阅记录'}
except MultipleResultsFound:
return {'status': 0, 'message': '借阅数据异常'}
except OperationalError:
return {'status': 0, 'message': '输入的数据可能有误'}
else:
return {'status': 1, 'message': '还书成功'}