建站基础,儿童网站设计模板,PHP网站开发都需要学什么,百度怎么注册公司网站文章正文一、业务场景#xff1a;当客户下单在指定的时间内如果没有付款#xff0c;那我们需要将这笔订单取消掉#xff0c;比如好的处理方法是运用延时取消#xff0c;很多人首先想到的当然是crontab#xff0c;这个也行#xff0c;不过这里我们运用swoole的异步毫秒定时…文章正文一、业务场景当客户下单在指定的时间内如果没有付款那我们需要将这笔订单取消掉比如好的处理方法是运用延时取消很多人首先想到的当然是crontab这个也行不过这里我们运用swoole的异步毫秒定时器来实现同样也不会影响到当前程序的运行具体可以参考https://wiki.swoole.com/wiki/page/319.html二、说明order_status为1时代表客户下单确定为2时代表客户已付款为0时代表订单已取消(正是swoole来做的)三、举例说明库存表csdn_product_stock产品ID为1的产品库存数量为20产品ID为2的库存数量为40然后客户下单一笔产品ID1减10产品ID2减20所以库存表只够2次下单例子中10秒后自动还原库存如下图图解1、第一次下完单产品ID1库存从20减到了10产品ID2库存从40减到了202、第二次下完单产品ID的库存为0了产品ID2的库存也为0了3、第三次下单时程序提示Out of stock4、过了10秒钟(每个订单下单后往后推10秒)客户两次下单由于没有付款(csdn_order表的order_status为1)产品1和产品2的库存被还原了(csdn_order表的order_status变为0)客户又可以继续下单了1、所需要sql数据库表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)) ENGINEInnoDB AUTO_INCREMENT0 DEFAULT CHARSETutf8;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)) ENGINEInnoDB AUTO_INCREMENT0 DEFAULT CHARSETutf8;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)) ENGINEInnoDB AUTO_INCREMENT3 DEFAULT CHARSETutf8;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);下面贴出来纯手工PHP很多同学用了原生PHP就不会运用到框架里去其实都一样的不要想得那么复杂就是了。只要一点就是你用多了你就会这样觉得咯。配置文件config.php 这个在框架的话基本上都是配置好了。?php $dbHost 192.168.23.110;$dbUser root;$dbPassword 123456;$dbName test;?swoole都是用在linux系统里的这里的host你可以自己搭建虚拟主机也可以网上购买属于自己的服务器订单提交的文件order_submit.php这里对订单生成同时扣除库存的一系列操作?phprequire (config.php);try { $pdo new PDO(mysql:host . $dbHost . ;dbname . $dbName, $dbUser, $dbPassword, array(PDO::ATTR_PERSISTENT true)); $pdo-setAttribute(PDO::ATTR_AUTOCOMMIT, 1); $pdo-setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $orderInfo array( order_amount 10.92, user_name yusan, order_status 1, date_created now(), product_lit array( 0 array( product_id 1, product_price 5.00, product_number 10, date_created now() ), 1 array( product_id 2, product_price 5.92, product_number 20, date_created now() ) ) ); try{ $pdo-beginTransaction();//开启事务处理 $sql insert into csdn_order (order_amount, user_name, order_status, date_created) values (:orderAmount, :userName, :orderStatus, now()); $stmt $pdo-prepare($sql); $affectedRows $stmt-execute(array(:orderAmount $orderInfo[order_amount], :userName $orderInfo[user_name], :orderStatus $orderInfo[order_status])); $orderId $pdo-lastInsertId(); if(!$affectedRows) { throw new PDOException(Failure to submit order!); } foreach($orderInfo[product_lit] as $productInfo) { $sqlProductDetail insert into csdn_order_detail (order_id, product_id, product_price, product_number, date_created) values (:orderId, :productId, :productPrice, :productNumber, now()); $stmtProductDetail $pdo-prepare($sqlProductDetail); $stmtProductDetail-execute(array(:orderId $orderId, :productId $productInfo[product_id], :productPrice $productInfo[product_price], :productNumber $productInfo[product_number])); $sqlCheck select product_stock_number from csdn_product_stock where product_id:productId; $stmtCheck $pdo-prepare($sqlCheck); $stmtCheck-execute(array(:productId $productInfo[product_id])); $rowCheck $stmtCheck-fetch(PDO::FETCH_ASSOC); if($rowCheck[product_stock_number] $productInfo[product_number]) { throw new PDOException(Out of stock, Failure to submit order!); } $sqlProductStock update csdn_product_stock set product_stock_numberproduct_stock_number-:productNumber, date_modifiednow() where product_id:productId; $stmtProductStock $pdo-prepare($sqlProductStock); $stmtProductStock-execute(array(:productNumber $productInfo[product_number], :productId $productInfo[product_id])); $affectedRowsProductStock $stmtProductStock-rowCount(); //库存没有正常扣除失败库存表里的product_stock_number设置了为非负数 //如果库存不足时sql异常SQLSTATE[22003]: Numeric value out of range: 1690 BIGINT UNSIGNED value is out of range in (test.csdn_product_stock.product_stock_number - 20) if($affectedRowsProductStock 0) { throw new PDOException(Out of stock, Failure to submit order!); } } echo Successful, Order Id is . $orderId .Order Amount is . $orderInfo[order_amount] . 。; $pdo-commit();//提交事务 //exec(php order_cancel.php -a . $orderId . ); pclose(popen(php order_cancel.php -a . $orderId . , w)); //system(php order_cancel.php -a . $orderId .