Spring Boot图书管理系统项目实战-7.借阅图书

导航:

pre:  6.图书管理

next:8.续借图书

 

只挑重点的讲,具体的请看项目源码。

1.项目源码

需要的朋友请给个赞,并留下邮箱,给你们发!

 

2.页面设计

2.1 bookBorrow.html





    
    
    借阅图书
    





3.借阅管理service

/**
 * @Description: 借阅图书服务
 * @Author laoxu
 * @Date 2020/1/12
 **/
@Service
public class BookBorrowService extends AbstractService {
    public void add(BookBorrowVO entity) {
        //String username = SecurityUtil.getLoginUser();
        insert("bookBorrowMapper.insert",entity);
    }

    public void modify(BookBorrowVO entity) {
        update("bookBorrowMapper.update",entity);
    }

    public void remove(Long id) {
        delete("bookBorrowMapper.delete",id);
    }

    public void removes(Long[] ids) {
        delete("bookBorrowMapper.deletes",ids);
    }

    public void returnBook(Map param) {
        delete("bookBorrowMapper.return",param);
    }

    public BookBorrowVO get(Long id) {
        return selectOne("bookBorrowMapper.select",id);
    }

    public List getParentList(Long id) {
        return selectList("bookBorrowMapper.selectParentList",id);
    }

    public int count(Map param) {
        return selectOne("bookBorrowMapper.count",param);
    }

    public List getList(Map param) {
        return selectList("bookBorrowMapper.selectList",param);
    }

    public List getPageResult(Map param) {
        return selectList("bookBorrowMapper.selectPageResult",param);
    }


    public int checkBorrow(BookBorrowVO entity){
        return selectOne("bookBorrowMapper.countBorrow",entity);
    }

    public int getBorrowCount(String date){
        return selectOne("bookBorrowMapper.selectBorrowCount",date);
    }

    public int getReturnCount(String date){
        return selectOne("bookBorrowMapper.selectReturnCount",date);
    }
}

4.借阅管理controller

/**
 * @Description: 借阅图书控制器
 * @Author laoxu
 * @Date 2020/1/12 23:24
 **/
@RestController
@RequestMapping("/api/bookBorrow")
public class BookBorrowController {
    @Autowired
    BookBorrowService bookBorrowService;

    @Autowired
    BookReaderService bookReaderService;

    @Autowired
    BookService bookService;

    /**
     * 保存(新增/修改)
     *
     * @param entity
     * @return
     */
    @PostMapping("/save")
    public Result modify(@RequestBody BookBorrowVO entity) {
        Long id = entity.getId();
        if(id!=null){
            bookBorrowService.modify(entity);
        }else{
            Map param = new HashMap<>();
            param.put("isbn",entity.getBookIsbn());
            param.put("code",entity.getReaderCode());
            //1.检查读者编号是否存在
            if(bookReaderService.count(param)==0){
                return ResultUtil.fail("读者不存在!");
            }
            //2.检查图书编号是否存在
            if(bookService.count(param)==0){
                return ResultUtil.fail("图书不存在!");
            }
            //3.检查该读者是否已经借过此书
            int count = bookBorrowService.checkBorrow(entity);
            if(count>0){
                return ResultUtil.fail("您已借过该图书!");
            }
            //4.检查图书剩余
            Book book = bookService.getByIsbn(entity.getBookIsbn());
            int leftNumber = book.getLeftNumber();
            if(leftNumber<1){
                return ResultUtil.fail("图书剩余数量为0!");
            }
            //5.扣减图书剩余
            book.setLeftNumber(leftNumber-1);
            bookService.modify(book);
            //6.保存借阅记录
            bookBorrowService.add(entity);
        }


        return ResultUtil.ok();
    }


    @PostMapping("/remove")
    public Result remove(@RequestBody Long[] ids) {
        bookBorrowService.removes(ids);
        return ResultUtil.ok();
    }

    /**
     *  归还
     * @param ids
     * @return
     */
    @PostMapping("/return")
    @Transactional
    public Result returnBook(@RequestBody Long[] ids) {
        // 1.增加图书库存
        Book entity = null;
        for (Long id: ids) {
            entity = bookService.getByIsbn(bookBorrowService.get(id).getBookIsbn());
            entity.setLeftNumber(entity.getLeftNumber()+1);
            bookService.modify(entity);
        }
        // 2.更新借阅状态
        Map param = new HashMap<>();
        param.put("ids", ids);
        param.put("borrowStatus",1);
        bookBorrowService.returnBook(param);

        return ResultUtil.ok();
    }

    @GetMapping("/get")
    public Result get(@RequestParam("id") Long id) {
        BookBorrowVO entity = bookBorrowService.get(id);

        return ResultUtil.ok(entity);
    }

    @GetMapping("/getPageResult")
    public ResultBean> getPageResult(
            @RequestParam(required = false) Integer[] borrowStatus,
            @RequestParam(required = false) String readerCode,
            @RequestParam(required = false) String isbn,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        Map param = new HashMap<>();
        // 统计记录数
        int totalRows = bookBorrowService.count(param);
        // 计算起始行号
        int offset = (page - 1) * limit;
        int rows = limit;

        param.put("offset", offset);
        param.put("rows", rows);
        param.put("borrowStatus",borrowStatus);
        param.put("readerCode",readerCode);
        param.put("isbn",isbn);

        // 获取当前页结果集
        List entities = bookBorrowService.getPageResult(param);

        ResultBean result = new ResultBean(0, "查询成功", totalRows, entities);

        return result;

    }

    @GetMapping("/getBorrowStat")
    public Result> getBorrowStat(){
        Map map = new HashMap<>();
        List days = DateUtil.getDaysBetwwen(6);

        map.put("columnName",days);
        BorrowStatVO borrowVO = new BorrowStatVO();
        BorrowStatVO returnVO = new BorrowStatVO();
        borrowVO.setName("借");
        returnVO.setName("还");
        borrowVO.setType("bar");
        returnVO.setType("bar");
        List borrowData = new ArrayList<>();
        List returnData = new ArrayList<>();
        for (String day:days) {
            borrowData.add(bookBorrowService.getBorrowCount(day));
            returnData.add(bookBorrowService.getReturnCount(day));
        }
        borrowVO.setData(borrowData);
        returnVO.setData(returnData);

        List list = new ArrayList<>();
        list.add(borrowVO);
        list.add(returnVO);

        map.put("columnValue",list);

        return ResultUtil.ok(map);

    }
}

 

 

需要源码的朋友留下邮箱:)

 

 

 

 

 

你可能感兴趣的:(#,Spring-Boot,Java项目实战,java,spring,boot)