option('directory')); $section = trim((string) $this->option('section')); $dataset = trim((string) $this->option('dataset')); $companyId = max(1, (int) $this->option('company')); $userId = $this->option('user') === null ? null : max(1, (int) $this->option('user')); if ($directory === '' || ! is_dir($directory)) { $this->error('导入目录不存在。'); return self::FAILURE; } if (! $registry->supports($section, $dataset)) { $this->error('不支持的终版页面。'); return self::FAILURE; } $files = glob(rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*.csv') ?: []; sort($files, SORT_NATURAL); if ($files === []) { $this->error('导入目录中没有 CSV 文件。'); return self::FAILURE; } $fields = $registry->fields($section, $dataset); $storeDataset = 'final:' . ($section === PayablesFinalRegistry::SECTION_COURIER ? 'courier' : 'supplier') . ':' . $dataset; $totalCreated = 0; $totalUpdated = 0; $totalRows = 0; try { foreach ($files as $index => $path) { $file = new UploadedFile( $path, basename($path), 'text/csv', null, true, ); $prepared = $runner->prepare( $file, $fields, $storeDataset, $companyId, $userId, ); $token = $prepared['token']; $offset = 0; $created = 0; $updated = 0; do { $progress = $runner->process($token, $offset, null, $companyId); $offset = $progress['offset']; $created = $progress['created']; $updated = $progress['updated']; } while (! $progress['done']); $totalCreated += $created; $totalUpdated += $updated; $totalRows += (int) $prepared['total']; $this->line(sprintf( '[%d/%d] %s:%s 行,新增 %s,更新 %s', $index + 1, count($files), basename($path), number_format((int) $prepared['total']), number_format($created), number_format($updated), )); } } catch (Throwable $exception) { report($exception); $this->error($exception->getMessage()); return self::FAILURE; } $this->info(sprintf( '导入完成:文件 %s 个,读取 %s 行,新增 %s,更新 %s。', number_format(count($files)), number_format($totalRows), number_format($totalCreated), number_format($totalUpdated), )); return self::SUCCESS; } }