option('date')); if ($date !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) !== 1) { $this->error('日期格式无效,应为 Y-m-d。'); return self::FAILURE; } $today = $date !== '' ? CarbonImmutable::createFromFormat('Y-m-d', $date, DropshipUploadReminder::TIMEZONE)->startOfDay() : DropshipUploadReminder::today(); $force = (bool) $this->option('force'); if ($today->day > DropshipUploadReminder::DEADLINE_DAY && ! $force) { $this->line($today->format('Y-m-d') . ':只在每月 1–' . DropshipUploadReminder::DEADLINE_DAY . ' 日提醒。'); return self::SUCCESS; } $month = DropshipUploadReminder::billMonth($today); $failed = false; foreach (Company::enabled()->orderBy('id')->get() as $company) { $company->makeCurrent(); $label = "#{$company->id} {$company->name} {$month}"; if (! DropshipUploadReminder::enabled() && ! $force) { $this->line($label . ':自动提醒已关闭'); continue; } $portal = route('business.supplier-portal.home', ['company_id' => $company->id]); if ($this->option('dry-run')) { $plan = $reminder->targets($month); $this->line($label . ':会提醒 ' . count($plan['targets']) . ' 家:' . implode('、', array_map(static fn ($f): string => (string) $f->name, $plan['targets'])) . ';已上传 ' . count($plan['done']) . ' 家;没有代发发货 ' . count($plan['no_shipment']) . ' 家;没配机器人 ' . count($plan['no_robot']) . ' 家' . ($plan['no_robot'] !== [] ? ':' . implode('、', $plan['no_robot']) : '') . ';入口 ' . $portal); continue; } $lock = sprintf('dropship-upload-reminder:%d:%s:%s', $company->id, $month, $today->format('Y-m-d')); if (! $force && ! Cache::add($lock, true, now()->addDays(2))) { $this->line($label . ':今天已经提醒过'); continue; } try { $result = $reminder->remind($month, $portal, null, $today); $this->info($label . ':' . DropshipFreightController::reminderSummary($month, $result)); $failed = $failed || $result['failed'] !== []; } catch (Throwable $error) { Cache::forget($lock); report($error); $this->error($label . ':自动提醒失败:' . $error->getMessage()); $failed = true; } } return $failed ? self::FAILURE : self::SUCCESS; } }