书城第七阶段补充

简介: 书城第七阶段补充

DAO层

dao

OrderDao

package com.atguigu.dao;
import com.atguigu.pojo.Order;
import java.util.List;
public interface OrderDao {
    public int saveOrder(Order order);
    public List<Order> queryOrders();
    public int changeOrderStatus(String orderId,int status);
    public  List<Order> queryByUserId(int userId);
}

OrderItemDao

package com.atguigu.dao;
import com.atguigu.pojo.OrderItem;
import java.util.List;
public interface OrderItemDao {
    public int saveOrderItem(OrderItem orderItem);
    public List<OrderItem> queryOrderItemByOrderId(String orderId);
}

impl

OrderDaoImpl

package com.atguigu.dao.impl;
import com.atguigu.dao.OrderDao;
import com.atguigu.pojo.Order;
import java.util.List;
public class OrderDaoImpl extends BaseDao implements OrderDao {
    @Override
    public int saveOrder(Order order) {
        String sql="insert into t_order(order_id,create_time,price,status,user_id)values(?,?,?,?,?)";
        return update(sql,order.getOrderId(),order.getCreateTime(),order.getPrice(),order.getStatus(),order.getUserId());
    }
    @Override
    public List<Order> queryOrders() {
        String sql = "select order_id orderId,create_time createTime,price,status,user_id from t_order";
        return queryForList(Order.class, sql);
    }
    @Override
    public int changeOrderStatus(String orderId, int status) {
        String sql="update t_order set status=? where order_id=?";
        return update(sql,status,orderId);
    }
    @Override
    public List<Order> queryByUserId(int userId) {
        String sql = "select order_id orderId,create_time createTime,price,status,user_id userId from t_order where user_id=?";
        return queryForList(Order.class,sql,userId);
    }
}

OrderItemDaoImpl

package com.atguigu.service;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderItem;
import java.util.List;
public interface OrderService {
    public String createOrder(Cart cart,Integer userId);
    public List<Order> showAllOrders();
    public int sendOrder(String orderId);
    public List<OrderItem> showOrderDetail(String orderId);
    public List<Order> showMyOrders(int userId);
    public int receiverOrder(String orderId);
}

test

OrderDaoTest

package com.atguigu.test;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.impl.OrderDaoImpl;
import com.atguigu.pojo.Order;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
public class OrderDaoTest {
    @Test
    public void saveOrder() {
        OrderDao orderDao=new OrderDaoImpl();
        orderDao.saveOrder(new Order("1234567890",new Date(),new BigDecimal(100),0,1));
    }
    @Test
    public void queryOrders() {
        OrderDao orderDao=new OrderDaoImpl();
        List<Order> orders = orderDao.queryOrders();
        System.out.println(orders);
    }
    @Test
    public void changeOrderStatus() {
        OrderDao orderDao=new OrderDaoImpl();
        orderDao.changeOrderStatus("1",1);
    }
    @Test
    public void queryByUserId() {
        OrderDao orderDao=new OrderDaoImpl();
        List<Order> orders = orderDao.queryByUserId(1);
        System.out.println(orders);
    }
}

OrderItemDaoTest

