首页>代码>servlet+jdbc实现的java web图书馆借阅管理平台>/lib-mgr/src/com/dreambamboo/controller/ReaderServlet.java
package com.dreambamboo.controller;

import com.dreambamboo.constants.Constants;
import com.dreambamboo.entities.Book;
import com.dreambamboo.entities.Borrow;
import com.dreambamboo.entities.Reader;
import com.dreambamboo.entities.ReturnBook;
import com.dreambamboo.service.BookAdminService;
import com.dreambamboo.service.BookService;
import com.dreambamboo.service.ReaderService;
import com.dreambamboo.service.impl.BookAdminServiceImpl;
import com.dreambamboo.service.impl.BookServiceImpl;
import com.dreambamboo.service.impl.ReaderServiceImpl;
import com.dreambamboo.utils.DateUtils;
import com.dreambamboo.vo.BorrowVO;
import com.dreambamboo.vo.ReaderVO;
import net.sf.json.JSON;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
 * @Author:dreambamboo
 * @Description:读者操作功能类
 * @Date:2019/1/11 10:37
 * @Param:
 * @return:
 */
public class ReaderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        ReaderService readerService = new ReaderServiceImpl();
        BookService bookService = new BookServiceImpl();
        BookAdminService bookAdminService = new BookAdminServiceImpl();
        String method = req.getParameter("method");
        int count = 0;
        Reader reader;
        String bookIdStr;
        String borrowId;
        //查询所有读者借阅图书信息
        if (method == null){
            reader = (Reader) session.getAttribute("reader");
            Constants.pageStr  = req.getParameter("page");
            Constants.limitStr = req.getParameter("limit");
            List<Borrow> borrowList = readerService.findBorrowsByReaderId(reader.getId(),Integer.valueOf(Constants.pageStr),Integer.valueOf(Constants.limitStr));
            count = readerService.findBorrowsCountByReaderId(reader.getId());
            BorrowVO borrowVO = new BorrowVO();
            borrowVO.setCode(0);
            borrowVO.setMsg(null);
            borrowVO.setCount(count);
            borrowVO.setData(borrowList);
            JSONObject jsonObject = JSONObject.fromObject(borrowVO);
            resp.getWriter().write(jsonObject.toString());
        } else switch (method) {
            //查询所有读者信息
            case "findAll":
                List<Reader> readerList;
                Constants.pageStr = req.getParameter("page");
                Constants.limitStr = req.getParameter("limit");
                readerList = readerService.findAllReaders(Integer.parseInt(Constants.pageStr), Integer.parseInt(Constants.limitStr));
                count = readerService.findReadersCount();
                ReaderVO readerVO = new ReaderVO();
                readerVO.setCode(0);
                readerVO.setCount(count);
                readerVO.setData(readerList);
                readerVO.setMsg(null);
                JSONObject jsonObject = JSONObject.fromObject(readerVO);
                resp.getWriter().write(jsonObject.toString());
                break;
            //编辑读者信息
            case "preEdit":
                Constants.readerId = req.getParameter("readerId");
                reader = readerService.findReaderById(Integer.parseInt(Constants.readerId));
                req.setAttribute("reader", reader);
                req.getRequestDispatcher("/reader/reader-edit.jsp").forward(req, resp);
                break;
            case "edit":
                Constants.readerId = req.getParameter("id");
                Constants.readerName = req.getParameter("name");
                Constants.username = req.getParameter("username");
                Constants.password = req.getParameter("password");
                Constants.tel = req.getParameter("tel");
                Constants.cardId = req.getParameter("cardid");
                Constants.gender = req.getParameter("gender");
                reader = new Reader(Integer.parseInt(Constants.readerId), Constants.readerName, Constants.username, Constants.cardId, Constants.tel, Constants.password, Constants.gender);
                readerService.modifyReader(reader);
                resp.sendRedirect("/reader/reader-list.jsp");
                break;
            //删除读者信息
            case "delete":
                Constants.readerId = req.getParameter("readerId");
                readerService.removeReaderById(Integer.parseInt(Constants.readerId));
                resp.sendRedirect("/reader/reader-list.jsp");
                break;
            //添加读者信息
            case "add":
                Constants.readerName = req.getParameter("name");
                Constants.username = req.getParameter("username");
                Constants.tel = req.getParameter("tel");
                Constants.cardId = req.getParameter("cardid");
                Constants.password = req.getParameter("password");
                Constants.gender = req.getParameter("gender");
                reader = new Reader(Constants.readerName, Constants.username, Constants.cardId, Constants.tel, Constants.password, Constants.gender);
                readerService.addReader(reader);
                resp.sendRedirect("/reader/reader-list.jsp");
                break;
            //读者借阅图书操作
            case "borrow":
                bookIdStr = req.getParameter("bookId");
                Book book = bookService.findBookById(Integer.parseInt(bookIdStr));
                reader = (Reader) session.getAttribute("reader");
                Borrow borrow = new Borrow();
                borrow.setReader(reader);
                borrow.setBook(book);
                borrow.setBookAdmin(null);
                Date date = new Date();
                borrow.setBorrowtime(DateUtils.dateToString(date));
                borrow.setReturntime(null);
                borrow.setState(0);
                readerService.addBorrow(borrow);
                resp.sendRedirect("/reader/reader.jsp");
                break;
            //读者归还图书操作
            case "return":
                borrowId = req.getParameter("borrowId");
                borrow = readerService.findBorrowById(Integer.parseInt(borrowId));
                borrow.setState(4);
                bookAdminService.modifyBorrow(borrow);
                reader = (Reader) session.getAttribute("reader");
                ReturnBook returnBook = new ReturnBook();
                returnBook.setBook(borrow.getBook());
                returnBook.setReader(reader);
                Date returnDate = new Date();
                returnBook.setReturntime(DateUtils.dateToString(returnDate));
                returnBook.setBookAdmin(null);
                returnBook.setState(4);
                bookAdminService.addReturnBook(returnBook);
                resp.sendRedirect("/reader/reader.jsp");
                break;
        }

    }
}
最近下载更多
duoduo1234  LV2 1月15日
人大叔大婶左权  LV2 2023年6月1日
北方菜  LV11 2023年3月21日
ewan007  LV30 2023年2月8日
rain112  LV31 2023年1月3日
2521436308  LV10 2022年11月25日
最代码-宋家辉  LV61 2022年11月20日
Weishenghui  LV7 2022年10月24日
liul101112  LV2 2022年6月30日
testuser1234567  LV24 2022年6月30日
最近浏览更多
krispeng  LV13 8月26日
暂无贡献等级
gujiuzzy 5月25日
暂无贡献等级
玖零定制问题修复  LV34 4月4日
XJH939  LV1 3月28日
duoduo1234  LV2 1月15日
asddwh  LV13 2023年12月26日
颜菜菜  LV2 2023年12月23日
xiaoqian555 2023年12月20日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友