company_id = $companyId ?? 1; $this->onQueue('exports'); } public function handle(ThirdPartyWarehouseStorageMirrorRepository $repository, InventoryWarehouseShortNames $shortNames): void { $path = storage_path('app/temp/' . $this->file_name); File::ensureDirectoryExists(dirname($path)); try { $csv = fopen($path, 'wb'); if ($csv === false) { throw new \RuntimeException('无法创建进销存报表导出文件。'); } fputs($csv, chr(0xEF) . chr(0xBB) . chr(0xBF)); fputcsv($csv, array_column($this->columns, 'label')); $names = $shortNames->forCompany($this->company_id); foreach ($repository->exportMonthlyInventoryReportRows($this->filters) as $row) { $row = $shortNames->fill($row, $names); fputcsv($csv, array_map(function (array $column) use ($row): mixed { $value = data_get($row, $column['name']); return $column['name'] === 'product_code' ? $this->csvIdentifier($value) : (($column['kind'] ?? null) === 'integer' ? ($value === null || $value === '' ? '' : $value) : $this->csvText($value)); }, $this->columns)); } fclose($csv); dispatch(new CreateMediableForExport($this->user, $this->file_name, '进销存报表')); } catch (Throwable $exception) { if (isset($csv) && is_resource($csv)) { fclose($csv); } @unlink($path); report($exception); $this->user->notify(new ExportFailed($exception->getMessage())); throw $exception; } } private function csvIdentifier(mixed $value): string { $text = trim((string) ($value ?? '')); return $text === '' ? '' : "'" . $text; } private function csvText(mixed $value): string { return trim((string) ($value ?? '')); } }