瀏覽代碼

去掉事物

Tree 2 天之前
父節點
當前提交
e29786e9b8
共有 1 個文件被更改,包括 41 次插入48 次删除
  1. 41 48
      app/Console/Commands/SuperballUpdatePoolAndStats.php

+ 41 - 48
app/Console/Commands/SuperballUpdatePoolAndStats.php

@@ -50,54 +50,47 @@ class SuperballUpdatePoolAndStats extends Command
             // 奖池 = 总流水的 1/10(内部单位),向下取整
             $poolAmount = (int)floor($flowing / 4);
 
-            // 2. 确保当日 superball_daily 记录存在
-            DB::connection('write')->transaction(function () use ($dateStr, $poolAmount, $today) {
-                // getOrCreateDaily 已经保证有一条记录(带 lucky_number),此处再加锁更新
-                $daily = $this->service->getOrCreateDaily($dateStr);
-
-                // 3. 计算本次要增加的 completed_count 和 total_balls
-                $hour = (int)$today->format('G'); // 0-23
-
-                $hour = intval(date('H'));
-                if ($hour < 2) {
-                    // 00:00 - 00:59
-                    $completedInc = mt_rand(5, 10);
-                    $multipliers = [1, 2, 3, 6];
-                } else if($hour<=10){
-                    // 01:00 以后
-                    $completedInc = mt_rand(10, 15);
-                    $multipliers = [6,10];
-                } else {
-                    // 01:00 以后
-                    $completedInc = mt_rand(15, 20);
-                    $multipliers = [10,15];
-                }
-
-                // 用昨日注册用户数 / 500 作为系数,重新调整 completedInc
-                $yesterdayDateId = Carbon::yesterday()->format('Ymd');
-                $yesterdayRegCount = (int) DB::connection('read')
-                    ->table('QPRecordDB.dbo.RecordPlatformData')
-                    ->where('DateID', $yesterdayDateId)
-                    ->where('Channel', -1)
-                    ->value('RegPeple');
-                $regFactor = $yesterdayRegCount > 0 ? ($yesterdayRegCount / 500) : 0.1;
-                $regFactor = max(1,$regFactor);
-                $completedInc = (int) ($completedInc * $regFactor);
-
-                $multiplier = $multipliers[array_rand($multipliers)];
-                $ballsInc = $completedInc * $multiplier;
-
-                DB::connection('write')->table(TableName::agent() . 'superball_daily')
-                    ->where('pool_date', $dateStr)
-                    ->update([
-                        'pool_amount' => $poolAmount,
-                        'completed_count' => DB::raw("completed_count + {$completedInc}"),
-                        'total_balls' => DB::raw("total_balls + {$ballsInc}"),
-                        'updated_at' => now()->format('Y-m-d H:i:s'),
-                    ]);
-
-                \Log::info("Superball pool ###$completedInc###$ballsInc");
-            });
+            // 2. 确保当日 superball_daily 记录存在(原子 MERGE,不持长锁)
+            $this->service->getOrCreateDaily($dateStr);
+
+            // 3. 计算本次要增加的 completed_count 和 total_balls
+            $hour = intval(date('H'));
+            if ($hour < 2) {
+                $completedInc = mt_rand(5, 10);
+                $multipliers = [1, 2, 3, 6];
+            } else if ($hour <= 10) {
+                $completedInc = mt_rand(10, 15);
+                $multipliers = [6, 10];
+            } else {
+                $completedInc = mt_rand(15, 20);
+                $multipliers = [10, 15];
+            }
+
+            // 用昨日注册用户数 / 500 作为系数,重新调整 completedInc
+            $yesterdayDateId = Carbon::yesterday()->format('Ymd');
+            $yesterdayRegCount = (int) DB::connection('read')
+                ->table('QPRecordDB.dbo.RecordPlatformData')
+                ->where('DateID', $yesterdayDateId)
+                ->where('Channel', -1)
+                ->value('RegPeple');
+            $regFactor = $yesterdayRegCount > 0 ? ($yesterdayRegCount / 500) : 0.1;
+            $regFactor = max(1, $regFactor);
+            $completedInc = (int) ($completedInc * $regFactor);
+
+            $multiplier = $multipliers[array_rand($multipliers)];
+            $ballsInc = $completedInc * $multiplier;
+
+            // 单条 UPDATE 原子累加,不需要事务包裹
+            DB::connection('write')->table(TableName::agent() . 'superball_daily')
+                ->where('pool_date', $dateStr)
+                ->update([
+                    'pool_amount' => $poolAmount,
+                    'completed_count' => DB::raw("completed_count + {$completedInc}"),
+                    'total_balls' => DB::raw("total_balls + {$ballsInc}"),
+                    'updated_at' => now()->format('Y-m-d H:i:s'),
+                ]);
+
+            \Log::info("Superball pool ###$completedInc###$ballsInc");
 
             \Log::info("Superball pool stats updated for {$dateStr}, pool_amount={$poolAmount}"."--");
             return true;