package com.atguigu.test;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderItem;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.List;
public class OrderServiceTest {
    private OrderService orderService=new OrderServiceImpl();
    @Test
    public void createOrder() {
        Cart cart=new Cart();
        cart.addItem(new CartItem(1,"java",1,new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(1,"java",1,new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(2,"数据结构与算法",1,new BigDecimal(100),new BigDecimal(100)));
        OrderService orderService=new OrderServiceImpl();
        System.out.println("订单号是:"+orderService.createOrder(cart, 1));
    }
    @Test
    public void showAllOrders() {
        List<Order> orders = orderService.showAllOrders();
        System.out.println(orders);
    }
    @Test
    public void sendOrder() {
        int i = orderService.sendOrder("1");
        System.out.println(i);
    }
    @Test
    public void showOrderDetail() {
        List<OrderItem> orderItems = orderService.showOrderDetail("1");
        System.out.println(orderItems);
    }
    @Test
    public void showMyOrders() {
        List<Order> orders = orderService.showMyOrders(1);
        System.out.println(orders);
    }
    @Test
    public void receiverOrder() {
        int i = orderService.receiverOrder("1");
        System.out.println(i);
    }
}

Service层

service

OrderService

package com.atguigu.service;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderItem;
import java.util.List;
public interface OrderService {
    public String createOrder(Cart cart,Integer userId);
    public List<Order> showAllOrders();
    public int sendOrder(String orderId);
    public List<OrderItem> showOrderDetail(String orderId);
    public List<Order> showMyOrders(int userId);
    public int receiverOrder(String orderId);
}

impl

OrderServiceImpl

package com.atguigu.service.impl;
import com.atguigu.dao.BookDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderItemDao;
import com.atguigu.dao.impl.BookDaoImpl;
import com.atguigu.dao.impl.OrderDaoImpl;
import com.atguigu.dao.impl.OrderItemDaoImpl;
import com.atguigu.pojo.*;
import com.atguigu.service.OrderService;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao =new OrderDaoImpl();
    private OrderItemDao orderItemDao=new OrderItemDaoImpl();
    private BookDao bookDao=new BookDaoImpl();
    @Override
    public String createOrder(Cart cart, Integer userId) {
        //订单号==唯一性
        String orderId=System.currentTimeMillis()+""+userId;
        //创建一个订单对象
        Order order=new Order(orderId,new Date(),cart.getTotalPrice(),0,userId);
        //保存订单
        orderDao.saveOrder(order);
        //遍历购物车中每一个商品项转换为订单保存到数据库
        for (Map.Entry<Integer, CartItem>entry:cart.getItems().entrySet()) {
            //获取购物车每一个商品项
            CartItem cartItem=entry.getValue();
            //转换为订单
            OrderItem orderItem=new OrderItem(null,cartItem.getName(),cartItem.getCount(),cartItem.getPrice(),cartItem.getTotalPrice(),orderId);
            //保存到数据库
            orderItemDao.saveOrderItem(orderItem);
            //更新库存和销量
            Book book = bookDao.queryBookById(cartItem.getId());
            book.setSales(book.getSales()+cartItem.getCount());
            book.setStock(book.getStock()-cartItem.getCount());
            bookDao.updateBook(book);
        }
        //清空购物车
        cart.clear();
        return orderId;
    }
    @Override
    public List<Order> showAllOrders() {
        return orderDao.queryOrders();
    }
    @Override
    public int sendOrder(String orderId) {
        return orderDao.changeOrderStatus(orderId,1);
    }
    @Override
    public List<OrderItem> showOrderDetail(String orderId) {
        return orderItemDao.queryOrderItemByOrderId(orderId);
    }
    @Override
    public List<Order> showMyOrders(int userId) {
        return orderDao.queryByUserId(userId);
    }
    @Override
    public int receiverOrder(String orderId) {
        return orderDao.changeOrderStatus(orderId,2);
    }
}

test

OrderServiceTest

package com.atguigu.test;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderItem;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.List;
public class OrderServiceTest {
    private OrderService orderService=new OrderServiceImpl();
    @Test
    public void createOrder() {
        Cart cart=new Cart();
        cart.addItem(new CartItem(1,"java",1,new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(1,"java",1,new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(2,"数据结构与算法",1,new BigDecimal(100),new BigDecimal(100)));
        OrderService orderService=new OrderServiceImpl();
        System.out.println("订单号是:"+orderService.createOrder(cart, 1));
    }
    @Test
    public void showAllOrders() {
        List<Order> orders = orderService.showAllOrders();
        System.out.println(orders);
    }
    @Test
    public void sendOrder() {
        int i = orderService.sendOrder("1");
        System.out.println(i);
    }
    @Test
    public void showOrderDetail() {
        List<OrderItem> orderItems = orderService.showOrderDetail("1");
        System.out.println(orderItems);
    }
    @Test
    public void showMyOrders() {
        List<Order> orders = orderService.showMyOrders(1);
        System.out.println(orders);
    }
    @Test
    public void receiverOrder() {
        int i = orderService.receiverOrder("1");
        System.out.println(i);
    }
}

Web层

OrderServlet

package com.atguigu.web;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderItem;
import com.atguigu.pojo.User;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();
    /**
     * 生成订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取Cart购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //获取Userid
        User loginUser = (User) req.getSession().getAttribute("user");
        if (loginUser == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
//        System.out.println("OrderServlet 程序在["+Thread.currentThread().getName()+"]中");
        Integer userId = loginUser.getId();
        //调用orderservice.createorder(cart,userid);生成订单
        String orderId = orderService.createOrder(cart, userId);
//        req.setAttribute("orderId",orderId);
        //请求转发至pages/cart/checkout.jsp
//        req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);
        req.getSession().setAttribute("orderId", orderId);
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
    }
    /**
     * 查看所有订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Order> showAllOrders = orderService.showAllOrders();
        req.getSession().setAttribute("showAllOrders", showAllOrders);
        resp.sendRedirect(req.getContextPath() + "/pages/manager/order_manager.jsp");
    }
    /**
     * 发货
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取orderId
        String orderId= req.getParameter("thisOrderId");
        //调用 orderService.sendOrder(orderId);
        orderService.sendOrder(orderId);
        resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showAllOrder");
    }
    /**
     * 查看订单详情
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取orderId
        String orderId= req.getParameter("thisOrderId");
        //调用 orderService.showOrderDetail(orderId);
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        req.getSession().setAttribute("orderItems", orderItems);
        resp.sendRedirect(req.getContextPath() +"/pages/orderDetail/orderDetail.jsp");
    }
    /**
     * 查看我的订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void showMyOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取Userid
        User loginUser = (User) req.getSession().getAttribute("user");
        if (loginUser == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        Integer userId = loginUser.getId();
        //调用orderService.showMyOrders(userId);
        List<Order> showMyOrders = orderService.showMyOrders(userId);
        req.getSession().setAttribute("showMyOrders", showMyOrders);
        resp.sendRedirect(req.getContextPath() + "/pages/order/order.jsp");
    }
    /**
     * 签收订单/确认收货
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId= req.getParameter("thisOrderId");
        orderService.receiverOrder(orderId);
        resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showMyOrder");
    }
}

重新配置web.xml

<servlet>
        <servlet-name>OrderServlet</servlet-name>
        <servlet-class>com.atguigu.web.OrderServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>OrderServlet</servlet-name>
        <url-pattern>/orderServlet</url-pattern>
        <url-pattern>/pages/manager/orderServlet</url-pattern>
    </servlet-mapping>

页面

新增 pages/ordeDetailr/orderDetail.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/9/23
  Time: 17:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>订单详情</title>
    <%--静态包含 base标签,css样式,jquery文件 --%>
    <%@ include file="/pages/common/head.jsp"%>
</head>
<body>
<%@ include file="/pages/common/login_sucess_menu.jsp"%>
<div id="main">
        <table>
            <tr>
                <td>商品名称</td>
                <td>数量</td>
                <td>单价</td>
                <td>总价</td>
                <td>订单号</td>
            </tr>
<%--            ${sessionScope.orderItems}--%>
            <c:if test="${not empty sessionScope.orderItems}">
                <%--  如果订单非空的情况   --%>
                <c:forEach items="${sessionScope.orderItems}" var="order">
                    <tr>
                        <td>${order.name}</td>
                        <td>${order.count}</td>
                        <td>${order.price}</td>
                        <td>${order.totalPrice}</td>
                        <td>${order.orderId}</td>
                    </tr>
                </c:forEach>
            </c:if>
        </table>
    </div>
    <%--静态包含页脚内容--%>
    <%@include file="/pages/common/footer.jsp"%>
</body>
</html>

修改 order.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/8/21
  Time: 下午 02:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>我的订单</title>
    <%--静态包含 base标签,css样式,jquery文件 --%>
    <%@ include file="/pages/common/head.jsp"%>
    <style type="text/css">
    h1 {
        text-align: center;
        margin-top: 200px;
    }
</style>
</head>
<body>
     <div id="header">
         <img class="logo_img" alt="" src="../../static/img/logo.gif">
         <span class="wel_word">我的订单</span>
         <%-- 静态包含登录成功之后的菜单--%>
         <%@ include file="/pages/common/login_sucess_menu.jsp"%>
     </div>
    <div id="main">
        <table>
            <tr>
                <td>日期</td>
                <td>金额</td>
                <td>状态</td>
                <td>详情</td>
            </tr>
<%--            ${sessionScope.showMyOrders}--%>
            <c:if test="${empty sessionScope.showMyOrders}">
                <%--  如果订单为空的情况   --%>
                <td colspan="4"><a href="index.jsp"> 亲,当前订单为空!去首页看看吧!!!</a></td>
            </c:if>
            <c:if test="${not empty sessionScope.showMyOrders}">
                <%--  如果订单非空的情况   --%>
                <c:forEach items="${sessionScope.showMyOrders}" var="showMyOrder">
                    <tr>
                        <td>${showMyOrder.createTime}</td>
                        <td>${showMyOrder.price}</td>
                        <c:if test="${showMyOrder.status==0}">
                            <td>
                                未发货
                            </td>
                        </c:if>
                        <c:if test="${showMyOrder.status==1}">
                            <td>
                                已发货
                                <a href="orderServlet?action=receiverOrder&thisOrderId=${showMyOrder.orderId}">收货</td>
                            </td>
                        </c:if>
                        <c:if test="${showMyOrder.status==2}">
                            <td>已签收</td>
                        </c:if>
                        <td><a href="orderServlet?action=showOrderDetail&thisOrderId=${showMyOrder.orderId}">查看详情</a> </td>
                    </tr>
                </c:forEach>
            </c:if>
        </table>
    </div>
     <%--静态包含页脚内容--%>
     <%@include file="/pages/common/footer.jsp"%>
</body>
</html>

修改 order_manager.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/8/21
  Time: 下午 02:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>订单管理</title>
    <%--静态包含 base标签,css样式,jquery文件 --%>
    <%@ include file="/pages/common/head.jsp"%>
</head>
<body>
    <div id="header">
        <img class="logo_img" alt=""  src="../../static/img/logo.gif">
        <span class="wel_word">订单管理系统</span>
        <%-- 静态包含manager 管理模块的菜单       --%>
        <%@ include file="/pages/common/manager_menu.jsp"%>
    </div>
    <div id="main">
        <table>
            <tr>
                <td>日期</td>
                <td>金额</td>
                <td>详情</td>
                <td>状态</td>
            </tr>
<%--            ${sessionScope.showAllOrders}--%>
            <c:if test="${empty sessionScope.showAllOrders}">
                <%--  如果订单为空的情况   --%>
                <td colspan="4"> 亲,当前订单为空!</td>
            </c:if>
            <c:if test="${not empty sessionScope.showAllOrders}">
                <c:forEach items="${sessionScope.showAllOrders}" var="order">
                    <tr>
                        <td>${order.createTime}</td>
                        <td>${order.price}</td>
                        <td><a href="orderServlet?action=showOrderDetail&thisOrderId=${order.orderId}">查看详情</a></td>
                        <c:if test="${order.status==0}">
                            <td>未发货 <a href="orderServlet?action=sendOrder&thisOrderId=${order.orderId}">发货</a> </td>
                        </c:if>
                        <c:if test="${order.status==1}">
                            <td>已发货</td>
                        </c:if>
                        <c:if test="${order.status==2}">
                            <td>已签收</td>
                        </c:if>
                    </tr>
                </c:forEach>
            </c:if>
        </table>
    </div>
    <%--静态包含页脚内容--%>
    <%@include file="/pages/common/footer.jsp"%>
</body>
</html>

修改 manager_menu.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/8/21
  Time: 下午 05:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div>
    <a href="manager/bookServlet?action=page">图书管理</a>
    <a href="orderServlet?action=showAllOrders">订单管理</a>
    <a href="index.jsp">返回商城</a>
</div>
相关文章
|
数据库
书城项目第六、七阶段2
书城项目第六、七阶段2
48 1
书城项目第六、七阶段1
书城项目第六、七阶段1
44 0
|
前端开发 Java 程序员
书城项目第五阶段-图书模块1
书城项目第五阶段-图书模块1
86 0
|
监控 安全 数据挖掘
泰山众筹系统开发详细指南丨设计方案丨规则玩法丨逻辑功能丨步骤需求丨源码程序
泰山众筹系统是一个基于区块链技术的众筹平台,旨在为用户提供一个安全、透明和高效的众筹环境。
书城项目第五阶段-图书模块2
书城项目第五阶段-图书模块2
29 0
|
Java 应用服务中间件
书城项目第六阶段
书城项目第六阶段
44 0
书城项目-第一阶段:表单验证的实现
书城项目-第一阶段:表单验证的实现
47 0
|
安全 Java 编译器
Java源代码到用户使用都经历了哪些阶段
Java源代码到用户使用都经历了哪些阶段
80 0
|
安全 测试技术 程序员
软工总结——生命周期各阶段所需文档
软工总结——生命周期各阶段所需文档
网络工程项目报价单应该怎么写?记住这6个步骤准没错!
网络工程项目报价单应该怎么写?记住这6个步骤准没错!
311 0