• Tamll商城管理系统订单模块(2)


    创建订单

    直接购买商品提交订单

    请求后端的地址

    http://localhost:8080/tmall/order/create/64?product_number=1

    请求方式:Get

    携带参数product_number=2

     //转到前台天猫-订单建立页(直接购买时建立的订单)
        @RequestMapping(value = "order/create/{product_id}", method = RequestMethod.GET)
        public String goToOrderConfirmPage(@PathVariable("product_id") Integer product_id,
                                           @RequestParam(required = false, defaultValue = "1") Short product_number,
                                           Map<String, Object> map,
                                           HttpSession session,
                                           HttpServletRequest request) throws UnsupportedEncodingException {
            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("通过产品ID获取产品信息:{}", product_id);
            Product product = productService.get(product_id);
            if (product == null) {
                return "redirect:/";
            }
            logger.info("获取产品的详细信息");
            product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id()));
            product.setSingleProductImageList(productImageService.getList(product_id, (byte) 0, new PageUtil(0, 1)));
    
            logger.info("封装订单项对象");
            ProductOrderItem productOrderItem = new ProductOrderItem();
            productOrderItem.setProductOrderItem_product(product);
            productOrderItem.setProductOrderItem_number(product_number);
            productOrderItem.setProductOrderItem_price(product.getProduct_sale_price() * product_number);
            productOrderItem.setProductOrderItem_user(new User().setUser_id(Integer.valueOf(userId.toString())));
    
            //初始化订单地址信息
            String addressId = "110000";
            String cityAddressId = "110100";
            String districtAddressId = "110101";
            String detailsAddress = null;
            String order_post = null;
            String order_receiver = null;
            String order_phone = null;
            Cookie[] cookies = request.getCookies();
            //如果读取到了浏览器Cookie
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    String cookieName = cookie.getName();
                    String cookieValue = cookie.getValue();
                    switch (cookieName) {
                        case "addressId":
                            addressId = cookieValue;
                            break;
                        case "cityAddressId":
                            cityAddressId = cookieValue;
                            break;
                        case "districtAddressId":
                            districtAddressId = cookieValue;
                            break;
                        case "order_post":
                            order_post = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "order_receiver":
                            order_receiver = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "order_phone":
                            order_phone = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "detailsAddress":
                            detailsAddress = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                    }
                }
            }
            logger.info("获取省份信息");
            List<Address> addressList = addressService.getRoot();
            logger.info("获取addressId为{}的市级地址信息", addressId);
            List<Address> cityAddress = addressService.getList(null, addressId);
            logger.info("获取cityAddressId为{}的区级地址信息", cityAddressId);
            List<Address> districtAddress = addressService.getList(null, cityAddressId);
    
            List<ProductOrderItem> productOrderItemList = new ArrayList<>();
            productOrderItemList.add(productOrderItem);
    
            map.put("orderItemList", productOrderItemList);
            map.put("addressList", addressList);
            map.put("cityList", cityAddress);
            map.put("districtList", districtAddress);
            map.put("orderTotalPrice", productOrderItem.getProductOrderItem_price());
    
            map.put("addressId", addressId);
            map.put("cityAddressId", cityAddressId);
            map.put("districtAddressId", districtAddressId);
            map.put("order_post", order_post);
            map.put("order_receiver", order_receiver);
            map.put("order_phone", order_phone);
            map.put("detailsAddress", detailsAddress);
    
            logger.info("转到前台天猫-订单建立页");
            return "fore/productBuyPage";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    断点调试走建立普通订单的流程

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

    第一步:根据产品id获取产品的信息。为空则返回首页

    Product product = productService.get(product_id);
    if (product == null) {
        return "redirect:/";
    }
    
    • 1
    • 2
    • 3
    • 4

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

    调用业务逻辑层查询出产品的分类信息和图片信息完成product类的set方法赋值

    product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id()));
    product.setSingleProductImageList(productImageService.getList(product_id, (byte) 0, new PageUtil(0, 1)));
    
    • 1
    • 2

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

    封装订单对象

    ProductOrderItem productOrderItem = new ProductOrderItem();
    productOrderItem.setProductOrderItem_product(product);
    productOrderItem.setProductOrderItem_number(product_number);
    productOrderItem.setProductOrderItem_price(product.getProduct_sale_price() * product_number);
    productOrderItem.setProductOrderItem_user(new User().setUser_id(Integer.valueOf(userId.toString())));
    
    • 1
    • 2
    • 3
    • 4
    • 5

    订单对象结构分析

    private Integer productOrderItem_id/*订单项ID*/;
    private Short productOrderItem_number/*订单项产品数量*/;
    private Double productOrderItem_price/*订单项产品总价格*/;
    private String productOrderItem_userMessage/*订单项备注*/;
    private Product productOrderItem_product/*订单项对应产品*/;
    private ProductOrder productOrderItem_order/*订单项对应订单*/;
    private User productOrderItem_user/*订单项对应用户*/;
    //订单产品是否已经评价
    private Boolean isReview;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

    之后通过将初始化的信息加入到cookie和修改购物车之后发送请求走的流程一样

     Cookie[] cookies = request.getCookies();
            //如果读取到了浏览器Cookie
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    String cookieName = cookie.getName();
                    String cookieValue = cookie.getValue();
                    switch (cookieName) {
                        case "addressId":
                            addressId = cookieValue;
                            break;
                        case "cityAddressId":
                            cityAddressId = cookieValue;
                            break;
                        case "districtAddressId":
                            districtAddressId = cookieValue;
                            break;
                        case "order_post":
                            order_post = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "order_receiver":
                            order_receiver = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "order_phone":
                            order_phone = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "detailsAddress":
                            detailsAddress = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                    }
                }
            }
            logger.info("获取省份信息");
            List<Address> addressList = addressService.getRoot();
            logger.info("获取addressId为{}的市级地址信息", addressId);
            List<Address> cityAddress = addressService.getList(null, addressId);
            logger.info("获取cityAddressId为{}的区级地址信息", cityAddressId);
            List<Address> districtAddress = addressService.getList(null, cityAddressId);
    
            List<ProductOrderItem> productOrderItemList = new ArrayList<>();
            productOrderItemList.add(productOrderItem);
    
            map.put("orderItemList", productOrderItemList);
            map.put("addressList", addressList);
            map.put("cityList", cityAddress);
            map.put("districtList", districtAddress);
            map.put("orderTotalPrice", productOrderItem.getProductOrderItem_price());
    
            map.put("addressId", addressId);
            map.put("cityAddressId", cityAddressId);
            map.put("districtAddressId", districtAddressId);
            map.put("order_post", order_post);
            map.put("order_receiver", order_receiver);
            map.put("order_phone", order_phone);
            map.put("detailsAddress", detailsAddress);
    
            logger.info("转到前台天猫-订单建立页");
            return "fore/productBuyPage";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

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

    从cookie中获取信息进行封装

    之后查询出相关的地址信息进行封装

    logger.info("获取省份信息");
    List<Address> addressList = addressService.getRoot();
    logger.info("获取addressId为{}的市级地址信息", addressId);
    List<Address> cityAddress = addressService.getList(null, addressId);
    logger.info("获取cityAddressId为{}的区级地址信息", cityAddressId);
    List<Address> districtAddress = addressService.getList(null, cityAddressId);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    封装所有集合返回到前端的页面完成测试

    map.put("orderItemList", productOrderItemList);
    map.put("addressList", addressList);
    map.put("cityList", cityAddress);
    map.put("districtList", districtAddress);
    map.put("orderTotalPrice", productOrderItem.getProductOrderItem_price());
    
    map.put("addressId", addressId);
    map.put("cityAddressId", cityAddressId);
    map.put("districtAddressId", districtAddressId);
    map.put("order_post", order_post);
    map.put("order_receiver", order_receiver);
    map.put("order_phone", order_phone);
    map.put("detailsAddress", detailsAddress);
    
    logger.info("转到前台天猫-订单建立页");
    return "fore/productBuyPage";
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    java 使用URLEncoder.encode和URLDecoder.decode编解码(utf-8)中文及特殊字符

    本文主要介绍Java中,使用URLEncoder.encode和URLDecoder.decode对url地址链接中,中文字符及特殊字符用 UTF-8字符集进行编码和解码的方法,及相关的示例代码。

    1、URLEncoder.encode和URLDecoder.decode

    URL只能使用英文字母、阿拉伯数字和某些标点符号,不能使用其他文字和符号,即

    只有字母和数字[0-9a-zA-Z]、一些特殊符号$-_.+!*'()[不包括双引号]、以及某些保留字(空格转换为+),才可以不经过编码直接用于URL,如果URL中有汉字,就必须编码后使用。

    URLDecoder类包含一个decode(String s,String enc)静态方法,它可以将application/x-www-form-urlencoded MIME字符串转成编码前的字符串;

    URLEncoder类包含一个encode(String s,String enc)静态方法,它可以将中文字符及特殊字符用转换成application/x-www-form-urlencoded MIME字符串。

    2、使用URLEncoder.encode编码

    public static String urlEncode(String urlToken) {
        String encoded = null;
        try {
    	    //用URLEncoder.encode方法会把空格变成加号(+),encode之后在替换一下
            encoded = URLEncoder.encode(urlToken, "UTF-8").replace("+", "%20");
        } catch (UnsupportedEncodingException e) {
            logger.error("URLEncode error {}", e);
        }
        return encoded;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3、使用URLEncoder.encode解码

    public static String urlEncode(String urlToken) {
        String decoded = null;
        try {
    	    decoded =URLDecoder.decode(urlToken, "UTF-8"); 
        } catch (UnsupportedEncodingException e) {
            logger.error("URLEncode error {}", e);
        }
        return decoded;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    购物车中的多列表提交订单

    购物车的多级订单的提交和之间分析的完全相同

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

    //转到前台天猫-购物车订单建立页
        @RequestMapping(value = "order/create/byCart", method = RequestMethod.GET)
        public String goToOrderConfirmPageByCart(Map map,
                                                 HttpSession session, HttpServletRequest request,
                                                 @RequestParam(required = false) Integer[] order_item_list) throws UnsupportedEncodingException {
            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";
            }
            if (order_item_list == null || order_item_list.length == 0) {
                logger.warn("用户订单项数组不存在,回到购物车页");
                return "redirect:/cart";
            }
            logger.info("通过订单项ID数组获取订单信息");
            List orderItemList = new ArrayList<>(order_item_list.length);
            for (Integer orderItem_id : order_item_list) {
                orderItemList.add(productOrderItemService.get(orderItem_id));
            }
            logger.info("------检查订单项合法性------");
            if (orderItemList.size() == 0) {
                logger.warn("用户订单项获取失败,回到购物车页");
                return "redirect:/cart";
            }
            //动态检测所有的订单
            for (ProductOrderItem orderItem : orderItemList) {
                if(orderItem.getProductOrderItem_user().getUser_id().equals(userId)==false){
    //            if (orderItem.getProductOrderItem_user().getUser_id() != userId) {
                    logger.warn("用户订单项与用户不匹配,回到购物车页");
                    return "redirect:/cart";
                }
                if (orderItem.getProductOrderItem_order() != null) {
                    logger.warn("用户订单项不属于购物车,回到购物车页");
                    return "redirect:/cart";
                }
            }
            logger.info("验证通过,获取订单项的产品信息");
            double orderTotalPrice = 0.0;
            for (ProductOrderItem orderItem : orderItemList) {
                Product product = productService.get(orderItem.getProductOrderItem_product().getProduct_id());
                product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id()));
                product.setSingleProductImageList(productImageService.getList(product.getProduct_id(), (byte) 0, new PageUtil(0, 1)));
                orderItem.setProductOrderItem_product(product);
                orderTotalPrice += orderItem.getProductOrderItem_price();
            }
            String addressId = "110000";
            String cityAddressId = "110100";
            String districtAddressId = "110101";
            String detailsAddress = null;
            String order_post = null;
            String order_receiver = null;
            String order_phone = null;
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    String cookieName = cookie.getName();
                    String cookieValue = cookie.getValue();
                    switch (cookieName) {
                        case "addressId":
                            addressId = cookieValue;
                            break;
                        case "cityAddressId":
                            cityAddressId = cookieValue;
                            break;
                        case "districtAddressId":
                            districtAddressId = cookieValue;
                            break;
                        case "order_post":
                            order_post = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "order_receiver":
                            order_receiver = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "order_phone":
                            order_phone = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                        case "detailsAddress":
                            detailsAddress = URLDecoder.decode(cookieValue, "UTF-8");
                            break;
                    }
                }
            }
            logger.info("获取省份信息");
            List
    addressList = addressService.getRoot(); logger.info("获取addressId为{}的市级地址信息", addressId); List
    cityAddress = addressService.getList(null, addressId); logger.info("获取cityAddressId为{}的区级地址信息", cityAddressId); List
    districtAddress = addressService.getList(null, cityAddressId); map.put("orderItemList", orderItemList); map.put("addressList", addressList); map.put("cityList", cityAddress); map.put("districtList", districtAddress); map.put("orderTotalPrice", orderTotalPrice); map.put("addressId", addressId); map.put("cityAddressId", cityAddressId); map.put("districtAddressId", districtAddressId); map.put("order_post", order_post); map.put("order_receiver", order_receiver); map.put("order_phone", order_phone); map.put("detailsAddress", detailsAddress); logger.info("转到前台天猫-订单建立页"); return "fore/productBuyPage"; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    断点调试提交两个以上订单

    在这里插入图片描述

    根据前端携带参数的大小创建指定长度的集合,

    根据914和915两个参数动态的将产品信息添加到集合中

    List<ProductOrderItem> orderItemList = new ArrayList<>(order_item_list.length);
    for (Integer orderItem_id : order_item_list) {
        orderItemList.add(productOrderItemService.get(orderItem_id));
    }
    
    • 1
    • 2
    • 3
    • 4

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

    动态的检测该订单是否已经生成了

     //动态检测所有的订单
            for (ProductOrderItem orderItem : orderItemList) {
                if(orderItem.getProductOrderItem_user().getUser_id().equals(userId)==false){
    //            if (orderItem.getProductOrderItem_user().getUser_id() != userId) {
                    logger.warn("用户订单项与用户不匹配,回到购物车页");
                    return "redirect:/cart";
                }
                if (orderItem.getProductOrderItem_order() != null) {
                    logger.warn("用户订单项不属于购物车,回到购物车页");
                    return "redirect:/cart";
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    区别在将信息封装到集合之中

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

    填写完成提交订单

    从前端jsp页面中的点击来提交订单。

    
    //在前端添加判断的条件,根据是购物车还是,直接创建的订单走不同的提交方式
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    payone 和paylist两个函数走的流程相同

    区别在于走的后端请求路径不同

    购物车

    填写信息提交订单

    http://localhost:8080/tmall/order/list

    //创建新订单-多订单项-ajax
        @ResponseBody
        @RequestMapping(value = "order/list", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
        public String createOrderByList(HttpSession session, Map<String, Object> map, HttpServletResponse response,
                                        @RequestParam String addressId,
                                        @RequestParam String cityAddressId,
                                        @RequestParam String districtAddressId,
                                        @RequestParam String productOrder_detail_address,
                                        @RequestParam String productOrder_post,
                                        @RequestParam String productOrder_receiver,
                                        @RequestParam String productOrder_mobile,
                                        @RequestParam String orderItemJSON) throws UnsupportedEncodingException {
            JSONObject object = new JSONObject();
            logger.info("检查用户是否登录");
            Object userId = checkUser(session);
            if (userId == null) {
                object.put("success", false);
                object.put("url", "/login");
                return object.toJSONString();
            }
            JSONObject orderItemMap = JSONObject.parseObject(orderItemJSON);
            Set<String> orderItem_id = orderItemMap.keySet();
            List<ProductOrderItem> productOrderItemList = new ArrayList<>(3);
            if (orderItem_id.size() > 0) {
                for (String id : orderItem_id) {
                    ProductOrderItem orderItem = productOrderItemService.get(Integer.valueOf(id));
                    if (orderItem == null || !orderItem.getProductOrderItem_user().getUser_id().equals(userId)) {
                        logger.warn("订单项为空或用户状态不一致!");
                        object.put("success", false);
                        object.put("url", "/cart");
                        return object.toJSONString();
                    }
                    if (orderItem.getProductOrderItem_order() != null) {
                        logger.warn("用户订单项不属于购物车,回到购物车页");
                        object.put("success", false);
                        object.put("url", "/cart");
                        return object.toJSONString();
                    }
                    boolean yn = productOrderItemService.update(new ProductOrderItem().setProductOrderItem_id(Integer.valueOf(id)).setProductOrderItem_userMessage(orderItemMap.getString(id)));
                    if (!yn) {
                        throw new RuntimeException();
                    }
                    orderItem.setProductOrderItem_product(productService.get(orderItem.getProductOrderItem_product().getProduct_id()));
                    productOrderItemList.add(orderItem);
                }
            } else {
                object.put("success", false);
                object.put("url", "/cart");
                return object.toJSONString();
            }
            logger.info("将收货地址等相关信息存入Cookie中,便于下次使用");
            Cookie[] cookies = new Cookie[]{
                    new Cookie("addressId", addressId),
                    new Cookie("cityAddressId", cityAddressId),
                    new Cookie("districtAddressId", districtAddressId),
                    new Cookie("order_post", URLEncoder.encode(productOrder_post, "UTF-8")),
                    new Cookie("order_receiver", URLEncoder.encode(productOrder_receiver, "UTF-8")),
                    new Cookie("order_phone", URLEncoder.encode(productOrder_mobile, "UTF-8")),
                    new Cookie("detailsAddress", URLEncoder.encode(productOrder_detail_address, "UTF-8"))
            };
            int maxAge = 60 * 60 * 24 * 365;
            for(Cookie cookie : cookies){
                //设置过期时间为一年
                cookie.setMaxAge(maxAge);
                //存储Cookie
                response.addCookie(cookie);
            }
            StringBuffer productOrder_code = new StringBuffer()
                    .append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))
                    .append(0)
                    .append(userId);
            logger.info("生成的订单号为:{}", productOrder_code);
            logger.info("整合订单对象");
            ProductOrder productOrder = new ProductOrder()
                    .setProductOrder_status((byte) 0)
                    .setProductOrder_address(new Address().setAddress_areaId(districtAddressId))
                    .setProductOrder_post(productOrder_post)
                    .setProductOrder_user(new User().setUser_id(Integer.valueOf(userId.toString())))
                    .setProductOrder_mobile(productOrder_mobile)
                    .setProductOrder_receiver(productOrder_receiver)
                    .setProductOrder_detail_address(productOrder_detail_address)
                    .setProductOrder_pay_date(new Date())
                    .setProductOrder_code(productOrder_code.toString());
            Boolean yn = productOrderService.add(productOrder);
            if (!yn) {
                throw new RuntimeException();
            }
            Integer order_id = lastIDService.selectLastID();
            logger.info("整合订单项对象");
            for (ProductOrderItem orderItem : productOrderItemList) {
                orderItem.setProductOrderItem_order(new ProductOrder().setProductOrder_id(order_id));
                yn = productOrderItemService.update(orderItem);
            }
            if (!yn) {
                throw new RuntimeException();
            }
    
            object.put("success", true);
            object.put("url", "/order/pay/" + productOrder.getProductOrder_code());
            return object.toJSONString();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101

    断点调试用户提交多订单的情景

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

    重要的地方在于如何通过Stringbuffer类为每个选项都创建一个订单

    StringBuffer productOrder_code = new StringBuffer()
            .append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))
            .append(0)
            .append(userId);
    logger.info("生成的订单号为:{}", productOrder_code);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过已有的订单项来创建订单

    ProductOrder productOrder = new ProductOrder()
            .setProductOrder_status((byte) 0)
            .setProductOrder_address(new Address().setAddress_areaId(districtAddressId))
            .setProductOrder_post(productOrder_post)
            .setProductOrder_user(new User().setUser_id(Integer.valueOf(userId.toString())))
            .setProductOrder_mobile(productOrder_mobile)
            .setProductOrder_receiver(productOrder_receiver)
            .setProductOrder_detail_address(productOrder_detail_address)
            .setProductOrder_pay_date(new Date())
            .setProductOrder_code(productOrder_code.toString());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    订单的状态为0代表完成下单待支付

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

    四个购物车列表生成了一个订单选项。

    logger.info("整合订单项对象");
    
    • 1

    整个订单对象

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

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

    将四个下单项同时与一个新生成的订单向关联起来

    for (ProductOrderItem orderItem : productOrderItemList) {
        orderItem.setProductOrderItem_order(new ProductOrder().setProductOrder_id(order_id));
        yn = productOrderItemService.update(orderItem);
    }
    
    • 1
    • 2
    • 3
    • 4

    给关联列关联一个订单的值

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

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

    关联的是新生成的570号订单

    之后关联成功以后

    object.put("url", "/order/pay/" + productOrder.getProductOrder_code());
    
    • 1

    跳转订单支付页完成订单支付

    //转到前台天猫-订单支付页
    @RequestMapping(value = "order/pay/{order_code}", method = RequestMethod.GET)
    public String goToOrderPayPage(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() != 0) {
            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);
            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();
        } else {
            for (ProductOrderItem productOrderItem : order.getProductOrderItemList()) {
                orderTotalPrice += productOrderItem.getProductOrderItem_price();
            }
        }
        logger.info("订单总金额为:{}元", orderTotalPrice);
    
        map.put("productOrder", order);
        map.put("orderTotalPrice", orderTotalPrice);
    
        logger.info("转到前台天猫-订单支付页");
        return "fore/productPayPage";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
  • 相关阅读:
    使用Fastmonkey进行iosMonkey测试初探
    [Vue]中数组的操作用法
    <四>理解空间配置器allocator, 优化STL 中的Vector
    lv3 嵌入式开发-4 linux shell命令(文件搜索、文件处理、压缩)
    数据库 第八章
    HTML期末作业,基于html实现中国脸谱传统文化网站设计(5个页面)
    C++异常捕获
    【oj刷题记】【考研写法注意点】【1294】【多项式加法】【利用归并思想】【考研写法关于另外一个可能导致指针越界的问题】【以及遍历链表最保险的写法】
    PPIO边缘云聚焦音视频底层技术,探索元宇宙“登月工程”
    太原理工大学计算机考研资料汇总
  • 原文地址:https://blog.csdn.net/weixin_46167190/article/details/126323307