MarkdownImportResource.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\MarkdownImportResource\Pages;
  4. use App\Models\MarkdownImport;
  5. use BackedEnum;
  6. use Filament\Actions\Action;
  7. use Filament\Actions\BulkActionGroup;
  8. use Filament\Actions\DeleteBulkAction;
  9. use Filament\Actions\EditAction;
  10. use Filament\Facades\Filament;
  11. use Filament\Notifications\Notification;
  12. use Filament\Forms\Components\FileUpload;
  13. use Filament\Forms\Components\Hidden;
  14. use Filament\Forms\Components\MarkdownEditor;
  15. use Filament\Schemas\Components\Utilities\Get;
  16. use Filament\Schemas\Components\Utilities\Set;
  17. use Filament\Resources\Resource;
  18. use Filament\Schemas\Schema;
  19. use Filament\Tables;
  20. use Filament\Tables\Table;
  21. use Illuminate\Database\Eloquent\Builder;
  22. use Illuminate\Database\Eloquent\Model;
  23. use Illuminate\Support\Facades\Storage;
  24. use UnitEnum;
  25. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  26. use App\Support\TextEncoding;
  27. use App\Rules\MarkdownFileExtension;
  28. class MarkdownImportResource extends Resource
  29. {
  30. protected static ?string $model = MarkdownImport::class;
  31. protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-document-arrow-down';
  32. protected static ?string $navigationLabel = 'Markdown 导入';
  33. protected static ?string $modelLabel = 'Markdown 导入';
  34. protected static ?string $pluralModelLabel = 'Markdown 导入';
  35. protected static UnitEnum|string|null $navigationGroup = '题库管理';
  36. protected static ?int $navigationSort = 1;
  37. protected static ?string $title = 'Markdown 试卷导入管理';
  38. protected static ?string $description = '导入 Markdown 格式的数学试卷,AI 智能识别题目,人工校对后入库';
  39. public static function mutateFormDataBeforeCreate(array $data): array
  40. {
  41. // 支持上传 markdown 文件:读取内容写入 original_markdown
  42. if (!empty($data['markdown_file']) && empty($data['original_markdown'])) {
  43. $path = $data['markdown_file'];
  44. if (is_string($path) && Storage::disk('local')->exists($path)) {
  45. $data['original_markdown'] = TextEncoding::toUtf8(Storage::disk('local')->get($path));
  46. }
  47. }
  48. // 文件名默认取上传文件名(优先原始文件名,其次取存储路径 basename)
  49. if (empty($data['file_name']) && !empty($data['markdown_file'])) {
  50. $storedNames = $data['uploaded_file_names'] ?? null;
  51. if (is_array($storedNames) && !empty($storedNames)) {
  52. $data['file_name'] = (string) array_values($storedNames)[0];
  53. } else {
  54. $path = is_array($data['markdown_file']) ? ($data['markdown_file'][0] ?? '') : (string) $data['markdown_file'];
  55. $data['file_name'] = $path !== '' ? basename($path) : null;
  56. }
  57. }
  58. // 文件名作为来源名称
  59. if (!empty($data['file_name'])) {
  60. $data['source_name'] = $data['file_name'];
  61. $data['source_type'] = 'other';
  62. }
  63. unset($data['markdown_file']);
  64. unset($data['uploaded_file_names']);
  65. return $data;
  66. }
  67. /**
  68. * 允许创建新的 Markdown 导入记录
  69. */
  70. public static function canCreate(): bool
  71. {
  72. return true;
  73. }
  74. public static function form(Schema $schema): Schema
  75. {
  76. return $schema
  77. ->schema([
  78. \Filament\Forms\Components\TextInput::make('file_name')
  79. ->label('文件名(来源名称)')
  80. ->required(fn (Get $get): bool => empty($get('markdown_file')))
  81. ->maxLength(255),
  82. FileUpload::make('markdown_file')
  83. ->label('Markdown 文件(可选)')
  84. ->disk('local')
  85. ->directory('imports/markdown')
  86. ->helperText('仅支持 .md / .markdown / .txt;上传后会自动读取内容并填充编辑器')
  87. ->maxSize(10 * 1024)
  88. ->storeFileNamesIn('uploaded_file_names')
  89. ->dehydrated(true)
  90. ->preserveFilenames()
  91. ->rules([new MarkdownFileExtension()])
  92. ->afterStateUpdated(function ($state, Set $set, Get $get): void {
  93. // 在提交表单前,FileUpload 的 state 可能还是 TemporaryUploadedFile(尚未保存到 disk)
  94. $first = is_array($state) ? ($state[0] ?? null) : $state;
  95. if ($first instanceof TemporaryUploadedFile) {
  96. $set('original_markdown', TextEncoding::toUtf8((string) @file_get_contents($first->getRealPath())));
  97. if (empty($get('file_name'))) {
  98. $set('file_name', $first->getClientOriginalName());
  99. }
  100. return;
  101. }
  102. $paths = is_array($state) ? $state : (empty($state) ? [] : [$state]);
  103. $path = (string) ($paths[0] ?? '');
  104. if ($path === '') {
  105. return;
  106. }
  107. // 已保存到 disk 后:读取文件内容填充编辑器
  108. if (Storage::disk('local')->exists($path)) {
  109. $set('original_markdown', TextEncoding::toUtf8(Storage::disk('local')->get($path)));
  110. }
  111. // 上传后的真实文件名:BaseFileUpload 会在保存时 storeFileName($storedFile, originalName)
  112. $storedNames = $get('uploaded_file_names');
  113. if (is_string($storedNames) && $storedNames !== '') {
  114. $set('file_name', $storedNames);
  115. } elseif (empty($get('file_name'))) {
  116. $set('file_name', basename($path));
  117. }
  118. }),
  119. Hidden::make('uploaded_file_names')
  120. ->dehydrated(true),
  121. MarkdownEditor::make('original_markdown')
  122. ->label('Markdown 内容(编辑器)')
  123. ->required(fn (Get $get): bool => empty($get('markdown_file')))
  124. ->columnSpanFull()
  125. // 固定编辑器高度,避免内容过长把页面撑开
  126. ->minHeight('45vh')
  127. ->maxHeight('45vh')
  128. ->toolbarButtons([
  129. 'bold',
  130. 'italic',
  131. 'strike',
  132. 'blockquote',
  133. 'bulletList',
  134. 'orderedList',
  135. 'link',
  136. 'codeBlock',
  137. 'table',
  138. 'undo',
  139. 'redo',
  140. ]),
  141. ]);
  142. }
  143. public static function table(Table $table): Table
  144. {
  145. return $table
  146. ->columns([
  147. Tables\Columns\TextColumn::make('file_name')
  148. ->label('文件名')
  149. ->searchable()
  150. ->sortable(),
  151. Tables\Columns\TextColumn::make('source_type')
  152. ->label('来源类型')
  153. ->badge()
  154. ->color('gray'),
  155. Tables\Columns\TextColumn::make('source_name')
  156. ->label('来源名称')
  157. ->searchable(),
  158. Tables\Columns\TextColumn::make('status')
  159. ->label('状态')
  160. ->badge()
  161. ->color(fn (string $state): string => match ($state) {
  162. 'pending' => 'gray',
  163. 'processing' => 'warning',
  164. 'parsed' => 'info',
  165. 'reviewed' => 'primary',
  166. 'completed' => 'success',
  167. 'failed' => 'danger',
  168. default => 'gray',
  169. })
  170. ->getStateUsing(function (?Model $record): string {
  171. if (!$record) {
  172. return '—';
  173. }
  174. return match ($record->status) {
  175. 'pending' => '待处理',
  176. 'processing' => $record->progress_label ?: '处理中',
  177. 'parsed' => '已解析(待校对)',
  178. 'reviewed' => '已校对(待入库)',
  179. 'completed' => '已完成(已入库)',
  180. 'failed' => '失败' . ($record->progress_message ? "({$record->progress_message})" : ''),
  181. default => (string) $record->status,
  182. };
  183. }),
  184. Tables\Columns\TextColumn::make('progress_message')
  185. ->label('当前步骤')
  186. ->getStateUsing(fn (?Model $record) => $record?->progress_message ?: '—')
  187. ->wrap()
  188. ->limit(60),
  189. Tables\Columns\TextColumn::make('progress_updated_at')
  190. ->label('进度更新时间')
  191. ->dateTime('m-d H:i:s')
  192. ->sortable()
  193. ->toggleable(isToggledHiddenByDefault: true),
  194. Tables\Columns\TextColumn::make('parsed_count')
  195. ->label('候选题数')
  196. ->getStateUsing(fn (?Model $record) => $record?->parsed_count ?? 0)
  197. ->sortable(),
  198. Tables\Columns\TextColumn::make('accepted_count')
  199. ->label('已接受')
  200. ->getStateUsing(fn (?Model $record) => $record?->accepted_count ?? 0)
  201. ->sortable(),
  202. Tables\Columns\TextColumn::make('created_at')
  203. ->label('导入时间')
  204. ->dateTime()
  205. ->sortable(),
  206. Tables\Columns\TextColumn::make('error_message')
  207. ->label('错误信息')
  208. ->visible(fn (?Model $record): bool => $record?->status === 'failed')
  209. ->wrap()
  210. ->limit(50),
  211. ])
  212. ->filters([
  213. Tables\Filters\SelectFilter::make('status')
  214. ->label('状态')
  215. ->options([
  216. 'pending' => '待处理',
  217. 'processing' => '处理中',
  218. 'parsed' => '已解析',
  219. 'reviewed' => '已校对',
  220. 'completed' => '已完成',
  221. 'failed' => '处理失败',
  222. ]),
  223. Tables\Filters\SelectFilter::make('source_type')
  224. ->label('来源类型')
  225. ->options([
  226. 'textbook' => '教材',
  227. 'exam' => '考试',
  228. 'other' => '其他',
  229. ]),
  230. ])
  231. ->actions([
  232. EditAction::make()
  233. ->label('编辑'),
  234. Action::make('parse')
  235. ->label('解析 Markdown')
  236. ->icon('heroicon-o-cog-6-tooth')
  237. ->color('info')
  238. ->visible(fn (?Model $record): bool => in_array($record?->status, ['pending', 'failed']))
  239. ->requiresConfirmation()
  240. ->modalHeading('解析 Markdown')
  241. ->modalDescription('将解析 Markdown 中的题目候选,并使用 AI 进行初步筛选。')
  242. ->action(function (?Model $record) {
  243. if ($record) {
  244. static::parseMarkdown($record);
  245. }
  246. }),
  247. Action::make('review')
  248. ->label('进入校对')
  249. ->icon('heroicon-o-clipboard-document-list')
  250. ->color('success')
  251. ->visible(fn (?Model $record): bool => in_array($record?->status, ['parsed', 'reviewed', 'completed']))
  252. ->url(function (?Model $record): string {
  253. // 根据状态跳转到不同页面
  254. $importId = $record?->id;
  255. $status = $record?->status;
  256. // 兼容 PHP 7.4 的写法
  257. if ($status === 'parsed') {
  258. return route('filament.admin.resources.pre-question-candidates.index', [
  259. 'import_id' => $importId
  260. ]);
  261. } elseif (in_array($status, ['reviewed', 'completed'])) {
  262. return route('filament.admin.resources.pre-question-candidates.index', [
  263. 'import_id' => $importId,
  264. 'tab' => 'reviewed' // 显示已校对标签页
  265. ]);
  266. }
  267. return route('filament.admin.resources.pre-question-candidates.index', [
  268. 'import_id' => $importId
  269. ]);
  270. }),
  271. Action::make('delete')
  272. ->label('删除')
  273. ->icon('heroicon-o-trash')
  274. ->color('danger')
  275. ->requiresConfirmation()
  276. ->modalHeading('删除导入记录')
  277. ->modalDescription('确定要删除这条导入记录吗?此操作不可撤销。')
  278. ->action(function (?Model $record) {
  279. if ($record) {
  280. $record->delete();
  281. Notification::make()
  282. ->title('删除成功')
  283. ->success()
  284. ->send();
  285. }
  286. }),
  287. ])
  288. ->bulkActions([
  289. BulkActionGroup::make([
  290. DeleteBulkAction::make(),
  291. ]),
  292. ])
  293. ->defaultSort('created_at', 'desc')
  294. ->paginated([10, 25, 50, 100])
  295. ->poll('10s');
  296. }
  297. public static function getEloquentQuery(): Builder
  298. {
  299. // 让 parsed_count / accepted_count 成为可排序的 SQL 字段(避免 order by accessor 报错)
  300. return parent::getEloquentQuery()
  301. ->withCount([
  302. 'candidates as parsed_count',
  303. 'candidates as accepted_count' => fn (Builder $query) => $query->where('is_question_candidate', true),
  304. ]);
  305. }
  306. public static function getPages(): array
  307. {
  308. return [
  309. 'index' => Pages\ListMarkdownImports::route('/'),
  310. 'create' => Pages\CreateMarkdownImport::route('/create'),
  311. 'edit' => Pages\EditMarkdownImport::route('/{record}/edit'),
  312. ];
  313. }
  314. /**
  315. * 解析 Markdown
  316. */
  317. public static function parseMarkdown(Model $record): void
  318. {
  319. try {
  320. // 验证状态
  321. if (!in_array($record->status, ['pending', 'failed'], true)) {
  322. Notification::make()
  323. ->title('只能解析待处理或失败状态的记录')
  324. ->warning()
  325. ->send();
  326. return;
  327. }
  328. // 验证 markdown 内容
  329. if (empty($record->original_markdown)) {
  330. Notification::make()
  331. ->title('Markdown 内容不能为空')
  332. ->warning()
  333. ->send();
  334. return;
  335. }
  336. // 失败状态重试:清空错误信息并重新进入待处理
  337. if ($record->status === 'failed') {
  338. $record->update([
  339. 'status' => 'pending',
  340. 'error_message' => null,
  341. ]);
  342. }
  343. // 先更新状态,确保列表页可见变化(避免“点了没反应”的体验)
  344. $record->update([
  345. 'status' => 'processing',
  346. 'progress_stage' => \App\Models\MarkdownImport::STAGE_QUEUED,
  347. 'progress_message' => '已提交解析任务,等待处理…',
  348. 'progress_current' => 0,
  349. 'progress_total' => 0,
  350. 'progress_updated_at' => now(),
  351. 'processing_started_at' => now(),
  352. 'processing_finished_at' => null,
  353. 'error_message' => null,
  354. ]);
  355. \Log::info('Markdown import parse queued', [
  356. 'import_id' => $record->id,
  357. 'status' => $record->status,
  358. 'stage' => $record->progress_stage,
  359. ]);
  360. // 派发异步任务
  361. \App\Jobs\ProcessMarkdownSplit::dispatch($record->id);
  362. Notification::make()
  363. ->title('已提交解析任务,正在后台处理...')
  364. ->body('列表页将自动刷新显示进度;若长期无进度,请确认 queue worker 正在运行。')
  365. ->success()
  366. ->send();
  367. } catch (\Exception $e) {
  368. Notification::make()
  369. ->title('解析失败:' . $e->getMessage())
  370. ->danger()
  371. ->send();
  372. }
  373. }
  374. }