TMall商城系统订单模块分析(3)

2022-08-15
2

点击完成结算操作

请求地址:http://localhost:8080/tmall/order/pay/2022081319163401185

请求方式:PUT方式

操作完成订单的支付:将状态改为提醒发货的状态

完成之后的跳转(GET)

http://localhost:8080/tmall/order/pay/success/2022081319163401185

//更新订单信息为已支付,待发货-ajax
    @ResponseBody
    @RequestMapping(value = "order/pay/{order_code}", method = RequestMethod.PUT)
    public String orderPay(HttpSession session, @PathVariable("order_code") String order_code) {
        JSONObject object = new JSONObject();
        logger.info("检查用户是否登录");
        Object userId = checkUser(session);
        if (userId == null) {
            object.put("success", false);
            object.put("url", "/login");
            return object.toJSONString();
        }
        logger.info("------验证订单信息------");
        logger.info("查询订单是否存在");
        ProductOrder order = productOrderService.getByCode(order_code);
        if (order == null) {
            logger.warn("订单不存在,返回订单列表页");
            object.put("success", false);
            object.put("url", "/order/0/10");
            return object.toJSONString();
        }
        logger.info("验证订单状态");
        if (order.getProductOrder_status() != 0) {
            logger.warn("订单状态不正确,返回订单列表页");
            object.put("success", false);
            object.put("url", "/order/0/10");
            return object.toJSONString();
        }
        logger.info("验证用户与订单是否一致");
        if (order.getProductOrder_user().getUser_id() != Integer.parseInt(userId.toString())) {
            logger.warn("用户与订单信息不一致,返回订单列表页");
            object.put("success", false);
            object.put("url", "/order/0/10");
            return object.toJSONString();
        }
        order.setProductOrderItemList(productOrderItemService.getListByOrderId(order.getProductOrder_id(), null));

        double orderTotalPrice = 0.00;
        if (order.getProductOrderItemList().size() == 1) {
            logger.info("获取单订单项的产品信息");
            ProductOrderItem productOrderItem = order.getProductOrderItemList().get(0);
            Product product = productService.get(productOrderItem.getProductOrderItem_product().getProduct_id());
            product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id()));
            productOrderItem.setProductOrderItem_product(product);
            orderTotalPrice = productOrderItem.getProductOrderItem_price();
            logger.info("更新产品销量信息");
            Product updateProduct = new Product()
                    .setProduct_id(product.getProduct_id())
                    .setProduct_sale_count(product.getProduct_sale_count() + productOrderItem.getProductOrderItem_number());
            logger.info("更新产品信息,产品ID值为:{}", product.getProduct_id());
            boolean yn = productService.update(updateProduct);
            if (!yn) {
                logger.info("产品销量信息更新失败!事务回滚");
                object.put("success", false);
                throw new RuntimeException();
            }
            logger.info("产品销量信息更新成功!");
        } else {
            for (ProductOrderItem productOrderItem : order.getProductOrderItemList()) {
                Product product = productService.get(productOrderItem.getProductOrderItem_product().getProduct_id());
                logger.info("更新产品销量信息");
                Product updateProduct = new Product()
                        .setProduct_id(product.getProduct_id())
                        .setProduct_sale_count(product.getProduct_sale_count() + productOrderItem.getProductOrderItem_number());
                logger.info("更新产品信息,产品ID值为:{}", product.getProduct_id());
                boolean yn = productService.update(updateProduct);
                if (!yn) {
                    logger.info("产品销量信息更新失败!事务回滚");
                    object.put("success", false);
                    throw new RuntimeException();
                }
                logger.info("产品销量信息更新成功!");
                orderTotalPrice += productOrderItem.getProductOrderItem_price();
            }
        }
        logger.info("总共支付金额为:{}元", orderTotalPrice);
        logger.info("更新订单信息");
        ProductOrder productOrder = new ProductOrder()
                .setProductOrder_id(order.getProductOrder_id())
                .setProductOrder_pay_date(new Date())
                .setProductOrder_status((byte) 1);

        boolean yn = productOrderService.update(productOrder);
        if (yn) {
            object.put("success", true);
            object.put("url", "/order/pay/success/" + order_code);
        } else {
            object.put("success", false);
            object.put("url", "/order/0/10");
        }
        return object.toJSONString();
    }

断点调试完成结算

产品结构图

