argument('activity_id'); $dateStr = $this->argument('date') ? Carbon::parse((string) $this->argument('date'))->toDateString() : now()->toDateString(); $count = max(1, (int) $this->option('count')); $dryRun = (bool) $this->option('dry-run'); $activity = Activity::query()->find($activityId); if (!$activity) { $this->error("未找到活动 id={$activityId}"); return self::FAILURE; } $this->info("活动 #{$activity->id} {$activity->title}"); $this->line("活动日:{$dateStr},生成 {$count} 条".($dryRun ? '(dry-run)' : '')); if ($dryRun) { return self::SUCCESS; } $created = 0; DB::transaction(function () use ($activity, $dateStr, $count, &$created) { $day = Carbon::parse($dateStr)->startOfDay(); $activityDay = ActivityDay::query() ->where('activity_id', $activity->id) ->whereDate('activity_date', $dateStr) ->first(); if (!$activityDay) { $opensAt = $day->copy()->subDays(7)->setTime(9, 0, 0); $activityDay = ActivityDay::create([ 'activity_id' => $activity->id, 'activity_date' => $dateStr, 'day_quota' => max($count + 20, 50), 'booked_count' => 0, 'opens_at' => $opensAt, ]); $this->line("已创建活动日场次 activity_day_id={$activityDay->id}"); } $ticketEach = 1; $needBooked = $activityDay->booked_count + ($count * $ticketEach); if ($activityDay->day_quota < $needBooked) { $activityDay->day_quota = $needBooked + 10; $activityDay->save(); $this->line("已调高放票数至 {$activityDay->day_quota}"); } for ($i = 1; $i <= $count; $i++) { $phone = $this->makeUniquePhone($activity->id, $activityDay->id, $i); $res = Reservation::create([ 'venue_id' => $activity->venue_id, 'activity_id' => $activity->id, 'activity_day_id' => $activityDay->id, 'visitor_name' => "报名用户{$dateStr}-{$i}", 'visitor_phone' => $phone, 'qr_token' => (string) Str::uuid(), 'status' => 'pending', 'ticket_count' => $ticketEach, 'reservation_source' => 'legacy', ]); $created++; $activityDay->increment('booked_count', $ticketEach); $this->line(" + #{$res->id} {$phone}"); } Activity::refreshRegisteredCountFromReservations($activity->id); }); $this->info("完成:新建 {$created} 条待核销预约;registered_count 已按已预约人数合计更新。"); return self::SUCCESS; } private function makeUniquePhone(int $activityId, int $activityDayId, int $i): string { $n = ($activityId * 100000 + $activityDayId * 1000 + $i) % 100000000; return '139'.str_pad((string) $n, 8, '0', STR_PAD_LEFT); } }