后端
概述
本模块主要完成对图书的借阅处理。需要实现三个接口,第一个是获取所有的读者的借阅卡号,第二个是获取所有的未被借阅的图书编号,第三个则是根据借阅卡的卡号和图书编号进行借阅。
获取所有的读者的借阅卡号
接口路径:/users/getAllCardNumber
Query参数:无
返回值形式:JSON 统一封装的对象R进行响应
1. { 2. "status": 200,//状态码 3. "msg": "获取成功",//响应信息 4. "data": [//返回数据==》所有的读者编号 5. "65125544" 6. ], 7. "map": {} 8. }
UsersController
1. @GetMapping("getAllCardNumber") 2. public R getAllCardNumber() { 3. List<Long> list=usersService.getAllCardNumber(); 4. return R.ok(list,"获取成功"); 5. }
UsersService
List<Long> getAllCardNumber();
UsersServiceImpl
1. @Override 2. public List<Long> getAllCardNumber() { 3. 4. List<Users> list = this.list(); 5. List<Long> collect = list.stream().map(item -> { 6. Long cardNumber = item.getCardNumber(); 7. return cardNumber; 8. }).collect(Collectors.toList()); 9. return collect; 10. 11. }
测试
GET http://localhost:8888/users/getAllCardNumber
获取所有的未被借阅的图书编号
接口路径:/books/getAllNotBorrowBoook
Query参数:无
返回值形式:JSON 统一封装的对象R进行响应
1. { 2. "status": 200,//状态码 3. "msg": "获取成功",//响应信息 4. "data": [//返回数据==》未被借阅的书籍编号 5. "65125544" 6. ], 7. "map": {} 8. }
BooksController
1. @GetMapping("getAllNotBorrowBook") 2. public R getAllNotBorrowBook() { 3. List<String> list= booksService.getAllNotBorrowBook(); 4. return R.ok(list,"获取成功"); 5. 6. }
BooksService
List<String> getAllNotBorrowBook();
BooksServiceImpl
1. @Override 2. public List<String> getAllNotBorrowBook() { 3. List<Books> list = this.list(); 4. List<String> collect = list.stream().filter(item -> { 5. if ("已借出".equals(item.getBookStatus())) { 6. return false; 7. } else { 8. return true; 9. } 10. }).map(item -> { 11. return item.getBookNumber(); 12. }).collect(Collectors.toList()); 13. return collect; 14. }
测试
GET http://localhost:8888/users/getAllCardNumber
进行借阅
接口路径:/booksBorrow/borrowBook
Query参数:BooksBorrowDTO
返回值形式:JSON 统一封装的对象R进行响应
1. { 2. "status": 200,//状态码 3. "msg": "获取成功",//响应信息 4. "data":true,//成功还是失败的响应 5. "map": {} 6. }
BooksBorrowController
1. /** 2. * 借书 3. * @param booksBorrowDTO 4. * @return 5. */ 6. @PostMapping("borrowBook") 7. public R borrowBookByCardNumberAndBookNumber(@RequestBody BooksBorrowDTO booksBorrowDTO) { 8. System.out.println("1234"); 9. boolean b= booksBorrowService.borrowBook(booksBorrowDTO); 10. if (b==false){ 11. return R.error("借书失败(请校验数据,IBSN或借书凭证出错,或者该书已借出异常)"); 12. }else { 13. return R.ok("借书成功"); 14. } 15. 16. }
BooksBorrowService
boolean borrowBook(BooksBorrowDTO booksBorrowDTO);
BooksBorrowServiceImpl
1. @Override 2. @Transactional(rollbackFor = Exception.class) 3. public boolean borrowBook(BooksBorrowDTO booksBorrowDTO) { 4. /** 5. * 1.接受前端请求中的参数(借阅证号、图书编号、借阅时间) 6. * 2.先根据借阅证号查询是否有此用户存在,不存在直接返回错误信息 7. * 3.用户存在,根据图书编号查询图书表,查询是否有图书存在并且该图书的状态是未借出 8. * 4.用户存在,图书存在,且未借出 说明可以借出该图书 9. * 5.#获取用户的规则编号#,根据编号查询出规则(判断空),获取规则的可借天数 10. * 6.设置期限天数为当前时间+规则的可借天数 设置归还日期为空 11. * 7.调用bookBorrow,进行插入记录 12. * 8.如果插入成功,修改在图书表中对应图书编号的状态为已借出 13. * 9.判断是否更新成功 14. * 10.插入成功+更新成功,则返回请求状态码200和请求信息 15. */ 16. // 图书编号 17. Integer bookNumber = booksBorrowDTO.getBookNumber(); 18. // 借阅证号 19. Long cardNumber = booksBorrowDTO.getCardNumber(); 20. // 借阅时间 21. LocalDateTime borrowDate = booksBorrowDTO.getBorrowDate(); 22. LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>(); 23. queryWrapper.eq(Users::getCardNumber, cardNumber); 24. Users users = usersMapper.selectOne(queryWrapper); 25. if (users == null) { 26. return false; 27. } 28. LambdaQueryWrapper<Books> queryWrapper1 = new LambdaQueryWrapper<>(); 29. queryWrapper1.eq(Books::getBookNumber, bookNumber); 30. Books book = booksMapper.selectOne(queryWrapper1); 31. if ((book == null) || (book.getBookStatus().equals(Constant.BOOKDISABLE))) { 32. return false; 33. } 34. 35. // 规则编号 36. Integer ruleNumber = users.getRuleNumber(); 37. LambdaQueryWrapper<BookRule> queryWrapper2 = new LambdaQueryWrapper<>(); 38. queryWrapper2.eq(BookRule::getBookRuleId, ruleNumber); 39. BookRule bookRule = bookRuleMapper.selectOne(queryWrapper2); 40. if (bookRule == null) { 41. return false; 42. } 43. // 可借天数 44. Integer bookDays = bookRule.getBookDays(); 45. LocalDateTime closeDate = borrowDate.plusDays(bookDays); 46. BooksBorrow booksBorrow1 = new BooksBorrow(); 47. booksBorrow1.setBorrowId(null); 48. booksBorrow1.setBookNumber(bookNumber); 49. booksBorrow1.setCardNumber(cardNumber); 50. booksBorrow1.setBorrowDate(borrowDate); 51. booksBorrow1.setCloseDate(closeDate); 52. booksBorrow1.setReturnDate(null); 53. boolean flag = this.save(booksBorrow1); 54. if (!flag) { 55. return false; 56. } 57. book.setBookStatus(Constant.BOOKDISABLE); 58. int update = booksMapper.update(book, queryWrapper1); 59. if (update==0) { 60. return false; 61. } 62. Violation violation = new Violation(); 63. BeanUtils.copyProperties(booksBorrow1, violation, "borrowId"); 64. violation.setViolationId(null); 65. violation.setViolationMessage(""); 66. violation.setBookNumber(book.getBookNumber()); 67. violation.setViolationAdminId(booksBorrowDTO.getBookAdminId()); 68. int save = violationMapper.insert(violation); 69. if (save==0) { 70. return false; 71. } 72. return true; 73. }
前端
在\compone\BookManage\下建立BorrowBook.vue
在compone\ManageHome.vue中添加
1. <el-menu-item index="borrowbook" @click="saveNavState('borrowbook')"> 2. <i class="el-icon-collection"></i> 3. <span slo="title">借阅图书</span> 4. </el-menu-item>
在route下的index.js添加:
import BorrowBook from '../components/BookManage/BorrowBook.vue'
{ path: '/borrowbook', component: BorrowBook },
BorrowBook.vue
1. <template> 2. <div class="borrowbook_container"> 3. <div class="header"><p>借阅图书 </p></div> 4. <div class="banner"> 5. 6. 借阅证号:<br> <el-select v-model.number="borrowInfo.cardNumber" placeholder="请选择借阅证号"> 7. <el-option 8. v-for="item in cardNumbers" 9. :key="item" 10. :label="item" 11. :value="item"> 12. </el-option> 13. </el-select> 14. 图书编号:<br> <el-select v-model.number="borrowInfo.bookNumber" placeholder="请选择图书编号"> 15. <el-option 16.