Complete e-commerce project-(8) Commodity order module (1): order settlement and mysql transaction

Order settlement page

  • Position: After selecting a good product in a shopping cart, you can click on to the settlement to enter into this settlement page
    The fastdfs service is not called, so the picture cannot be loaded

  • The most critical point here is:

    • When it comes to money transactions, the accuracy of the data must be guaranteed!
    • We can't believe the commodity price information passed by the front end, but we should calculate the price based on the commodity id checked in redis.
  • There is nothing else to say, the data needed by the front-end can be passed from the back-end

    • (1) User address information
    • (2) Choice of payment method
      • Payment method, we directly choose 0,1 to represent that payment method, there are many ways
    • (3) Product information
    • (4) Freight: This is a complicated place (but we don't have a freight system, so no matter what you buy, the freight is defaulted to ten yuan )
      Insert picture description here

Focus: Submit an order

Create order data table

Precautions:

  • (1) The order number no longer uses the database auto-incrementing primary key, but is generated by the backend.
  • (2) There can be multiple product information in an order, and the basic information of the order and the order product information are in a one-to-many relationship

Order form introduction

Insert picture description here

  • Generally, product reviews are only available after the order is completed. This is very realistic

Submit order data:

  • Only need (1) order address
  • (2) Payment method
  • The rest can be obtained through the backend. For example, the total price and which products are purchased can be obtained from the data sheet or redis.
    Insert picture description here
  • This is the address when you click to submit the order, and you can see that we only passed two pieces of data.
    Insert picture description here

Save the data processing operations in the order (the business logic is complex, the code is not difficult, I have written all of them)

  • (1) Create the basic information object of the order
  • (2) Query the selected shopping cart data
  • (3) Query sku product objects
    • 3.1 Judging the product inventory, and prompting if it is insufficient
    • 3.2 Inventory sufficient to continue execution
    • 3.3 Modify the inventory and sales of goods
    • 3.4 Create Order Item Table Object
  • (4) Delete the selected product data of the shopping cart

The first is data verification and loading

		# 接收
        dict1 = json.loads(request.body.decode())
        address_id = dict1.get('address_id')
        pay_method = dict1.get('pay_method')
        pay_method = int(pay_method)
        # 验证
        if not all([address_id, pay_method]):
            return http.JsonResponse({
    
    'code': RETCODE.PARAMERR, 'errmsg': '参数不完整'})
        # 收货地址
        try:
            address = Address.objects.get(pk=address_id, is_delete=False, user_id=request.user.id)
        except:
            return http.JsonResponse({
    
    'code': RETCODE.PARAMERR, 'errmsg': '收货地址无效'})
        # 支付方式
        if pay_method not in [1, 2]:
            return http.JsonResponse({
    
    'code': RETCODE.PARAMERR, 'errmsg': '支付方式无效'})

        # 处理(业务最多)
        user = request.user
        now = datetime.now()
        # 1.查询购物车选中的商品
        redis_cli = get_redis_connection('cart')
        # 下面是从redis中拿出商品数据
        # 从hash中读取商品编号、数量
        cart_dict_bytes = redis_cli.hgetall('cart%d' % request.user.id)
        cart_dict_int = {
    
    int(sku_id): int(count) for sku_id, count in cart_dict_bytes.items()}
        # 从set中读取选中的商品编号
        cart_selected_bytes = redis_cli.smembers('selected%d' % request.user.id)
        cart_selected_int = [int(sku_id) for sku_id in cart_selected_bytes]

Next is the specific business processing

When constructing the order object, we use the current time to create an order number.

Insert picture description here

The following is the business processing code, we need to use mysql transaction

  • Because the judgment of commodity inventory and the creation of order objects must be successful and failed at the same time. We need to use transactions.
  • Clear : Django itself does not provide transaction functions! The database used must have transaction capabilities . The transaction management function provided by Django itself still calls the interface method of the bottom layer of the database.
  • Documentation on mysql transactions in django
First, let's talk about django automatic submission

Insert picture description here

Insert picture description here
This is that we don't need to manually commit to submit data by ourselves, django will manage it for us.

Call the original method and manually manage the transaction

Insert picture description here

Insert picture description here

  • To open the transaction, we need to use:
    Insert picture description here
    the logic behind here also involves pessimistic locking, optimistic locking, and transaction isolation level. Let's get here first, and follow up.

Guess you like

Origin blog.csdn.net/pythonstrat/article/details/108184741