private Integer product_id/*产品ID*/;
private String product_name/*产品名称*/;
private String product_title/*产品标题*/;
private Double product_price/*产品原价格*/;
private Double product_sale_price/*产品促销价格*/;
private Date product_create_date/*产品创建日期*/;
private Category product_category/*产品对应类型*/;
private Byte product_isEnabled/*产品状态*/;
private Integer product_sale_count/*销量数*/;
private Integer product_review_count/*评价数*/;
private List<PropertyValue> propertyValueList/*产品属性值集合*/;
private List<ProductImage> singleProductImageList/*产品预览图片集合*/;
private List<ProductImage> detailProductImageList/*产品详细图片集合*/;
private List<Review> reviewList/*产品评论集合*/;
private List<ProductOrderItem> productOrderItemList/*产品订单项集合*/;

支付完成之后销售数加1(只关联一个物品的时候)

logger.info("更新产品销量信息");
Product updateProduct = new Product()
        .setProduct_id(product.getProduct_id())
        .setProduct_sale_count(product.getProduct_sale_count() + productOrderItem.getProductOrderItem_number());
logger.info("更新产品信息,产品ID值为:{}", product.getProduct_id());
boolean yn = productService.update(updateProduct);
if (!yn) {
    logger.info("产品销量信息更新失败!事务回滚");
    object.put("success", false);
    throw new RuntimeException();
}
logger.info("产品销量信息更新成功!");

一个订单对应多个产品遍历集合数据进行更新

 for (ProductOrderItem productOrderItem : order.getProductOrderItemList()) {
        Product product = productService.get(productOrderItem.getProductOrderItem_product().getProduct_id());
        logger.info("更新产品销量信息");
        Product updateProduct = new Product()
                .setProduct_id(product.getProduct_id())
                .setProduct_sale_count(product.getProduct_sale_count() + productOrderItem.getProductOrderItem_number());
        logger.info("更新产品信息,产品ID值为:{}", product.getProduct_id());
        boolean yn = productService.update(updateProduct);
        if (!yn) {
            logger.info("产品销量信息更新失败!事务回滚");
            object.put("success", false);
            throw new RuntimeException();
        }
        logger.info("产品销量信息更新成功!");
        orderTotalPrice += productOrderItem.getProductOrderItem_price();
    }
}

如果成功前端页面回调,success页面,否则返回订单页面

if (yn) {
    object.put("success", true);
    object.put("url", "/order/pay/success/" + order_code);
} else {
    object.put("success", false);
    object.put("url", "/order/0/10");
}

确认支付的jsp页面向后端发送请求

function pay() {
    $.ajax({
        url: "${pageContext.request.contextPath}/order/pay/${requestScope.productOrder.productOrder_code}",
        type: "PUT",
        data: null,
        dataType: "json",
        success: function (data) {
            if (data.success !== true) {
                alert("订单处理异常,请稍候再试!");
            }
            location.href = "/tmall" + data.url;
        },
        beforeSend: function () {

        },
        error: function () {
            alert("订单支付出现问题,请重新支付!");
            location.href = "/tmall/order/0/10";
        }
    });
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xt8Wv9tp-1660487895920)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220813223928051.png)]
在这里插入图片描述

跳转到支付成功的页面

