option('company') ?: (company_id() ?: 1)); $company = Company::query()->whereKey($companyId)->first(); if (! $company) { $this->error("公司 #{$companyId} 不存在。"); return self::FAILURE; } $company->makeCurrent(); $path = trim((string) $this->argument('file')) ?: '-'; $handle = $path === '-' ? STDIN : @fopen($path, 'rb'); if (! is_resource($handle)) { $this->error('无法读取供应商名单文件。'); return self::FAILURE; } $names = []; $lineNumber = 0; try { while (($line = fgets($handle)) !== false) { $lineNumber++; if ($lineNumber === 1) { $line = ltrim($line, "\xEF\xBB\xBF"); } $name = trim($line); if ($lineNumber === 1 && in_array($name, ['供应商', '供应商名称'], true)) { continue; } if ($name !== '') { $names[] = $name; } } } finally { if ($path !== '-') { fclose($handle); } } if ($lineNumber > 5000) { $this->error('催办名单不能超过 5000 行。'); return self::FAILURE; } if ($names === []) { $this->error('供应商名单为空,未修改现有名单。'); return self::FAILURE; } try { $saved = $targets->replace($names); } catch (Throwable $exception) { $this->error($exception->getMessage()); return self::FAILURE; } $missingRobots = array_values(array_diff($saved, $notifier->configuredSuppliers())); $this->info(sprintf( '#%d %s: 已保存 %d 家当前供应商;其中 %d 家可自动催办,%d 家未配置钉钉群机器人。', $company->id, $company->name, count($saved), count($saved) - count($missingRobots), count($missingRobots), )); if ($missingRobots !== []) { $this->warn('未配置机器人:' . implode('、', $missingRobots)); } return self::SUCCESS; } }