option('month')); if ($month === '') { $month = now('Asia/Shanghai')->subMonthNoOverflow()->format('Y-m'); } $lock = Cache::lock('inventory-supplier-summary-inbound-import', 21600); if (! $lock->get()) { return $this->finish([ 'status' => 'failed', 'report_month' => $month, 'source_filename' => basename((string) $this->argument('file')), 'success' => 0, 'duplicates' => 0, 'failed' => 1, 'mapped' => 0, 'total_in_table' => 0, 'failure_reason_count' => 1, 'failure_reasons' => [[ 'row' => 0, 'reason' => '已有供应商汇总导入任务正在执行', ]], ]); } try { $result = $importer->import((string) $this->argument('file'), $month); } catch (Throwable $exception) { report($exception); $result = [ 'status' => 'failed', 'report_month' => $month, 'source_filename' => basename((string) $this->argument('file')), 'success' => 0, 'duplicates' => 0, 'failed' => 1, 'mapped' => 0, 'total_in_table' => 0, 'failure_reason_count' => 1, 'failure_reasons' => [[ 'row' => 0, 'reason' => $exception->getMessage(), ]], ]; } finally { $lock->release(); } return $this->finish($result); } /** @param array $result */ private function finish(array $result): int { $reasons = is_array($result['failure_reasons'] ?? null) ? $result['failure_reasons'] : []; $result['failure_reason_count'] = count($reasons); $json = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if (! is_string($json)) { $json = '{"status":"failed","failure_reasons":[{"row":0,"reason":"结果 JSON 编码失败"}]}'; } $resultPath = trim((string) $this->option('result-json')); if ($resultPath !== '') { File::ensureDirectoryExists(dirname($resultPath)); File::put($resultPath, $json . PHP_EOL); } $this->line(sprintf( '%s 导入状态=%s,成功=%s,重复=%s,失败=%s,目标月份总行数=%s', (string) ($result['report_month'] ?? ''), (string) ($result['status'] ?? 'failed'), number_format((int) ($result['success'] ?? 0)), number_format((int) ($result['duplicates'] ?? 0)), number_format((int) ($result['failed'] ?? 0)), number_format((int) ($result['total_in_table'] ?? 0)), )); foreach (array_slice($reasons, 0, 10) as $reason) { $this->warn(sprintf( '第 %s 行:%s', (string) ($reason['row'] ?? ''), (string) ($reason['reason'] ?? '未知错误'), )); } if (count($reasons) > 10) { $this->line(sprintf( '另有 %s 条失败原因,完整清单见结果 JSON。', number_format(count($reasons) - 10), )); } return ($result['status'] ?? 'failed') === 'success' ? self::SUCCESS : self::FAILURE; } }