[project Construction 12]babasport Order Processing principle and code Implementation.

Source: Internet
Author: User

The previous blog post we have introduced the principle of shopping cart and implementation, then the shopping cart is to submit the order, the way the order is realized? Then let's learn from the following together.

There are several key points to submitting an order:
1, the user must log in
2, Shopping cart must have shopping items
3, the shopping cart in the inventory must be less than inventory
4, Clearing success, clean up the shopping cart (this only cleans up shopping items that have been settled in the Cart)

Next we'll look at the design of the two tables related to the Order:
Order Form

Order Details Form

Here we can find that the order form is similar to our shopping cart, the Order Details table and the shopping items in our shopping cart are very similar. Having understood these principles, we'll look at the implementation of the Code.


Let's write the service layer first: Cartserviceimpl.java

1 @Autowired2     PrivateOrderdao orderdao;3 @Autowired4     PrivateDetaildao detaildao;5     //Save Order6      public voidinsertorder (order order, String Username) {7         //Order ID8Long id = jedis.incr ("oid");9 Order.setid (id);Ten         //User ID oneString Buyerid =Jedis.get (username); a Order.setbuyerid (long.parselong (buyerid)); -Buyercart Buyercart =Selectbuyercartfromredis (username); -list<buyeritem> items =Buyercart.getitems (); the          for(buyeritem Item:items) { - item.setsku (selectskubyid (item.getsku (). getId ())); -         } -         //freight, provided by freight + Order.setdeliverfee (buyercart.getfee ()); -         //Total order amount, provided by the shopping cart + order.settotalprice (buyercart.gettotalprice ()); a         //order amount, provided by the shopping cart at order.setorderprice (buyercart.getproductprice ()); -         //Payment status: 0 to 1 pending payment 2 paid 3 pending refund 4 Refund Success 5 refund failure -         if(order.getpaymentway () = = 1) { -Order.setispaiy (0); -}Else { -             //we have only two options on this page: 0 to pay 1 for payment inOrder.setispaiy (1); -         } to         //Order Status: 0 Submit Order 1 Warehouse Distribution 2 merchandise out of stock 3 waiting for receipt 4 complete 5 to return 6 returned +Order.setorderstate (0); -         //Order generation Time theOrder.setcreatedate (NewDate ()); *         //Generate Order $ orderdao.insertselective (order);Panax Notoginseng          -         //Save Order Details the          for(buyeritem Item:items) { +Detail Detail =NewDetail (); a             //Order Details Form the             /* + * ID: self-growth - * Order id, product id, product name, color Chinese name, size, price, quantity, number of shopping items submitted in cart $              */ $ Detail.setorderid (id); - detail.setproductid (item.getsku (). getproductid ()); - detail.setproductname (item.getsku (). getproduct (). getName ()); the detail.setcolor (item.getsku (). getColor (). getName ()); - detail.setsize (item.getsku (). getsize ());Wuyi detail.setprice (item.getsku (). getprice ()); the detail.setamount (item.getamount ()); -              wu             //Save shopping items in your cart - detaildao.insertselective (detail); about              $             //Reduced Inventory -              -             //empty shopping cart, here only write all delete method, If delete a shopping item need to use Jedis.hdel (); -Jedis.del ("buyercart:" +username); a         } +}

Here is the use of Redis to generate the primary key id, we have compared the Redis generated primary key and the database generated primary key and UUID comparison, here is no longer elaborate.
The rest is to pick up the shopping cart, where we can go through username to remove skuId and purchase quantity amount, because the table name we stored in Redis is "buyercart:" +username is the key: skuId, value is amount .
The rest is to assemble the contents of the cart into Order.

Then look at the controller layer code: Cartcontroller.java:

1 //to settle2@RequestMapping (value= "/buyer/truebuy")3      publicString truebuy (string[] skuids, model model, httpservletrequest request, httpservletresponse Response) {4         //1, The shopping cart must have goods,5         //Remove the user name and remove the shopping cart6String username =sessionproviderservice.getattributterforusername (requestutils.getcsessionid (request, response));7         //Remove all Shopping carts8Buyercart Buyercart =cartservice.selectbuyercartfromredisbyskuids (skuids, username);9list<buyeritem> items =Buyercart.getitems ();Ten         if(items.size () > 0) { one             //there is a product in the shopping cart a             //determine if the selected items are in stock, and if there is one item that is not in stock, refresh the Page. -Boolean flag =true; -             //2, the goods in the shopping cart must be in stock and purchased larger than the stock Quantity. tip: The original page of the shopping cart is not moving. the goods changed to no-goods, accents reminder. the              for(buyeritem Buyeritem:items) { -                 //Shopping items filled with shopping cart, the current shopping item only skuid this one thing, we also need the number of shopping items to determine whether there are goods - buyeritem.setsku (cartservice.selectskubyid (buyeritem.getsku (). getId ())); -                 //Check Inventory +                 if(buyeritem.getamount () >Buyeritem.getsku (). Getstock ()) { -                     //No goods +Buyeritem.setishave (false); aFlag =false; at                 } -                 if(!Flag) { -                     //no goods, The original page does not move, there is goods changed to no goods, refresh the Page. -Model.addattribute ("buyercart", buyercart); -                     return"cart"; -                 } in             } -}Else { to             //Shopping Cart No items +             //no product: 1> original Shopping cart page refresh (shopping cart page tip no Items) -             return"redirect:/shopping/tocart"; the         } *          $         Panax Notoginseng         //3, normal access to the next page -         return"order"; the     } +      a      the     //Submit Order +@RequestMapping (value= "/buyer/submitorder") -      publicString submitorder (order order, httpservletrequest request, httpservletresponse Response) { $String username =sessionproviderservice.getattributterforusername (requestutils.getcsessionid (request, response)); $          -         //Save Order - Cartservice.insertorder (order, username); the          -         return"success";Wuyi}

A blog post on the shopping cart we have already said that for "/buyer/" the format of the request of our SPRINGMVC will intercept, the form of interception is to determine whether the user is logged in. We will not explain it in detail here.
This is to determine whether the quantity in the shopping item is larger than the inventory, and if it is larger than the inventory, refresh the shopping cart page and show the item is not in Stock.
The remaining commit order is to save order to the order form, which also contains the save shopping details to the order Details Table.

As a whole, the order of this link is quite simple, it is necessary to note that the order form and order Details table DESIGN. But the code above also has a lot of imperfections, such as reducing inventory and clearing the shopping cart Options.  Of course, This is just a practice project, the whole project is done here, and later on I will upload the code and all the information to the Internet for everyone to use, of course, The information also contains 12 days of video Commentary. I hope you can study together in your spare time.

The content is probably so much, thanks for reading this series and replying to the Friends of the Park.



[project Construction 12]babasport Order Processing principle and code Implementation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.