Bladeren bron

零售购买超限购后,能回滚缓存

shizhongqi 4 maanden geleden
bovenliggende
commit
74e7822904
2 gewijzigde bestanden met toevoegingen van 56 en 0 verwijderingen
  1. 16 0
      app-mall/controllers/OrderController.php
  2. 40 0
      biz-hd/product/classes/ProductClass.php

+ 16 - 0
app-mall/controllers/OrderController.php

@@ -273,6 +273,17 @@ class OrderController extends BaseController
 
         $connection = Yii::$app->db;
         $transaction = $connection->beginTransaction();
+        $transactionFinished = false;
+        register_shutdown_function(function () use (&$transactionFinished, $transaction) {
+            if ($transactionFinished) {
+                return;
+            }
+            \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
+            if ($transaction->isActive) {
+                $transaction->rollBack();
+            }
+        });
+
         try {
             $orderValidTime = !getenv('ORDER_VALID_TIME') ? 600 : getenv('ORDER_VALID_TIME');
             $post['deadline'] = time() + $orderValidTime;
@@ -480,6 +491,9 @@ class OrderController extends BaseController
 
             $transaction->commit();
 
+            $transactionFinished = true;
+            \bizHd\product\classes\ProductClass::clearLimitBuyRollbackSnapshot();
+
             $orderSn = $return->orderSn ?? '';
             $orderPrice = $return->orderPrice ?? 0;
             $id = $return->id ?? 0;
@@ -487,6 +501,8 @@ class OrderController extends BaseController
             util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
         } catch (\Exception $e) {
             $transaction->rollBack();
+            \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
+            $transactionFinished = true;
             Yii::error("失败原因:" . $e->getMessage());
             util::fail('下单失败');
         }

+ 40 - 0
biz-hd/product/classes/ProductClass.php

@@ -646,4 +646,44 @@ class ProductClass extends BaseClass
         return true;
     }
 
+    /**
+     * 回滚当前请求中已记录的限购缓存变更
+     *
+     * @return bool
+     */
+    public static function rollbackLimitBuySnapshot()
+    {
+        $snapshotList = Yii::$app->params[self::LIMIT_BUY_ROLLBACK_SNAPSHOT_KEY] ?? [];
+        if (empty($snapshotList) || !is_array($snapshotList)) {
+            return true;
+        }
+        foreach ($snapshotList as $snapshot) {
+            $productId = intval($snapshot['productId'] ?? 0);
+            $customId = intval($snapshot['customId'] ?? 0);
+            if ($productId <= 0 || $customId <= 0) {
+                continue;
+            }
+            $limitKey = self::LIMIT_BUY_KEY . $productId;
+            $hasOldValue = intval($snapshot['hasOldValue'] ?? 0);
+            if ($hasOldValue == 1) {
+                $oldValue = $snapshot['oldValue'] ?? '0';
+                Yii::$app->redis->executeCommand('HSET', [$limitKey, $customId, $oldValue]);
+            } else {
+                Yii::$app->redis->executeCommand('HDEL', [$limitKey, $customId]);
+            }
+        }
+        self::clearLimitBuyRollbackSnapshot();
+        return true;
+    }
+
+    /**
+     * 清理当前请求中记录的限购回滚快照
+     *
+     * @return void
+     */
+    public static function clearLimitBuyRollbackSnapshot()
+    {
+        unset(Yii::$app->params[self::LIMIT_BUY_ROLLBACK_SNAPSHOT_KEY]);
+    }
+
 }