TextbookSeriesResource.php 11 KB

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