Daniel Supernault 2 тижнів тому
батько
коміт
bbc7f7b0d4

+ 1 - 6
app/Console/Commands/TransformImports.php

@@ -163,12 +163,7 @@ class TransformImports extends Command
                         throw new \Exception("Could not generate unique creation_id for ImportPost ID {$ip->id}");
                     }
 
-                    $uid = str_pad($id, 6, 0, STR_PAD_LEFT);
-                    $yearStr = str_pad($uniqueIdData['year'], 2, 0, STR_PAD_LEFT);
-                    $monthStr = str_pad($uniqueIdData['month'], 2, 0, STR_PAD_LEFT);
-                    $dayStr = str_pad($uniqueIdData['day'], 2, 0, STR_PAD_LEFT);
-                    $zone = $yearStr.$monthStr.$dayStr.str_pad($uniqueIdData['incr'], 3, 0, STR_PAD_LEFT);
-                    $statusId = '1'.$uid.$zone;
+                    $statusId = $uniqueIdData['status_id'];
 
                     $status = new Status;
                     $status->profile_id = $pid;

+ 36 - 11
app/Services/ImportService.php

@@ -192,23 +192,48 @@ class ImportService
             }
 
             $maxExistingIncr = $query->max('creation_id') ?? 0;
+
             $incr = $maxExistingIncr + 1;
 
-            if ($incr > 999) {
-                [$newYear, $newMonth, $newDay] = self::getNextValidDate($year, $month, $day);
-                if (! $newYear) {
-                    return null;
+            while ($incr <= 999) {
+                $uid = str_pad($userId, 6, 0, STR_PAD_LEFT);
+                $yearStr = str_pad($year, 2, 0, STR_PAD_LEFT);
+                $monthStr = str_pad($month, 2, 0, STR_PAD_LEFT);
+                $dayStr = str_pad($day, 2, 0, STR_PAD_LEFT);
+                $zone = $yearStr.$monthStr.$dayStr.str_pad($incr, 3, 0, STR_PAD_LEFT);
+                $statusId = '1'.$uid.$zone;
+
+                $statusExists = DB::table('statuses')->where('id', $statusId)->exists();
+
+                $importExists = ImportPost::where('user_id', $userId)
+                    ->where('creation_year', $year)
+                    ->where('creation_month', $month)
+                    ->where('creation_day', $day)
+                    ->where('creation_id', $incr)
+                    ->when($excludeImportPostId, function ($q) use ($excludeImportPostId) {
+                        return $q->where('id', '!=', $excludeImportPostId);
+                    })
+                    ->exists();
+
+                if (! $statusExists && ! $importExists) {
+                    return [
+                        'incr' => $incr,
+                        'year' => $year,
+                        'month' => $month,
+                        'day' => $day,
+                        'status_id' => $statusId,
+                    ];
                 }
 
-                return self::getUniqueCreationId($userId, $newYear, $newMonth, $newDay, $excludeImportPostId);
+                $incr++;
             }
 
-            return [
-                'incr' => $incr,
-                'year' => $year,
-                'month' => $month,
-                'day' => $day,
-            ];
+            [$newYear, $newMonth, $newDay] = self::getNextValidDate($year, $month, $day);
+            if (! $newYear) {
+                return null;
+            }
+
+            return self::getUniqueCreationId($userId, $newYear, $newMonth, $newDay, $excludeImportPostId);
         }, 3);
     }