|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Models\Activity;
|
|
|
|
|
|
use App\Models\ActivityDay;
|
|
|
|
|
|
use App\Models\Reservation;
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 在指定活动日的场次下批量生成测试预约(默认「今天」,用于核销端「今日报名」联调)。
|
|
|
|
|
|
*/
|
|
|
|
|
|
class SeedReservationsForDateCommand extends Command
|
|
|
|
|
|
{
|
|
|
|
|
|
protected $signature = 'reservations:seed-for-date
|
|
|
|
|
|
{activity_id=1 : 活动 ID}
|
|
|
|
|
|
{date? : 活动日 Y-m-d,默认今天}
|
|
|
|
|
|
{--count=20 : 生成条数}
|
|
|
|
|
|
{--dry-run : 只打印计划,不写入}';
|
|
|
|
|
|
|
|
|
|
|
|
protected $description = '在某一活动日生成若干条待核销预约(用于今日报名列表等测试)';
|
|
|
|
|
|
|
|
|
|
|
|
public function handle(): int
|
|
|
|
|
|
{
|
|
|
|
|
|
$activityId = (int) $this->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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|