TextbookSeriesResource.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\TextbookSeriesResource\Pages;
  4. use App\Models\TextbookSeries;
  5. use App\Services\TextbookApiService;
  6. use BackedEnum;
  7. use UnitEnum;
  8. use Filament\Facades\Filament;
  9. use Filament\Schemas\Schema;
  10. use Filament\Forms\Components\TextInput;
  11. use Filament\Forms\Components\Select;
  12. use Filament\Forms\Components\Toggle;
  13. use Filament\Forms\Components\Textarea;
  14. use Filament\Resources\Resource;
  15. use Filament\Tables;
  16. use Filament\Tables\Columns\TextColumn;
  17. use Filament\Tables\Columns\BadgeColumn;
  18. use Filament\Tables\Columns\ToggleColumn;
  19. use Filament\Actions\EditAction;
  20. use Filament\Actions\DeleteAction;
  21. use Filament\Actions\Action;
  22. use Illuminate\Database\Eloquent\Model;
  23. class TextbookSeriesResource extends Resource
  24. {
  25. protected static ?string $model = TextbookSeries::class;
  26. protected static ?string $recordTitleAttribute = 'name';
  27. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-rectangle-stack';
  28. protected static ?string $navigationLabel = '教材系列';
  29. protected static UnitEnum|string|null $navigationGroup = '教材管理';
  30. protected static ?int $navigationSort = 1;
  31. protected static ?TextbookApiService $apiService = null;
  32. public static function boot()
  33. {
  34. parent::boot();
  35. static::$apiService = app(TextbookApiService::class);
  36. }
  37. protected static function getApiService(): TextbookApiService
  38. {
  39. if (!static::$apiService) {
  40. static::$apiService = app(TextbookApiService::class);
  41. }
  42. return static::$apiService;
  43. }
  44. public static function form(Schema $schema): Schema
  45. {
  46. return $schema
  47. ->schema([
  48. TextInput::make('name')
  49. ->label('系列名称')
  50. ->required()
  51. ->maxLength(128)
  52. ->placeholder('如:人教版、北师大版、华东师大版'),
  53. TextInput::make('slug')
  54. ->label('别名')
  55. ->maxLength(64)
  56. ->placeholder('如:pep、bsd、ecnu'),
  57. TextInput::make('publisher')
  58. ->label('出版社')
  59. ->maxLength(128)
  60. ->placeholder('如:人民教育出版社'),
  61. TextInput::make('region')
  62. ->label('适用地区')
  63. ->maxLength(128)
  64. ->placeholder('如:全国、江苏省、浙江省'),
  65. TextInput::make('stages')
  66. ->label('适用学段')
  67. ->helperText('JSON 格式,如:["primary", "junior"]')
  68. ->placeholder('["primary", "junior", "senior"]')
  69. ->formatStateUsing(fn ($state) => is_array($state) ? json_encode($state, JSON_UNESCAPED_UNICODE) : $state)
  70. ->dehydrateStateUsing(fn ($state) => is_string($state) ? json_decode($state, true) : $state),
  71. TextInput::make('start_year')
  72. ->label('起始年份')
  73. ->numeric()
  74. ->placeholder('如:2024'),
  75. Select::make('is_active')
  76. ->label('是否启用')
  77. ->options(function (): array {
  78. return [
  79. 1 => '已启用',
  80. 0 => '已停用',
  81. ];
  82. })
  83. ->default(1)
  84. ->required()
  85. ->native(false)
  86. ->selectablePlaceholder(false)
  87. ->helperText('选择是否启用此教材系列'),
  88. TextInput::make('sort_order')
  89. ->label('排序')
  90. ->numeric()
  91. ->default(0)
  92. ->helperText('数字越小排序越靠前'),
  93. Textarea::make('meta')
  94. ->label('扩展信息')
  95. ->placeholder('JSON 格式,如:{"website": "http://example.com", "description": "说明"}')
  96. ->formatStateUsing(fn ($state) => is_array($state) ? json_encode($state, JSON_UNESCAPED_UNICODE) : $state)
  97. ->dehydrateStateUsing(fn ($state) => is_string($state) ? json_decode($state, true) : $state)
  98. ->columnSpanFull(),
  99. ]);
  100. }
  101. public static function table(Tables\Table $table): Tables\Table
  102. {
  103. return $table
  104. ->columns([
  105. TextColumn::make('id')
  106. ->label('ID')
  107. ->sortable()
  108. ->copyable()
  109. ->copyMessage('ID 已复制'),
  110. TextColumn::make('name')
  111. ->label('系列名称')
  112. ->searchable()
  113. ->sortable(),
  114. TextColumn::make('slug')
  115. ->label('别名')
  116. ->searchable()
  117. ->copyable(),
  118. TextColumn::make('publisher')
  119. ->label('出版社')
  120. ->searchable(),
  121. TextColumn::make('region')
  122. ->label('适用地区')
  123. ->searchable(),
  124. BadgeColumn::make('stages')
  125. ->label('学段')
  126. ->formatStateUsing(function ($state): string {
  127. // 确保返回字符串,即使输入是数组
  128. if (is_string($state)) {
  129. $stages = json_decode($state, true) ?? [];
  130. } elseif (is_array($state)) {
  131. $stages = $state;
  132. } else {
  133. return '';
  134. }
  135. $stageMap = [
  136. 'primary' => '小学',
  137. 'junior' => '初中',
  138. 'senior' => '高中',
  139. ];
  140. return implode(', ', array_map(fn($s) => $stageMap[$s] ?? $s, $stages));
  141. })
  142. ->separator(',')
  143. ->color('success'),
  144. TextColumn::make('start_year')
  145. ->label('起始年份')
  146. ->sortable(),
  147. ToggleColumn::make('is_active')
  148. ->label('启用状态'),
  149. TextColumn::make('sort_order')
  150. ->label('排序')
  151. ->sortable(),
  152. TextColumn::make('created_at')
  153. ->label('创建时间')
  154. ->dateTime('Y-m-d H:i')
  155. ->sortable()
  156. ->toggleable(),
  157. ])
  158. ->filters([
  159. Tables\Filters\SelectFilter::make('is_active')
  160. ->label('启用状态')
  161. ->options([
  162. 1 => '已启用',
  163. 0 => '已停用',
  164. ]),
  165. ])
  166. ->actions([
  167. EditAction::make()
  168. ->label('编辑'),
  169. DeleteAction::make()
  170. ->label('删除'),
  171. Action::make('view_textbooks')
  172. ->label('查看教材')
  173. ->icon('heroicon-o-book-open')
  174. ->url(fn(Model $record): string =>
  175. route('filament.admin.resources.textbooks.index', ['tableFilters[series_id][value]' => $record->id])
  176. ),
  177. ])
  178. ->bulkActions([
  179. \Filament\Actions\BulkActionGroup::make([
  180. \Filament\Actions\DeleteBulkAction::make()
  181. ->label('批量删除'),
  182. \Filament\Actions\BulkAction::make('activate')
  183. ->label('批量启用')
  184. ->icon('heroicon-o-check-circle')
  185. ->action(function ($records) {
  186. foreach ($records as $record) {
  187. static::getApiService()->updateTextbookSeries($record->id, ['is_active' => true]);
  188. }
  189. Filament::notify('success', '批量启用成功');
  190. }),
  191. \Filament\Actions\BulkAction::make('deactivate')
  192. ->label('批量停用')
  193. ->icon('heroicon-o-x-circle')
  194. ->action(function ($records) {
  195. foreach ($records as $record) {
  196. static::getApiService()->updateTextbookSeries($record->id, ['is_active' => false]);
  197. }
  198. Filament::notify('success', '批量停用成功');
  199. }),
  200. ]),
  201. ])
  202. ->defaultSort('sort_order')
  203. ->paginated([10, 25, 50, 100])
  204. ->poll('30s');
  205. }
  206. public static function getEloquentQuery(): \Illuminate\Database\Eloquent\builder
  207. {
  208. // 直接使用数据库查询
  209. return parent::getEloquentQuery();
  210. }
  211. public static function getRecord(?string $key): ?Model
  212. {
  213. // 直接从数据库获取记录
  214. return app(static::$model)->find($key);
  215. }
  216. protected static function newModel(array $data): Model
  217. {
  218. // 直接创建记录到数据库
  219. $model = app(static::$model);
  220. $model->fill($data);
  221. $model->save();
  222. return $model;
  223. }
  224. protected static function updateRecord(Model $record, array $data): Model
  225. {
  226. // 直接更新数据库记录
  227. $record->update($data);
  228. return $record;
  229. }
  230. protected static function deleteRecord(Model $record): bool
  231. {
  232. // 直接删除数据库记录
  233. return $record->delete();
  234. }
  235. public static function getPages(): array
  236. {
  237. return [
  238. 'index' => Pages\ManageTextbookSeries::route('/'),
  239. 'create' => Pages\CreateTextbookSeries::route('/create'),
  240. 'edit' => Pages\EditTextbookSeries::route('/{record}/edit'),
  241. ];
  242. }
  243. public static function canViewAny(): bool
  244. {
  245. // 临时允许所有用户查看,等待权限系统完善
  246. return true;
  247. }
  248. public static function getHeaderActions(): array
  249. {
  250. return [
  251. \Filament\Actions\Action::make('import_excel')
  252. ->label('Excel导入')
  253. ->icon('heroicon-o-document-arrow-up')
  254. ->color('success')
  255. ->url(fn(): string =>
  256. route('filament.admin.pages.textbook-excel-import-page')
  257. ),
  258. ];
  259. }
  260. public static function canCreate(): bool
  261. {
  262. // 临时允许所有用户创建,等待权限系统完善
  263. return true;
  264. }
  265. public static function canEdit(Model $record): bool
  266. {
  267. // 临时允许所有用户编辑,等待权限系统完善
  268. return true;
  269. }
  270. public static function canDelete(Model $record): bool
  271. {
  272. // 临时允许所有用户删除,等待权限系统完善
  273. return true;
  274. }
  275. public static function canDeleteAny(): bool
  276. {
  277. // 临时允许所有用户批量删除,等待权限系统完善
  278. return true;
  279. }
  280. }