//转到前台天猫-订单支付成功页
@RequestMapping(value = "order/pay/success/{order_code}", method = RequestMethod.GET)
public String goToOrderPaySuccessPage(Map<String, Object> map, HttpSession session,
                                      @PathVariable("order_code") String order_code) {
    logger.info("检查用户是否登录");
    Object userId = checkUser(session);
    User user;
    if (userId != null) {
        logger.info("获取用户信息");
        user = userService.get(Integer.parseInt(userId.toString()));
        map.put("user", user);
    } else {
        return "redirect:/login";
    }
    logger.info("------验证订单信息------");
    logger.info("查询订单是否存在");
    ProductOrder order = productOrderService.getByCode(order_code);
    if (order == null) {
        logger.warn("订单不存在,返回订单列表页");
        return "redirect:/order/0/10";
    }
    logger.info("验证订单状态");
    if (order.getProductOrder_status() != 1) {
        logger.warn("订单状态不正确,返回订单列表页");
        return "redirect:/order/0/10";
    }
    logger.info("验证用户与订单是否一致");
    if (order.getProductOrder_user().getUser_id() != Integer.parseInt(userId.toString())) {
        logger.warn("用户与订单信息不一致,返回订单列表页");
        return "redirect:/order/0/10";
    }
    order.setProductOrderItemList(productOrderItemService.getListByOrderId(order.getProductOrder_id(), null));

    double orderTotalPrice = 0.00;
    if (order.getProductOrderItemList().size() == 1) {
        logger.info("获取单订单项的产品信息");
        ProductOrderItem productOrderItem = order.getProductOrderItemList().get(0);
        orderTotalPrice = productOrderItem.getProductOrderItem_price();
    } else {
        for (ProductOrderItem productOrderItem : order.getProductOrderItemList()) {
            orderTotalPrice += productOrderItem.getProductOrderItem_price();
        }
    }
    logger.info("订单总金额为:{}元", orderTotalPrice);

    logger.info("获取订单详情-地址信息");
    Address address = addressService.get(order.getProductOrder_address().getAddress_areaId());
    Stack<String> addressStack = new Stack<>();
    //详细地址
    addressStack.push(order.getProductOrder_detail_address());
    //最后一级地址
    addressStack.push(address.getAddress_name() + " ");
    //如果不是第一级地址
    while (!address.getAddress_areaId().equals(address.getAddress_regionId().getAddress_areaId())) {
        address = addressService.get(address.getAddress_regionId().getAddress_areaId());
        addressStack.push(address.getAddress_name() + " ");
    }
    StringBuilder builder = new StringBuilder();
    while (!addressStack.empty()) {
        builder.append(addressStack.pop());
    }
    logger.info("订单地址字符串:{}", builder);
    order.setProductOrder_detail_address(builder.toString());

    map.put("productOrder", order);
    map.put("orderTotalPrice", orderTotalPrice);

    logger.info("转到前台天猫-订单支付成功页");
    return "fore/productPaySuccessPage";
}
Stack<String> addressStack = new Stack<>();
//详细地址
addressStack.push(order.getProductOrder_detail_address());
//最后一级地址
addressStack.push(address.getAddress_name() + " ");
//如果不是第一级地址
while (!address.getAddress_areaId().equals(address.getAddress_regionId().getAddress_areaId())) {
    address = addressService.get(address.getAddress_regionId().getAddress_areaId());
    addressStack.push(address.getAddress_name() + " ");
}
StringBuilder builder = new StringBuilder();
while (!addressStack.empty()) {
    builder.append(addressStack.pop());
}

这些地方有些查询难度结合后台

订单页面显示

知识点:订单,订单项,产品的关系

一个客户可以下多个订单,一个订单可以包含一个或多个订购项目,一个订购项目可以对应一个或多个商品

请求地址

http://localhost:8080/tmall/order/0/10

请求方式:get方式

说明:跳转订单页面并携带分页参数

对应的代码部分

//转到前台天猫-订单列表页
@RequestMapping(value = "order", method = RequestMethod.GET)
public String goToPageSimple() {
    return "redirect:/order/0/10";
}
@RequestMapping(value = "order/{index}/{count}", method = RequestMethod.GET)
public String goToPage(HttpSession session, Map<String, Object> map,
                       @RequestParam(required = false) Byte status,
                       @PathVariable("index") Integer index/* 页数 */,
                       @PathVariable("count") Integer count/* 行数*/) {
    logger.info("检查用户是否登录");
    Object userId = checkUser(session);
    User user;
    if (userId != null) {
        logger.info("获取用户信息");
        user = userService.get(Integer.parseInt(userId.toString()));
        map.put("user", user);
    } else {
        return "redirect:/login";
    }
    Byte[] status_array = null;
    if (status != null) {
        status_array = new Byte[]{status};
    }

    PageUtil pageUtil = new PageUtil(index, count);
    logger.info("根据用户ID:{}获取订单列表", userId);
    List<ProductOrder> productOrderList = productOrderService.getList(
            new ProductOrder().setProductOrder_user(
                    new User().setUser_id(Integer.valueOf(userId.toString()))
            ), status_array, new OrderUtil("productOrder_id", true), pageUtil
    );

    //订单总数量
    Integer orderCount = 0;
    if (productOrderList.size() > 0) {
        orderCount = productOrderService.getTotal(new ProductOrder().setProductOrder_user(new User().setUser_id(Integer.valueOf(userId.toString()))), status_array);
        logger.info("获取订单项信息及对应的产品信息");
        for (ProductOrder order : productOrderList) {
            List<ProductOrderItem> productOrderItemList = productOrderItemService.getListByOrderId(
                    order.getProductOrder_id(), null
            );
            if (productOrderItemList != null) {
                for (ProductOrderItem productOrderItem : productOrderItemList) {
                    Integer product_id = productOrderItem.getProductOrderItem_product().getProduct_id();
                    Product product = productService.get(product_id);
                    product.setSingleProductImageList(productImageService.getList(
                            product_id, (byte) 0, new PageUtil(0, 1)
                    ));
                    productOrderItem.setProductOrderItem_product(product);
                    if (order.getProductOrder_status() == 3) {
                        productOrderItem.setIsReview(reviewService.getTotalByOrderItemId(
                                productOrderItem.getProductOrderItem_id()) > 0
                        );
                    }
                }
            }
            order.setProductOrderItemList(productOrderItemList);
        }
    }
    pageUtil.setTotal(orderCount);

    logger.info("获取产品分类列表信息");
    List<Category> categoryList = categoryService.getList(null, new PageUtil(0, 5));

    map.put("pageUtil", pageUtil);
    map.put("productOrderList", productOrderList);
    map.put("categoryList", categoryList);
    map.put("status", status);

    logger.info("转到前台天猫-订单列表页");
    return "fore/orderListPage";
}

