package com.yj.web;

import com.yj.bean.Book;
import com.yj.bean.Page;
import com.yj.service.BookService;
import com.yj.service.impl.BookServiceImpl;
import com.yj.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author yj
 * @create 2020-08-24 15:25
 *
 * BookServlet类继承自BaseServlet，用于处理与书籍相关的请求。
 * 通过调用BookService接口的实现类BookServiceImpl，完成书籍的增删改查、分页查询等操作。
 */
public class BookServlet extends BaseServlet {

    // 创建BookService接口的实现类BookServiceImpl对象
    private BookService bookService = new BookServiceImpl();

    /**
     * 处理添加书籍的请求
     * @param req HttpServletRequest对象
     * @param resp HttpServletResponse对象
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取当前页码，默认从0开始
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 0);
        // 页码加1，表示添加书籍后跳转到下一页
        pageNo += 1;

        // 将请求参数封装到Book对象中
        Book book = (Book) WebUtils.copyParamToBean(req.getParameterMap(), new Book());

        // 调用BookService的addBook方法添加书籍
        bookService.addBook(book);

        // 重定向到书籍管理页面，并跳转到添加书籍后的页码
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
    }

    /**
     * 处理删除书籍的请求
     * @param req HttpServletRequest对象
     * @param resp HttpServletResponse对象
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取要删除的书籍ID
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);

        // 调用BookService的deleteBookById方法删除书籍
        bookService.deleteBookById(i);

        // 重定向到书籍管理页面，并保持当前页码
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    /**
     * 处理更新书籍的请求
     * @param req HttpServletRequest对象
     * @param resp HttpServletResponse对象
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将请求参数封装到Book对象中
        Book book = (Book) WebUtils.copyParamToBean(req.getParameterMap(), new Book());

        // 调用BookService的updateBook方法更新书籍
        bookService.updateBook(book);

        // 重定向到书籍管理页面，并保持当前页码
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    /**
     * 处理获取单个书籍信息的请求
     * @param req HttpServletRequest对象
     * @param resp HttpServletResponse对象
     * @throws ServletException
     * @throws IOException
     */
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取要查询的书籍ID
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);

        // 调用BookService的queryBookById方法查询书籍
        Book book = bookService.queryBookById(i);

        // 将查询到的书籍信息保存到request域中
        req.setAttribute("book", book);

        // 请求转发到书籍编辑页面
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req, resp);
    }

    /**
     * 处理查询所有书籍的请求
     * @param req HttpServletRequest对象
     * @param resp HttpServletResponse对象
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 调用BookService的queryBooks方法查询所有书籍
        List<Book> books = bookService.queryBooks();

        // 将查询到的书籍列表保存到request域中
        req.setAttribute("books", books);

        // 请求转发到书籍管理页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }

    /**
     * 处理分页查询书籍的请求
     * @param req HttpServletRequest对象
     * @param resp HttpServletResponse对象
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求参数pageNo和pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);

        // 调用BookService的page方法进行分页查询
        Page<Book> page = bookService.page(pageNo, pageSize);

        // 设置分页请求的URL
        page.setUrl("manager/bookServlet?action=page");

        // 将分页对象保存到request域中
        req.setAttribute("page", page);

        // 请求转发到书籍管理页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }
}