day11-功能實現10

一刀一個小西瓜發表於2022-12-29

家居網購專案實現010

以下皆為部分程式碼,詳見 https://github.com/liyuelian/furniture_mall.git

24.bugFix-新增購物車按鈕動態處理

24.1需求分析/圖解

  1. 如某個家居的庫存量為0,前臺的“add to cart”按鈕顯示為“暫時缺貨”
  2. 後臺也要求新增校驗,只有在庫存>0時,才能新增到購物車

24.2程式碼實現

  1. 修改customer/index.jsp

    image-20221229180412921
  2. CartServlet新增判斷,如果在新增購物車時,該家居庫存為0,就停止業務

    image-20221229180321357

24.3完成測試

image-20221229180523809

25.功能23-附加功能-訂單管理

25.1需求分析/圖解

點選訂單管理,前往order.jsp頁面進行訂單管理

image-20221229181533011

點選詳情的眼睛圖示,可以顯示該訂單詳情,即顯示某個訂單的所有訂單項

image-20221227201044836 image-20221227201115765

25.2思路分析

點選訂單管理按鈕,跳轉到訂單管理頁面order.jsp。根據使用者id,在訂單管理頁面中顯示使用者對應的訂單。點選訂單詳情,跳轉到order_detail.jsp中,根據訂單號顯示訂單所有項。

25.3程式碼實現

25.3.1dao層

OrderDAO

/**
 * 根據使用者id,查詢該使用者的所有訂單Order
 *
 * @param id 使用者id
 * @return 返回訂單
 */
public List<Order> queryOrderByMemberId(int id);

OrderDAOImpl

@Override
public List<Order> queryOrderByMemberId(int id) {
    String sql = "SELECT `id`,`create_time` AS createTime,`price`,`status`,`member_id` AS memberId " +
            "FROM `order` " +
            "WHERE `member_id`=?";
    return queryMulti(sql, Order.class, id);
}

test包-OrderDAOImplTest

@Test
public void queryOrderByMemberId() {
    List<Order> orders = orderDAO.queryOrderByMemberId(2);
    for (Order order : orders) {
        System.out.println(order);
    }
}
image-20221229190005583

OrderItemDAO

/**
 * 根據訂單id-orderId查詢對應的表單項orderItem
 *
 * @param orderId 訂單id
 * @return 返回表單項orderItem
 */
public List<OrderItem> queryOrderItemByOrderId(String orderId);

OrderItemDAOImpl

@Override
public List<OrderItem> queryOrderItemByOrderId(String orderId) {
    String sql =
            "SELECT `id`,`name`,`price`,`count`,`total_price` AS totalPrice,`order_id` AS orderId " +
                    "FROM `order_item` " +
                    "WHERE `order_id`=?";
    return queryMulti(sql, OrderItem.class, orderId);
}

OrderItemDAOImplTest

@Test
public void queryOrderItemByOrderId() {
    List<OrderItem> orderItems = orderItemDAO.queryOrderItemByOrderId("16722358644142");
    for (OrderItem orderItem : orderItems) {
        System.out.println(orderItem);
    }
}

image-20221229191847363

25.3.2service層

OrderService

/**
 * 根據使用者id返回訂單order
 * @param id 使用者id
 * @return 訂單
 */
public List<Order> queryOrderByMemberId(int id);

/**
* 根據訂單id返回對應的訂單項orderItem
* @param orderId 訂單id
* @return 返回訂單項
*/
public List<OrderItem> queryOrderItemByOrderId(String orderId);

OrderServiceImpl

@Override
public List<Order> queryOrderByMemberId(int id) {
    return orderDAO.queryOrderByMemberId(id);
}

@Override
public List<OrderItem> queryOrderItemByOrderId(String orderId) {
    return orderItemDAO.queryOrderItemByOrderId(orderId);
}

test包-OrderServiceImpl

@Test
public void queryOrderByMemberId() {
    List<Order> orders = orderService.queryOrderByMemberId(3);
    for (Order order : orders) {
        System.out.println(order);
    }
}

@Test
public void queryOrderItemByOrderId() {
    List<OrderItem> orderItems = orderService.queryOrderItemByOrderId("16722370522643");
    for (OrderItem orderItem : orderItems) {
        System.out.println(orderItem);
    }
}
image-20221229190434546 image-20221229192741882

25.3.3web層

OrderServlet,增加兩個方法

/**
 * 顯示訂單order
 *
 * @param req
 * @param resp
 * @throws ServletException
 * @throws IOException
 */
protected void showOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //獲取當前使用者id
    Member member = (Member) req.getSession().getAttribute("member");
    if (member == null) {
        //重定向到登入頁面
        resp.sendRedirect(req.getContextPath() + "/views/member/login.jsp");
        return;
    }
    int id = DataUtils.parseInt(member.getId().toString(), 0);
    //根據使用者id查詢對應的訂單
    List<Order> orders = orderService.queryOrderByMemberId(id);
    //將訂單資料返回顯示
    req.setAttribute("orders", orders);
    //請求轉發到order.jsp顯示
    req.getRequestDispatcher("/views/order/order.jsp").forward(req, resp);
}


/**
 * 顯示訂單項orderItem
 *
 * @param req
 * @param resp
 * @throws ServletException
 * @throws IOException
 */
protected void showOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //獲取當前orderid
    String orderId = req.getParameter("orderId");
    //根據orderId返回對應的訂單項orderItem
    List<OrderItem> orderItems = orderService.queryOrderItemByOrderId(orderId);
    //計算該order中所有orderItem的總商品數量和總價格
    Integer totalCount = 0;
    BigDecimal totalPrice = BigDecimal.valueOf(0);
    for (OrderItem orderItem : orderItems) {
        totalCount += orderItem.getCount();
        totalPrice = totalPrice.add(orderItem.getTotalPrice());
    }
    System.out.println("totalCount=>" + totalCount);
    System.out.println("totalPrice=>" + totalPrice);
    //將訂單項資料返回顯示
    //訂單id
    req.setAttribute("orderId", orderId);
    //訂單商品總數
    req.setAttribute("totalCount", totalCount);
    //訂單商品總額
    req.setAttribute("totalPrice", totalPrice);
    //訂單項
    req.setAttribute("orderItems", orderItems);
    //請求轉發到order_detail.jsp
    req.getRequestDispatcher("/views/order/order_detail.jsp").forward(req, resp);
}

25.3.4前端頁面修改

25.4完成測試

使用者只有在登入情況下但能進入訂單管理

image-20221229204717352

點選詳情的眼睛圖案,即可檢視該訂單詳情

image-20221229204819992

相關文章