TextbookResource.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\TextbookResource\Pages;
  4. use App\Filament\Resources\TextbookResource\Schemas\TextbookFormSchema;
  5. use App\Filament\Resources\TextbookResource\Tables\TextbookTable;
  6. use App\Filament\Resources\TextbookResource\Actions\DeleteTextbookAction;
  7. use App\Models\Textbook;
  8. use App\Services\TextbookApiService;
  9. use BackedEnum;
  10. use UnitEnum;
  11. use Filament\Resources\Resource;
  12. use Filament\Schemas\Schema;
  13. use Filament\Tables;
  14. use Illuminate\Database\Eloquent\Model;
  15. class TextbookResource extends Resource
  16. {
  17. protected static ?string $model = Textbook::class;
  18. protected static ?string $recordTitleAttribute = 'official_title';
  19. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-book-open';
  20. protected static ?string $navigationLabel = '教材管理';
  21. protected static UnitEnum|string|null $navigationGroup = '教材管理';
  22. protected static ?int $navigationSort = 2;
  23. protected static ?TextbookApiService $apiService = null;
  24. public static function boot()
  25. {
  26. parent::boot();
  27. static::$apiService = app(TextbookApiService::class);
  28. }
  29. protected static function getApiService(): TextbookApiService
  30. {
  31. if (!static::$apiService) {
  32. static::$apiService = app(TextbookApiService::class);
  33. }
  34. return static::$apiService;
  35. }
  36. public static function form(Schema $schema): Schema
  37. {
  38. return TextbookFormSchema::make($schema);
  39. }
  40. public static function table(Tables\Table $table): Tables\Table
  41. {
  42. return TextbookTable::make($table);
  43. }
  44. public static function getEloquentQuery(): \Illuminate\Database\Eloquent\Builder
  45. {
  46. // 返回空查询,实际数据通过 API 获取
  47. return parent::getEloquentQuery()->whereRaw('1=0');
  48. }
  49. public static function getRecords(): array
  50. {
  51. // 从 API 获取教材数据
  52. $apiService = static::getApiService();
  53. $result = $apiService->getTextbooks();
  54. \Log::info('TextbookResource::getRecords called', [
  55. 'count' => count($result['data'] ?? []),
  56. 'has_data' => !empty($result['data'])
  57. ]);
  58. $records = [];
  59. foreach ($result['data'] ?? [] as $item) {
  60. $model = new \App\Models\Textbook($item);
  61. $model->exists = true;
  62. $model->id = $item['id'];
  63. $records[] = $model;
  64. }
  65. return $records;
  66. }
  67. public static function resolveRecordRouteBinding(int | string $key, ?\Closure $modifyQuery = null): ?\Illuminate\Database\Eloquent\Model
  68. {
  69. $record = static::getApiService()->getTextbook((int) $key);
  70. if (!$record) {
  71. return null;
  72. }
  73. $model = new \App\Models\Textbook($record);
  74. $model->exists = true;
  75. $model->id = $record['id'];
  76. return $model;
  77. }
  78. public static function getPages(): array
  79. {
  80. return [
  81. 'index' => Pages\ManageTextbooks::route('/'),
  82. 'create' => Pages\CreateTextbook::route('/create'),
  83. 'view' => Pages\ViewTextbook::route('/{record}'),
  84. 'edit' => Pages\EditTextbook::route('/{record}/edit'),
  85. ];
  86. }
  87. public static function canViewAny(): bool
  88. {
  89. // 临时允许所有用户查看,等待权限系统完善
  90. return true;
  91. }
  92. public static function getHeaderActions(): array
  93. {
  94. return [
  95. \Filament\Actions\Action::make('import_excel')
  96. ->label('Excel导入')
  97. ->icon('heroicon-o-document-arrow-up')
  98. ->color('success')
  99. ->url(fn(): string =>
  100. route('filament.admin.pages.textbook-excel-import-page')
  101. ),
  102. ];
  103. }
  104. public static function canCreate(): bool
  105. {
  106. // 临时允许所有用户创建,等待权限系统完善
  107. return true;
  108. }
  109. public static function canEdit(Model $record): bool
  110. {
  111. // 临时允许所有用户编辑,等待权限系统完善
  112. return true;
  113. }
  114. public static function canDelete(Model $record): bool
  115. {
  116. // 临时允许所有用户删除,等待权限系统完善
  117. return true;
  118. }
  119. public static function canDeleteAny(): bool
  120. {
  121. // 临时允许所有用户批量删除,等待权限系统完善
  122. return true;
  123. }
  124. }