断点调试获取订单项信息及对应的产品信息

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f6MwYWqK-1660487895922)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220814160332290.png)]

第一步获取用户的所有订单

List<ProductOrder> productOrderList = productOrderService.getList(
        new ProductOrder().setProductOrder_user(
                new User().setUser_id(Integer.valueOf(userId.toString()))
        ), status_array, new OrderUtil("productOrder_id", true), pageUtil
);

第二步:根据订单遍历集合获取订单项

 logger.info("获取订单项信息及对应的产品信息");
    for (ProductOrder order : productOrderList) {
        List<ProductOrderItem> productOrderItemList = productOrderItemService.getListByOrderId(
                order.getProductOrder_id(), null
        );
        if (productOrderItemList != null) {
            for (ProductOrderItem productOrderItem : productOrderItemList) {
                Integer product_id = productOrderItem.getProductOrderItem_product().getProduct_id();
                Product product = productService.get(product_id);
                product.setSingleProductImageList(productImageService.getList(
                        product_id, (byte) 0, new PageUtil(0, 1)
                ));
                productOrderItem.setProductOrderItem_product(product);
                if (order.getProductOrder_status() == 3) {
                    productOrderItem.setIsReview(reviewService.getTotalByOrderItemId(
                            productOrderItem.getProductOrderItem_id()) > 0
                    );
                }
            }
        }
        order.setProductOrderItemList(productOrderItemList);
    }
}

更具订单项中的产品id或者产品信息,并将查询到的产品添加到订单对应的集合中,返回到前端

订单状态码显示和提交

对应JSP页面orderListPage

状态码为0(待付款)

<c:when test="${productOrder.productOrder_status==0}">
    <td class="td_order_content"
        rowspan="${fn:length(requestScope.productOrderItemList)}">
        <span class="span_order_status" title="等待买家付款">等待买家付款</span>
    </td>
    <td class="td_order_content"
        rowspan="${fn:length(requestScope.productOrderItemList)}">
        <a class="order_btn pay_btn"
           href="${pageContext.request.contextPath}/order/pay/${productOrder.productOrder_code}">立即付款</a>
        <p class="order_close"><a class="order_close" href="javascript:void(0)"
                                  οnclick="closeOrder('${productOrder.productOrder_code}')">取消订单</a>
        </p>
    </td>
</c:when>

状态码为1待发货

<c:when test="${productOrder.productOrder_status==1}">
    <td class="td_order_content"
        rowspan="${fn:length(requestScope.productOrderItemList)}">
        <span class="span_order_status" title="买家已付款,等待卖家发货">等待卖家发货</span>
    </td>
    <td class="td_order_content"
        rowspan="${fn:length(requestScope.productOrderItemList)}">
        <a class="order_btn delivery_btn"
           href="${pageContext.request.contextPath}/order/delivery/${productOrder.productOrder_code}">提醒发货</a>
    </td>
</c:when>

状态码为2已发货待确认

<c:when test="${productOrder.productOrder_status==2}">
                                        <td class="td_order_content"
                                            rowspan="${fn:length(requestScope.productOrderItemList)}">
                                            <span class="span_order_status" title="卖家已发货,等待买家确认">等待买家确认</span>
                                        </td>
                                        <td class="td_order_content"
                                            rowspan="${fn:length(requestScope.productOrderItemList)}">
                                            <a class="order_btn confirm_btn"
                                               href="${pageContext.request.contextPath}/order/confirm/${productOrder.productOrder_code}">确认收货</a>
                                        </td>
                                    </c:when>

状态码为3交易完成

<c:when test="${productOrder.productOrder_status==3}">
    <td class="td_order_content"
        rowspan="${fn:length(requestScope.productOrderItemList)}">
        <span class="span_order_status" title="交易成功">交易成功</span>
    </td>
</c:when>

