Home > Hosting > System

This time, I used swoole to delay the order and restore the inventory!

2024-02-18 11:59:50

1. Business scenario: When a customer places an order and fails to pay within the specified time, we need to cancel the order. For example, a good handling method is to use delayed cancellation. Of course, crontab is the first thing that many people think of, and this will do, but here we use swoole's asynchronous millisecond timer to realize it, which will also not affect the operation of the current program.

2. Description: order_status is 1 to confirm the order placed on behalf of the customer, 2 to indicate that the customer has paid, and 0 to indicate that the order has been cancelled (which is what swoole did).

Three, for example, the inventory table csdn_product_stock has 20 products with product ID1 and 40 products with product ID2, and then the customer orders a single product ID 1 minus 10, and the product ID 2 minus 20, so the inventory table is only enough for two orders. In the example, the inventory is automatically restored after 10 seconds.

As shown below, illustrate:

1. The inventory of product ID1 was reduced from 20 to 10 after the first order was placed, and the inventory of product ID2 was reduced from 40 to 20; 2. After the second order is placed, the inventory of product ID is 0, and the inventory of product ID2 is also 0.

3. When placing an order for the third time, the program prompts Out of stock;;

4. After 10 seconds (10 seconds after each order is placed), the customer places an order twice. Because there is no payment (the order_status in the csdn_order table is 1), the inventory of product 1 and product 2 is restored (the order_status in the csdn_order table becomes 0), and the customer can continue to place an order.

1, the required sql database table

DROP TABLE IF EXISTS `csdn_order`; CREATE TABLE `csdn_order` ( `order_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `order_amount` float(10,2) unsigned NOT NULL DEFAULT '0.00', `user_name` varchar(64) CHARACTER SET latin1 NOT NULL DEFAULT '', `order_status` tinyint(2) unsigned NOT NULL DEFAULT '0', `date_created` datetime NOT NULL, PRIMARY KEY (`order_id`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `csdn_order_detail`; CREATE TABLE `csdn_order_detail` ( `detail_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `order_id` int(10) unsigned NOT NULL, `product_id` int(10) NOT NULL, `product_price` float(10,2) NOT NULL, `product_number` smallint(4) unsigned NOT NULL DEFAULT '0', `date_created` datetime NOT NULL, PRIMARY KEY (`detail_id`), KEY `idx_order_id` (`order_id`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `csdn_product_stock`; CREATE TABLE `csdn_product_stock` ( `auto_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `product_id` int(10) NOT NULL, `product_stock_number` int(10) unsigned NOT NULL, `date_modified` datetime NOT NULL, PRIMARY KEY (`auto_id`), KEY `idx_product_id` (`product_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; INSERT INTO `csdn_product_stock` VALUES ('1', '1', '20', '2018-09-13 19:36:19'); INSERT INTO `csdn_product_stock` VALUES ('2', '2', '40', '2018-09-13 19:36:19');

The following is a handmade PHP. Many students use native PHP, so they won't apply it to the framework. In fact, it's all the same. Don't think so complicated. As long as you use too much, you will feel this way.

Configuration file config.php, if this is in the framework, it is basically configured.

Swoole is used in linux system, where you can build your own virtual host or buy your own server online.

Order submission file order_submit.php, where a series of operations are performed on order generation and inventory deduction.

Delayed processing of orders order_cancel.php

For more swoole technical articles, please visit the swoole tutorial column!


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us