option('date')) ?: now($timezone)->toDateString(); try { $date = Carbon::createFromFormat('!Y-m-d', $dateValue, $timezone)->startOfDay(); } catch (Throwable) { $this->error('执行日期格式无效。'); return self::FAILURE; } if ($date->format('Y-m-d') !== $dateValue) { $this->error('执行日期格式无效。'); return self::FAILURE; } $failed = false; $dryRun = (bool) $this->option('dry-run'); foreach (Company::enabled()->orderBy('id')->get() as $company) { $company->makeCurrent(); $lock = Cache::lock(sprintf( 'supplier-statement-reminder:%d:%s', $company->id, $date->toDateString(), ), 900); if (! $dryRun && ! $lock->get()) { $this->warn("#{$company->id} {$company->name}: 已有催办任务运行"); continue; } try { $result = $reminder->send( $date, (int) $company->id, $dryRun, trim((string) $this->option('supplier')), ); if (! $result['reminder_day']) { $this->line("#{$company->id} {$company->name}: 今天不在 1-14 日催办窗口"); continue; } if ($result['source_suppliers'] === 0) { $this->warn("#{$company->id} {$company->name}: 上月对账入库数据尚未导入或没有入库供应商"); continue; } $this->line(sprintf( '#%d %s %s: 入库供应商 %d 家,可提醒 %d 家,%s %d 家,已上传 %d 家,今日已提醒 %d 家,未配置机器人 %d 家,失败 %d 家', $company->id, $company->name, $result['accounting_month'], $result['source_suppliers'], $result['configured'], $dryRun ? '将提醒' : '已发送', count($dryRun ? $result['previewed'] : $result['sent']), count($result['uploaded']), count($result['already_sent']), count($result['missing_robot']), count($result['failed']), )); foreach ($result['failed'] as $supplier => $message) { $this->error("{$supplier}: {$message}"); $failed = true; } } catch (Throwable $exception) { report($exception); $this->error("#{$company->id} {$company->name}: 催办任务失败:" . $exception->getMessage()); $failed = true; } finally { if (! $dryRun) { $lock->release(); } } } return $failed ? self::FAILURE : self::SUCCESS; } }