状态的更改

  1. 立即付款

    /order/pay/${productOrder.productOrder_code} 跳转支付页面完成支付

  2. 提醒发货

    /order/delivery/${productOrder.productOrder_code}

//更新订单信息为已发货,待确认-ajax
@RequestMapping(value = "order/delivery/{order_code}", method = RequestMethod.GET)
public String orderDelivery(HttpSession session, @PathVariable("order_code") String order_code) {
    logger.info("检查用户是否登录");
    Object userId = checkUser(session);
    if (userId == null) {
        return "redirect:/order/0/10";
    }
    logger.info("------验证订单信息------");
    logger.info("查询订单是否存在");
    ProductOrder order = productOrderService.getByCode(order_code);
    if (order == null) {
        logger.warn("订单不存在,返回订单列表页");
        return "redirect:/order/0/10";
    }
    logger.info("验证订单状态");
    if (order.getProductOrder_status() != 1) {
        logger.warn("订单状态不正确,返回订单列表页");
        return "redirect:/order/0/10";
    }
    logger.info("验证用户与订单是否一致");
    if (order.getProductOrder_user().getUser_id() != Integer.parseInt(userId.toString())) {
        logger.warn("用户与订单信息不一致,返回订单列表页");
        return "redirect:/order/0/10";
    }
    logger.info("更新订单信息");
    ProductOrder productOrder = new ProductOrder()
            .setProductOrder_id(order.getProductOrder_id())
            .setProductOrder_delivery_date(new Date())
            .setProductOrder_status((byte) 2);  //设置信息为2

    productOrderService.update(productOrder);

    return "redirect:/order/0/10";
}
  1. 确认收货

    完成之后跳转到订单确认页面

    /order/confirm/${productOrder.productOrder_code}

//转到前台天猫-订单确认页
@RequestMapping(value = "order/confirm/{order_code}", method = RequestMethod.GET)
public String goToOrderConfirmPage(Map<String, Object> map, HttpSession session,
                                   @PathVariable("order_code") String order_code) {
    logger.info("检查用户是否登录");
    Object userId = checkUser(session);
    User user;
    if (userId != null) {
        logger.info("获取用户信息");
        user = userService.get(Integer.parseInt(userId.toString()));
        map.put("user", user);
    } else {
        return "redirect:/login";
    }
    logger.info("------验证订单信息------");
    logger.info("查询订单是否存在");
    ProductOrder order = productOrderService.getByCode(order_code);
    if (order == null) {
        logger.warn("订单不存在,返回订单列表页");
        return "redirect:/order/0/10";
    }
    logger.info("验证订单状态");
    if (order.getProductOrder_status() != 2) {
        logger.warn("订单状态不正确,返回订单列表页");
        return "redirect:/order/0/10";
    }
    logger.info("验证用户与订单是否一致");
    if (order.getProductOrder_user().getUser_id() != Integer.parseInt(userId.toString())) {
        logger.warn("用户与订单信息不一致,返回订单列表页");
        return "redirect:/order/0/10";
    }
    order.setProductOrderItemList(productOrderItemService.getListByOrderId(order.getProductOrder_id(), null));

    double orderTotalPrice = 0.00;
    if (order.getProductOrderItemList().size() == 1) {
        logger.info("获取单订单项的产品信息");
        ProductOrderItem productOrderItem = order.getProductOrderItemList().get(0);
        Integer product_id = productOrderItem.getProductOrderItem_product().getProduct_id();
        Product product = productService.get(product_id);
        product.setSingleProductImageList(productImageService.getList(product_id, (byte) 0, new PageUtil(0, 1)));
        productOrderItem.setProductOrderItem_product(product);
        orderTotalPrice = productOrderItem.getProductOrderItem_price();
    } else {
        logger.info("获取多订单项的产品信息");
        for (ProductOrderItem productOrderItem : order.getProductOrderItemList()) {
            Integer product_id = productOrderItem.getProductOrderItem_product().getProduct_id();
            Product product = productService.get(product_id);
            product.setSingleProductImageList(productImageService.getList(product_id, (byte) 0, new PageUtil(0, 1)));
            productOrderItem.setProductOrderItem_product(product);
            orderTotalPrice += productOrderItem.getProductOrderItem_price();
        }
    }
    logger.info("订单总金额为:{}元", orderTotalPrice);

    map.put("productOrder", order);
    map.put("orderTotalPrice", orderTotalPrice);

    logger.info("转到前台天猫-订单确认页");
    return "fore/orderConfirmPage";
}

根据订单号查询订单,判断订单中的订单项的长度,判断是多订单,单一订单。

最后遍历集合动态的向订单中求总的价格

相关信息

    评论