label('新建系列') ->mutateFormDataUsing(function (array $data): array { // 调用 API 创建教材系列 try { $apiService = app(TextbookApiService::class); $result = $apiService->createTextbookSeries($data); if ($result) { Notification::make() ->title('教材系列创建成功') ->success() ->send(); } return $data; } catch (\Exception $e) { Notification::make() ->title('教材系列创建失败') ->body($e->getMessage()) ->danger() ->send(); return $data; } }), Actions\Action::make('sync_to_question_bank') ->label('同步到题库') ->icon('heroicon-o-arrow-path') ->color('warning') ->requiresConfirmation() ->modalHeading('同步教材系列到题库') ->modalDescription('此操作将完全覆盖题库服务中的所有教材系列数据,包括ID。操作不可撤销!') ->modalSubmitActionLabel('确认同步') ->modalCancelActionLabel('取消') ->action(function () { try { $apiService = app(TextbookApiService::class); $result = $apiService->syncTextbookSeriesToQuestionBank(); if (isset($result['success']) && $result['success']) { Notification::make() ->title('同步成功') ->success() ->body("已同步 {$result['synced_count']} 个系列到题库服务") ->send(); } else { throw new \Exception($result['message'] ?? '同步失败'); } } catch (\Exception $e) { Notification::make() ->title('同步失败') ->danger() ->body($e->getMessage()) ->send(); \Illuminate\Support\Facades\Log::error('同步教材系列到题库失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } }), ]; } protected function mutateTableQueryUsing(Builder $query): Builder { // 由于数据在 PostgreSQL 中,这里返回空查询 // 实际数据通过 API 获取 return $query->whereRaw('1=0'); } public function mount(): void { parent::mount(); // 初始化时从 API 获取数据 $this->refreshTableDataFromApi(); } protected function refreshTableDataFromApi(): void { // 通过 API 获取教材系列数据 try { $apiService = app(TextbookApiService::class); $seriesData = $apiService->getTextbookSeries(); // 将 API 数据转换为 Eloquent 集合(用于显示) // 这里需要根据实际 API 响应格式调整 // 由于 Filament 表格需要 Eloquent 模型,这里只是示例 // 实际项目中可能需要创建临时的资源类来处理 API 数据 } catch (\Exception $e) { Notification::make() ->title('获取数据失败') ->body($e->getMessage()) ->danger() ->send(); } } }