shish 7 månader sedan
förälder
incheckning
7911d37dda
1 ändrade filer med 32 tillägg och 3 borttagningar
  1. 32 3
      biz-hd/custom/classes/CustomClass.php

+ 32 - 3
biz-hd/custom/classes/CustomClass.php

@@ -714,8 +714,23 @@ class CustomClass extends BaseClass
                 $data['birthdayDate'] = $birthdayDate;
                 $data['lunar'] = $lunar;
             }
-            $custom = self::addCustom($data);
-            $isNewCustom = 1;
+            // 处理并发插入:尝试插入,如果遇到唯一键冲突则重新查询
+            try {
+                $custom = self::addCustom($data);
+                $isNewCustom = 1;
+            } catch (\yii\db\IntegrityException $e) {
+                // 唯一键冲突,说明其他并发请求已经插入,重新查询
+                if (strpos($e->getMessage(), 'Duplicate entry') !== false) {
+                    $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
+                    if (empty($custom)) {
+                        // 如果仍然查不到,则抛出异常
+                        throw $e;
+                    }
+                    $isNewCustom = 0;
+                } else {
+                    throw $e;
+                }
+            }
         }
         $customId = $custom->id ?? 0;
         $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
@@ -740,7 +755,21 @@ class CustomClass extends BaseClass
                 'long' => $shop->long ?? '',
                 'salt' => $salt,
             ];
-            $hd = HdClass::addData($hdData);
+            // 处理并发插入:尝试插入,如果遇到唯一键冲突则重新查询
+            try {
+                $hd = HdClass::addData($hdData);
+            } catch (\yii\db\IntegrityException $e) {
+                // 唯一键冲突,说明其他并发请求已经插入,重新查询
+                if (strpos($e->getMessage(), 'Duplicate entry') !== false) {
+                    $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
+                    if (empty($hd)) {
+                        // 如果仍然查不到,则抛出异常
+                        throw $e;
+                    }
+                } else {
+                    throw $e;
+                }
+            }
             $isNewCustom = 1;
         }
         $hdId = $hd->id ?? 0;