老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)

利用swoole實(shí)現(xiàn)訂單延時(shí)

本篇內(nèi)容介紹了“利用swoole實(shí)現(xiàn)訂單延時(shí)”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)公司成立于2013年,先為余干等服務(wù)建站,余干等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為余干企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

一、業(yè)務(wù)場景:當(dāng)客戶下單在指定的時(shí)間內(nèi)如果沒有付款,那我們需要將這筆訂單取消掉,比如好的處理方法是運(yùn)用延時(shí)取消,很多人首先想到的當(dāng)然是crontab,這個(gè)也行,不過這里我們運(yùn)用swoole的異步毫秒定時(shí)器來實(shí)現(xiàn),同樣也不會影響到當(dāng)前程序的運(yùn)行。

二、說明,order_status為1時(shí)代表客戶下單確定,為2時(shí)代表客戶已付款,為0時(shí)代表訂單已取消(正是swoole來做的)

三、舉例說明,庫存表csdn_product_stock產(chǎn)品ID為1的產(chǎn)品庫存數(shù)量為20,產(chǎn)品ID為2的庫存數(shù)量為40,然后客戶下單一筆產(chǎn)品ID1減10,產(chǎn)品ID2減20,所以庫存表只夠2次下單,例子中10秒后自動還原庫存。

如下圖,圖解:

1、第一次下完單產(chǎn)品ID1庫存從20減到了10,產(chǎn)品ID2庫存從40減到了20;2、第二次下完單產(chǎn)品ID的庫存為0了,產(chǎn)品ID2的庫存也為0了,

3、第三次下單時(shí),程序提示Out of stock;

4、過了10秒鐘(每個(gè)訂單下單后往后推10秒),客戶兩次下單,由于沒有付款(csdn_order表的order_status為1),產(chǎn)品1和產(chǎn)品2的庫存被還原了(csdn_order表的order_status變?yōu)?),客戶又可以繼續(xù)下單了

1、所需要sql數(shù)據(jù)庫表

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');

下面貼出來純手工PHP,很多同學(xué)用了原生PHP,就不會運(yùn)用到框架里去,其實(shí)都一樣的,不要想得那么復(fù)雜就是了。只要一點(diǎn)就是你用多了,你就會這樣覺得咯。

配置文件config.php  ,這個(gè)在框架的話,基本上都是配置好了。

swoole都是用在linux系統(tǒng)里的,這里的host你可以自己搭建虛擬主機(jī),也可以網(wǎng)上購買屬于自己的服務(wù)器

訂單提交的文件order_submit.php,這里對訂單生成,同時(shí)扣除庫存的一系列操作

 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();//開啟事務(wù)處理
        $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_number=product_stock_number-:productNumber, date_modified=now() 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設(shè)置了為非負(fù)數(shù)
            //如果庫存不足時(shí),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();//提交事務(wù)
        //exec("php order_cancel.php -a" . $orderId . " &");
        pclose(popen('php order_cancel.php -a ' . $orderId . ' &', 'w'));
        //system("php order_cancel.php -a" . $orderId . " &", $phpResult);
        //echo $phpResult;
    }catch(PDOException $e){
        echo $e->getMessage();
        $pdo->rollback();
    }
    $pdo = null;
} catch (PDOException $e) {
    echo $e->getMessage();
}
?>

訂單的延時(shí)處理order_cancel.php

 true));
    $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    swoole_timer_after(10000, function ($queryString) {
        global $queryString, $pdo;
        try{
            $pdo->beginTransaction();//開啟事務(wù)處理
            $orderId = $queryString['a'];  
            $sql = "select order_status from csdn_order where order_id=:orderId";  
            $stmt = $pdo->prepare($sql);  
            $stmt->execute(array(':orderId' => $orderId));  
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            //$row['order_status'] === "1"代表已下單,但未付款,我們還原庫存只針對未付款的訂單
            if(isset($row['order_status']) && $row['order_status'] === "1") {
                $sqlOrderDetail = "select product_id, product_number from csdn_order_detail where order_id=:orderId";  
                $stmtOrderDetail = $pdo->prepare($sqlOrderDetail);  
                $stmtOrderDetail->execute(array(':orderId' => $orderId));  
                while($rowOrderDetail = $stmtOrderDetail->fetch(PDO::FETCH_ASSOC)) {
                    $sqlRestoreStock = "update csdn_product_stock set product_stock_number=product_stock_number + :productNumber, date_modified=now() where product_id=:productId";  
                    $stmtRestoreStock = $pdo->prepare($sqlRestoreStock);
                    $stmtRestoreStock->execute(array(':productNumber' => $rowOrderDetail['product_number'], ':productId' => $rowOrderDetail['product_id']));
                }
                $sqlRestoreOrder = "update csdn_order set order_status=:orderStatus where order_id=:orderId";  
                $stmtRestoreOrder = $pdo->prepare($sqlRestoreOrder);
                $stmtRestoreOrder->execute(array(':orderStatus' => 0, ':orderId' => $orderId));
            }
            $pdo->commit();//提交事務(wù)
        }catch(PDOException $e){
            echo $e->getMessage();
            $pdo->rollback();
        }
        $pdo = null;
        appendLog(date("Y-m-d H:i:s") . "\t" . $queryString['a'] . "\t" . "end\t" . json_encode($queryString));
    }, $pdo);
} catch (PDOException $e) {
    echo $e->getMessage();
}
function appendLog($str) {
    $dir = 'log.txt';
    $fh = fopen($dir, "a");
    fwrite($fh, $str . "\n");
    fclose($fh);
}
?>

“利用swoole實(shí)現(xiàn)訂單延時(shí)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!


文章標(biāo)題:利用swoole實(shí)現(xiàn)訂單延時(shí)
文章網(wǎng)址:http://www.xueling.net.cn/article/jgdshs.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP
主站蜘蛛池模板: 成人免费视频在线观看 | 国内自拍视频在线播放 | 久夜蜜汁av玖潮碰撩尤物 | 国产一区精品福利 | 色午夜日本高清视频WWW | 性色AV一区二区三区V视界影院 | 美日韩精品一区二区三区 | 欧美性少妇xxxx极品高清hd | 亚洲欧美在线播放 | 老熟妇高潮一区二区三区 | 免费播放黄色片 | JIZZJIZZ亚洲日本少妇 | 人一级毛片 | 精品亚洲国产视频 | 人成免费网站 | 欧美成人一级 | 精品国产91久久久久久久妲己 | 久久免费视频播放 | 亚洲午夜精品久久久久久性色 | 免费av国产 | a在线播放 | 91嫩草在线视频 | 丰满少妇熟乱XXXXX视频 | 亚洲欧洲精品视频在线观看 | 新婚少妇在线观看一区 | 成人免费黄色网页 | 国产精品国 | 婷婷丁香亚洲色综合91 | 国产91视频一区 | 又爽又高潮的BB视频免费看 | 亚洲国产av无码精品果冻传媒 | 免费在线观看一区 | 精品亚洲国产成人av不卡 | 日韩欧美在线视频一区 | luxu在线| 久久人人做人人爽人人AV | 成人18夜夜网深夜福利网 | 在线看播放免费网站 | 成人91在线 | 日韩人妻无码一区二区三区综合部 | 午夜a级片|