ManageTextbookSeries.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Filament\Resources\TextbookSeriesResource\Pages;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use App\Filament\Resources\TextbookSeriesResource;
  5. use App\Services\TextbookApiService;
  6. use Filament\Actions;
  7. use Filament\Resources\Pages\ManageRecords;
  8. use Filament\Notifications\Notification;
  9. use Illuminate\Database\Eloquent\Model;
  10. class ManageTextbookSeries extends ManageRecords
  11. {
  12. protected static string $resource = TextbookSeriesResource::class;
  13. protected function getHeaderActions(): array
  14. {
  15. return [
  16. Actions\CreateAction::make()
  17. ->label('新建系列')
  18. ->mutateFormDataUsing(function (array $data): array {
  19. // 调用 API 创建教材系列
  20. try {
  21. $apiService = app(TextbookApiService::class);
  22. $result = $apiService->createTextbookSeries($data);
  23. if ($result) {
  24. Notification::make()
  25. ->title('教材系列创建成功')
  26. ->success()
  27. ->send();
  28. }
  29. return $data;
  30. } catch (\Exception $e) {
  31. Notification::make()
  32. ->title('教材系列创建失败')
  33. ->body($e->getMessage())
  34. ->danger()
  35. ->send();
  36. return $data;
  37. }
  38. }),
  39. Actions\Action::make('sync_to_question_bank')
  40. ->label('同步到题库')
  41. ->icon('heroicon-o-arrow-path')
  42. ->color('warning')
  43. ->requiresConfirmation()
  44. ->modalHeading('同步教材系列到题库')
  45. ->modalDescription('此操作将完全覆盖题库服务中的所有教材系列数据,包括ID。操作不可撤销!')
  46. ->modalSubmitActionLabel('确认同步')
  47. ->modalCancelActionLabel('取消')
  48. ->action(function () {
  49. try {
  50. $apiService = app(TextbookApiService::class);
  51. $result = $apiService->syncTextbookSeriesToQuestionBank();
  52. if (isset($result['success']) && $result['success']) {
  53. Notification::make()
  54. ->title('同步成功')
  55. ->success()
  56. ->body("已同步 {$result['synced_count']} 个系列到题库服务")
  57. ->send();
  58. } else {
  59. throw new \Exception($result['message'] ?? '同步失败');
  60. }
  61. } catch (\Exception $e) {
  62. Notification::make()
  63. ->title('同步失败')
  64. ->danger()
  65. ->body($e->getMessage())
  66. ->send();
  67. \Illuminate\Support\Facades\Log::error('同步教材系列到题库失败', [
  68. 'error' => $e->getMessage(),
  69. 'trace' => $e->getTraceAsString()
  70. ]);
  71. }
  72. }),
  73. ];
  74. }
  75. protected function mutateTableQueryUsing(Builder $query): Builder
  76. {
  77. // 由于数据在 PostgreSQL 中,这里返回空查询
  78. // 实际数据通过 API 获取
  79. return $query->whereRaw('1=0');
  80. }
  81. public function mount(): void
  82. {
  83. parent::mount();
  84. // 初始化时从 API 获取数据
  85. $this->refreshTableDataFromApi();
  86. }
  87. protected function refreshTableDataFromApi(): void
  88. {
  89. // 通过 API 获取教材系列数据
  90. try {
  91. $apiService = app(TextbookApiService::class);
  92. $seriesData = $apiService->getTextbookSeries();
  93. // 将 API 数据转换为 Eloquent 集合(用于显示)
  94. // 这里需要根据实际 API 响应格式调整
  95. // 由于 Filament 表格需要 Eloquent 模型,这里只是示例
  96. // 实际项目中可能需要创建临时的资源类来处理 API 数据
  97. } catch (\Exception $e) {
  98. Notification::make()
  99. ->title('获取数据失败')
  100. ->body($e->getMessage())
  101. ->danger()
  102. ->send();
  103. }
  104. }
  105. }