StudentResource.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\StudentResource\Pages;
  4. use App\Models\Student;
  5. use App\Models\Teacher;
  6. use BackedEnum;
  7. use Filament\Forms\Components\Select;
  8. use Filament\Forms\Components\Textarea;
  9. use Filament\Forms\Components\TextInput;
  10. use Filament\Resources\Resource;
  11. use Filament\Schemas\Schema;
  12. use Filament\Tables;
  13. use Filament\Tables\Table;
  14. use Illuminate\Support\Facades\Cache;
  15. class StudentResource extends Resource
  16. {
  17. protected static ?string $model = Student::class;
  18. protected static BackedEnum | string | null $navigationIcon = 'heroicon-o-academic-cap';
  19. protected static bool $shouldRegisterNavigation = false;
  20. public static function form(Schema $schema): Schema
  21. {
  22. $currentUser = auth()->user();
  23. return $schema->schema([
  24. // 学生ID字段在创建时隐藏,编辑时显示但禁用
  25. TextInput::make('student_id')
  26. ->label('学生ID')
  27. ->disabled()
  28. ->hidden(fn (?Student $record) => blank($record))
  29. ->formatStateUsing(fn (?Student $record): string => $record?->student_id ?? ''),
  30. TextInput::make('name')
  31. ->label('姓名')
  32. ->required()
  33. ->maxLength(128)
  34. ->placeholder('请输入学生姓名'),
  35. TextInput::make('grade')
  36. ->label('年级')
  37. ->required()
  38. ->maxLength(32)
  39. ->placeholder('例如:高一、高二等'),
  40. TextInput::make('class_name')
  41. ->label('班级')
  42. ->helperText('选填项,如不确定可留空')
  43. ->maxLength(64)
  44. ->placeholder('例如:1班、2班等'),
  45. Select::make('teacher_id')
  46. ->label('指导老师')
  47. ->options(fn () => self::teacherOptionsForCurrentUser())
  48. ->searchable()
  49. ->required()
  50. ->preload()
  51. ->placeholder('请选择指导老师')
  52. ->hidden(fn () => $currentUser?->isTeacher() ?? false) // 老师登录时隐藏
  53. ->dehydrateStateUsing(function ($state) use ($currentUser) {
  54. // 如果是老师,自动设置为当前老师的ID
  55. if ($currentUser?->isTeacher() ?? false) {
  56. return $currentUser->teacher?->teacher_id;
  57. }
  58. return $state;
  59. }),
  60. Textarea::make('remark')
  61. ->label('备注')
  62. ->rows(3)
  63. ->placeholder('请输入备注信息(可选)')
  64. ->columnSpanFull(),
  65. ])->columns(2);
  66. }
  67. public static function table(Table $table): Table
  68. {
  69. $currentUser = auth()->user();
  70. return $table
  71. ->query(
  72. Student::query()
  73. ->with(['teacher.user', 'user'])
  74. ->when($currentUser?->isTeacher() ?? false, function ($query) use ($currentUser) {
  75. // 如果是老师登录,只显示该老师的学生
  76. $teacherId = $currentUser->teacher?->teacher_id;
  77. if ($teacherId) {
  78. $query->where('teacher_id', $teacherId);
  79. }
  80. })
  81. )
  82. ->columns([
  83. Tables\Columns\TextColumn::make('student_id')
  84. ->label('学生ID')
  85. ->badge()
  86. ->color('primary')
  87. ->copyable()
  88. ->copyMessage('学生ID已复制')
  89. ->copyMessageDuration(1500)
  90. ->sortable()
  91. ->searchable(),
  92. Tables\Columns\TextColumn::make('name')
  93. ->label('姓名')
  94. ->weight('bold')
  95. ->searchable()
  96. ->sortable(),
  97. Tables\Columns\TextColumn::make('grade')
  98. ->label('年级')
  99. ->badge()
  100. ->color('success')
  101. ->sortable(),
  102. Tables\Columns\TextColumn::make('class_name')
  103. ->label('班级')
  104. ->placeholder('未分配')
  105. ->sortable()
  106. ->formatStateUsing(fn ($state) => $state ?: '未分配'),
  107. Tables\Columns\TextColumn::make('teacher.user.full_name')
  108. ->label('指导老师')
  109. ->default('未分配')
  110. ->sortable()
  111. ->searchable()
  112. ->visible(fn () => !($currentUser?->isTeacher() ?? false)), // 老师登录时不显示老师列
  113. ])
  114. ->filters([
  115. Tables\Filters\SelectFilter::make('grade')
  116. ->label('年级')
  117. ->options(fn () => self::gradeOptions())
  118. ->placeholder('全部年级'),
  119. Tables\Filters\SelectFilter::make('class_name')
  120. ->label('班级')
  121. ->options(fn () => self::classOptions())
  122. ->placeholder('全部班级'),
  123. Tables\Filters\SelectFilter::make('teacher_id')
  124. ->label('指导老师')
  125. ->options(fn () => self::teacherOptionsForCurrentUser())
  126. ->placeholder('全部老师')
  127. ->visible(fn () => !($currentUser?->isTeacher() ?? false)), // 老师登录时隐藏老师筛选
  128. ])
  129. ->actions([])
  130. ->bulkActions([])
  131. ->emptyStateHeading('暂无学生记录')
  132. ->emptyStateDescription('开始创建你的第一个学生吧')
  133. ->emptyStateActions([]);
  134. }
  135. public static function getPages(): array
  136. {
  137. return [
  138. 'index' => Pages\ListStudents::route('/'),
  139. 'create' => Pages\CreateStudent::route('/create'),
  140. 'view' => Pages\ViewStudent::route('/{record}'),
  141. 'edit' => Pages\EditStudent::route('/{record}/edit'),
  142. ];
  143. }
  144. protected static function teacherOptions(): array
  145. {
  146. // 使用缓存优化性能,缓存1小时
  147. return cache()->remember('teacher_options', 3600, function () {
  148. return Teacher::with(['user' => function ($query) {
  149. $query->select('user_id', 'full_name', 'role');
  150. }])
  151. ->whereHas('user', function ($query) {
  152. $query->where('role', 'teacher');
  153. })
  154. ->select('teacher_id', 'user_id', 'name')
  155. ->get()
  156. ->map(function ($teacher) {
  157. return [
  158. 'id' => $teacher->teacher_id,
  159. 'name' => $teacher->user->full_name ?? $teacher->name
  160. ];
  161. })
  162. ->pluck('name', 'id')
  163. ->toArray();
  164. });
  165. }
  166. protected static function teacherOptionsForCurrentUser(): array
  167. {
  168. $currentUser = auth()->user();
  169. // 如果是老师登录,只返回自己的选项
  170. if ($currentUser?->isTeacher() ?? false) {
  171. $teacherId = $currentUser->teacher?->teacher_id;
  172. $teacherName = $currentUser->teacher?->user?->full_name
  173. ?? $currentUser->teacher?->name
  174. ?? '当前老师';
  175. if ($teacherId) {
  176. return [$teacherId => $teacherName];
  177. }
  178. return [];
  179. }
  180. // 如果是管理员,返回所有老师
  181. return self::teacherOptions();
  182. }
  183. protected static function gradeOptions(): array
  184. {
  185. // 使用缓存优化性能,缓存30分钟
  186. return cache()->remember('grade_options', 1800, function () {
  187. return Student::query()
  188. ->select('grade')
  189. ->distinct()
  190. ->whereNotNull('grade')
  191. ->orderBy('grade')
  192. ->pluck('grade', 'grade')
  193. ->toArray();
  194. });
  195. }
  196. protected static function classOptions(): array
  197. {
  198. // 使用缓存优化性能,缓存30分钟
  199. return Cache::remember('class_options', 1800, function () {
  200. return Student::query()
  201. ->select('class_name')
  202. ->whereNotNull('class_name')
  203. ->where('class_name', '!=', '')
  204. ->distinct()
  205. ->orderBy('class_name')
  206. ->pluck('class_name', 'class_name')
  207. ->toArray();
  208. });
  209. }
  210. /**
  211. * 清除相关缓存
  212. */
  213. public static function clearCaches(): void
  214. {
  215. Cache::forget('teacher_options');
  216. Cache::forget('grade_options');
  217. Cache::forget('class_options');
  218. }
  219. /**
  220. * 在保存前自动设置老师ID
  221. */
  222. public static function beforeSave(): void
  223. {
  224. $currentUser = auth()->user();
  225. if ($currentUser?->isTeacher() ?? false) {
  226. // 如果是老师,自动设置老师ID
  227. request()->merge(['teacher_id' => $currentUser->teacher?->teacher_id]);
  228. }
  229. }
  230. }