TextbookCatalogResource.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\TextbookCatalogResource\Pages;
  4. use App\Models\TextbookCatalog;
  5. use App\Services\TextbookApiService;
  6. use BackedEnum;
  7. use UnitEnum;
  8. use Filament\Resources\Resource;
  9. use Filament\Tables;
  10. use Filament\Tables\Columns\TextColumn;
  11. use Filament\Tables\Columns\BadgeColumn;
  12. use Filament\Actions\EditAction;
  13. use Filament\Actions\DeleteAction;
  14. use Illuminate\Database\Eloquent\Model;
  15. class TextbookCatalogResource extends Resource
  16. {
  17. protected static ?string $model = ApiTextbookCatalog::class;
  18. protected static ?string $recordTitleAttribute = 'title';
  19. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-squares-2x2';
  20. protected static ?string $navigationLabel = '教材目录';
  21. protected static UnitEnum|string|null $navigationGroup = '教材管理';
  22. protected static ?int $navigationSort = 3;
  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 table(Tables\Table $table): Tables\Table
  37. {
  38. return $table
  39. ->columns([
  40. TextColumn::make('textbook.official_title')
  41. ->label('教材')
  42. ->searchable()
  43. ->wrap(),
  44. TextColumn::make('title')
  45. ->label('目录标题')
  46. ->searchable()
  47. ->wrap(),
  48. TextColumn::make('display_no')
  49. ->label('编号')
  50. ->searchable(),
  51. BadgeColumn::make('node_type')
  52. ->label('类型')
  53. ->formatStateUsing(fn (string $state): string => match ($state) {
  54. 'chapter' => '章',
  55. 'section' => '节',
  56. 'subsection' => '小节',
  57. 'item' => '条目',
  58. 'project' => '项目学习',
  59. 'reading' => '阅读材料',
  60. 'practice' => '综合实践',
  61. 'summary' => '复习',
  62. 'appendix' => '附录',
  63. default => '其他',
  64. })
  65. ->color('info'),
  66. TextColumn::make('depth')
  67. ->label('层级')
  68. ->sortable(),
  69. TextColumn::make('page_start')
  70. ->label('起始页码')
  71. ->sortable(),
  72. TextColumn::make('page_end')
  73. ->label('结束页码')
  74. ->sortable(),
  75. TextColumn::make('created_at')
  76. ->label('创建时间')
  77. ->dateTime('Y-m-d H:i')
  78. ->sortable()
  79. ->toggleable(),
  80. ])
  81. ->filters([
  82. Tables\Filters\SelectFilter::make('textbook_id')
  83. ->label('教材')
  84. ->options(function () {
  85. $textbooks = static::getApiService()->getTextbooks();
  86. $options = [];
  87. foreach ($textbooks['data'] ?? [] as $t) {
  88. $options[$t['id']] = $t['official_title'];
  89. }
  90. return $options;
  91. })
  92. ->searchable()
  93. ->preload(),
  94. Tables\Filters\SelectFilter::make('node_type')
  95. ->label('节点类型')
  96. ->options([
  97. 'chapter' => '章',
  98. 'section' => '节',
  99. 'subsection' => '小节',
  100. 'item' => '条目',
  101. 'project' => '项目学习',
  102. 'reading' => '阅读材料',
  103. 'practice' => '综合实践',
  104. 'summary' => '复习',
  105. 'appendix' => '附录',
  106. 'custom' => '其他',
  107. ]),
  108. ])
  109. ->actions([
  110. EditAction::make()
  111. ->label('编辑'),
  112. DeleteAction::make()
  113. ->label('删除'),
  114. ])
  115. ->paginated([10, 25, 50, 100])
  116. ->poll(null); // 禁用自动刷新
  117. }
  118. public static function getEloquentQuery(): \Illuminate\Database\Eloquent\builder
  119. {
  120. // 完全不使用数据库查询,所有数据通过 API 获取
  121. // 强制使用 migrations 表,这个表肯定存在
  122. return parent::getEloquentQuery()->from('migrations')->whereRaw('1=0');
  123. }
  124. public static function getRecord(?string $key): ?Model
  125. {
  126. // 教材目录是嵌套数据,需要根据 textbook_id 获取
  127. $textbookId = request()->get('textbook_id');
  128. if (!$textbookId) {
  129. return null;
  130. }
  131. $catalog = static::getApiService()->getTextbookCatalog((int) $textbookId, 'flat');
  132. foreach ($catalog as $node) {
  133. if ($node['id'] == $key) {
  134. return new ApiTextbookCatalog($node);
  135. }
  136. }
  137. return null;
  138. }
  139. public static function getRecords(): array
  140. {
  141. $textbookId = request()->get('tableFilters.textbook_id.value');
  142. if (!$textbookId) {
  143. return [];
  144. }
  145. $catalog = static::getApiService()->getTextbookCatalog((int) $textbookId, 'flat');
  146. $records = [];
  147. foreach ($catalog as $node) {
  148. $records[] = new ApiTextbookCatalog($node);
  149. }
  150. return $records;
  151. }
  152. public static function getPages(): array
  153. {
  154. return [
  155. 'index' => Pages\ManageTextbookCatalogs::route('/'),
  156. ];
  157. }
  158. public static function canViewAny(): bool
  159. {
  160. // 临时允许所有用户查看,等待权限系统完善
  161. return true;
  162. }
  163. public static function canCreate(): bool
  164. {
  165. // 临时允许所有用户创建,等待权限系统完善
  166. return true;
  167. }
  168. public static function canEdit(Model $record): bool
  169. {
  170. // 临时允许所有用户编辑,等待权限系统完善
  171. return true;
  172. }
  173. public static function canDelete(Model $record): bool
  174. {
  175. // 临时允许所有用户删除,等待权限系统完善
  176. return true;
  177. }
  178. public static function canDeleteAny(): bool
  179. {
  180. // 临时允许所有用户批量删除,等待权限系统完善
  181. return true;
  182. }
  183. protected static function deleteRecord(Model $record): bool
  184. {
  185. // 删除记录时,同时通过 API 删除题库服务中的数据
  186. return static::getApiService()->deleteTextbookCatalog($record->id);
  187. }
  188. }
  189. /**
  190. * API 教材目录模型 - 完全通过 API 获取数据
  191. * 这个类继承自 Model 但不执行任何数据库查询
  192. */
  193. class ApiTextbookCatalog extends \Illuminate\Database\Eloquent\Model
  194. {
  195. protected $table = 'migrations'; // 使用肯定存在的表
  196. // 禁用时间戳
  197. public $timestamps = false;
  198. // 禁用所有fillable检查
  199. protected $guarded = [];
  200. public function __construct(array $attributes = [])
  201. {
  202. parent::__construct($attributes);
  203. }
  204. }