| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Filament\Resources\TextbookSeriesResource\Pages;
- use Illuminate\Database\Eloquent\Builder;
- use App\Filament\Resources\TextbookSeriesResource;
- use App\Services\TextbookApiService;
- use Filament\Actions;
- use Filament\Resources\Pages\ManageRecords;
- use Filament\Notifications\Notification;
- use Illuminate\Database\Eloquent\Model;
- class ManageTextbookSeries extends ManageRecords
- {
- protected static string $resource = TextbookSeriesResource::class;
- protected function getHeaderActions(): array
- {
- return [
- Actions\CreateAction::make()
- ->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();
- }
- }
- }
|