MarkdownImportResource.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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\BulkActionGroup;
  7. use Filament\Actions\DeleteBulkAction;
  8. use Filament\Actions\EditAction;
  9. use Filament\Actions\Action;
  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\Section;
  16. use Filament\Forms\Components\Select;
  17. use Filament\Forms\Components\Toggle;
  18. use Filament\Forms\Components\TextInput;
  19. use Filament\Schemas\Components\Utilities\Get;
  20. use Filament\Schemas\Components\Utilities\Set;
  21. use Filament\Resources\Resource;
  22. use Filament\Schemas\Schema;
  23. use Filament\Tables;
  24. use Filament\Tables\Table;
  25. use Illuminate\Database\Eloquent\Builder;
  26. use Illuminate\Database\Eloquent\Model;
  27. use Illuminate\Support\Facades\Storage;
  28. use Illuminate\Support\Facades\DB;
  29. use UnitEnum;
  30. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  31. use App\Support\TextEncoding;
  32. use App\Rules\MarkdownFileExtension;
  33. use Filament\Tables\Columns\TextColumn;
  34. use Filament\Tables\Enums\FiltersLayout;
  35. class MarkdownImportResource extends Resource
  36. {
  37. protected static ?string $model = MarkdownImport::class;
  38. protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-arrow-up-tray';
  39. protected static ?string $navigationLabel = 'Markdown 导入';
  40. protected static ?string $modelLabel = 'Markdown 导入';
  41. protected static ?string $pluralModelLabel = 'Markdown 导入';
  42. protected static UnitEnum|string|null $navigationGroup = '卷子导入流程';
  43. protected static ?int $navigationSort = 1;
  44. protected static ?string $title = 'Markdown 试卷导入管理';
  45. protected static ?string $description = '导入 Markdown 格式的数学试卷,AI 智能识别题目,人工校对后入库';
  46. public static function mutateFormDataBeforeCreate(array $data): array
  47. {
  48. // 支持上传 markdown 文件:读取内容写入 original_markdown
  49. if (!empty($data['markdown_file']) && empty($data['original_markdown'])) {
  50. $path = $data['markdown_file'];
  51. if (is_string($path) && Storage::disk('local')->exists($path)) {
  52. $data['original_markdown'] = TextEncoding::toUtf8(Storage::disk('local')->get($path));
  53. }
  54. }
  55. // 文件名默认取上传文件名(优先原始文件名,其次取存储路径 basename)
  56. if (empty($data['file_name']) && !empty($data['markdown_file'])) {
  57. $storedNames = $data['uploaded_file_names'] ?? null;
  58. if (is_array($storedNames) && !empty($storedNames)) {
  59. $data['file_name'] = (string) array_values($storedNames)[0];
  60. } else {
  61. $path = is_array($data['markdown_file']) ? ($data['markdown_file'][0] ?? '') : (string) $data['markdown_file'];
  62. $data['file_name'] = $path !== '' ? basename($path) : null;
  63. }
  64. }
  65. // 文件名作为来源名称
  66. if (!empty($data['file_name'])) {
  67. $data['source_name'] = $data['file_name'];
  68. $data['source_type'] = 'other';
  69. }
  70. unset($data['markdown_file']);
  71. unset($data['uploaded_file_names']);
  72. return $data;
  73. }
  74. /**
  75. * 允许创建新的 Markdown 导入记录
  76. */
  77. public static function canCreate(): bool
  78. {
  79. return true;
  80. }
  81. public static function form(Schema $schema): Schema
  82. {
  83. return $schema
  84. ->schema([
  85. Section::make('上传与来源信息')
  86. ->schema([
  87. \Filament\Forms\Components\TextInput::make('file_name')
  88. ->label('文件名(来源名称)')
  89. ->required(fn (Get $get): bool => empty($get('markdown_file')))
  90. ->maxLength(255),
  91. FileUpload::make('markdown_file')
  92. ->label('Markdown 文件(可选)')
  93. ->disk('local')
  94. ->directory('imports/markdown')
  95. ->helperText('仅支持 .md / .markdown / .txt;上传后会自动读取内容并填充编辑器')
  96. ->maxSize(10 * 1024)
  97. ->storeFileNamesIn('uploaded_file_names')
  98. ->dehydrated(true)
  99. ->preserveFilenames()
  100. ->rules([new MarkdownFileExtension()])
  101. ->afterStateUpdated(function ($state, Set $set, Get $get): void {
  102. // 在提交表单前,FileUpload 的 state 可能还是 TemporaryUploadedFile(尚未保存到 disk)
  103. $first = is_array($state) ? ($state[0] ?? null) : $state;
  104. if ($first instanceof TemporaryUploadedFile) {
  105. $set('original_markdown', TextEncoding::toUtf8((string) @file_get_contents($first->getRealPath())));
  106. if (empty($get('file_name'))) {
  107. $set('file_name', $first->getClientOriginalName());
  108. }
  109. return;
  110. }
  111. $paths = is_array($state) ? $state : (empty($state) ? [] : [$state]);
  112. $path = (string) ($paths[0] ?? '');
  113. if ($path === '') {
  114. return;
  115. }
  116. // 已保存到 disk 后:读取文件内容填充编辑器
  117. if (Storage::disk('local')->exists($path)) {
  118. $set('original_markdown', TextEncoding::toUtf8(Storage::disk('local')->get($path)));
  119. }
  120. // 上传后的真实文件名:BaseFileUpload 会在保存时 storeFileName($storedFile, originalName)
  121. $storedNames = $get('uploaded_file_names');
  122. if (is_string($storedNames) && $storedNames !== '') {
  123. $set('file_name', $storedNames);
  124. } elseif (empty($get('file_name'))) {
  125. $set('file_name', basename($path));
  126. }
  127. }),
  128. Hidden::make('uploaded_file_names')
  129. ->dehydrated(true),
  130. ])
  131. ->columns(2),
  132. Section::make('解析规则(可选)')
  133. ->schema([
  134. Select::make('parse_mode')
  135. ->label('解析模式')
  136. ->options([
  137. 'strict' => '严格模式',
  138. 'relaxed' => '宽松模式',
  139. ])
  140. ->default('strict')
  141. ->dehydrated(false),
  142. TextInput::make('split_marker')
  143. ->label('分题符号')
  144. ->placeholder('如:---')
  145. ->dehydrated(false),
  146. TextInput::make('type_marker')
  147. ->label('题型标记')
  148. ->placeholder('如:#选择题')
  149. ->dehydrated(false),
  150. Toggle::make('auto_detect_images')
  151. ->label('自动识别图片')
  152. ->default(true)
  153. ->dehydrated(false),
  154. ])
  155. ->columns(2)
  156. ->collapsed(),
  157. Section::make('Markdown 内容')
  158. ->schema([
  159. MarkdownEditor::make('original_markdown')
  160. ->label('Markdown 内容(编辑器)')
  161. ->required(fn (Get $get): bool => empty($get('markdown_file')))
  162. ->columnSpanFull()
  163. // 固定编辑器高度,避免内容过长把页面撑开
  164. ->minHeight('45vh')
  165. ->maxHeight('45vh')
  166. ->toolbarButtons([
  167. 'bold',
  168. 'italic',
  169. 'strike',
  170. 'blockquote',
  171. 'bulletList',
  172. 'orderedList',
  173. 'link',
  174. 'codeBlock',
  175. 'table',
  176. 'undo',
  177. 'redo',
  178. ]),
  179. ]),
  180. ]);
  181. }
  182. public static function table(Table $table): Table
  183. {
  184. return $table
  185. ->columns([
  186. TextColumn::make('file_name')
  187. ->label('文件名')
  188. ->searchable()
  189. ->sortable(),
  190. TextColumn::make('filename_parse_status')
  191. ->label('命名解析')
  192. ->badge()
  193. ->getStateUsing(function (?Model $record): string {
  194. if (!$record) {
  195. return '未知';
  196. }
  197. $parsed = $record->parseFilename();
  198. return empty($parsed) ? '不规范' : '正常';
  199. })
  200. ->color(function (?Model $record): string {
  201. if (!$record) {
  202. return 'gray';
  203. }
  204. return empty($record->parseFilename()) ? 'warning' : 'success';
  205. })
  206. ->tooltip(function (?Model $record): ?string {
  207. if (!$record) {
  208. return null;
  209. }
  210. $parsed = $record->parseFilename();
  211. if (empty($parsed)) {
  212. return '系列_年级_学期_学科_名称';
  213. }
  214. return sprintf(
  215. '系列:%s 年级:%s 学期:%s 学科:%s 名称:%s',
  216. $parsed['series'] ?? '-',
  217. $parsed['grade'] ?? '-',
  218. $parsed['term'] ?? '-',
  219. $parsed['subject'] ?? '-',
  220. $parsed['name'] ?? '-'
  221. );
  222. })
  223. ->url(fn (?Model $record): ?string => $record ? route('filament.admin.pages.markdown-import-workbench', [
  224. 'import_id' => $record->id,
  225. ]) : null)
  226. ->openUrlInNewTab(),
  227. TextColumn::make('source_name')
  228. ->label('来源')
  229. ->toggleable(isToggledHiddenByDefault: true),
  230. TextColumn::make('status')
  231. ->label('状态')
  232. ->badge()
  233. ->color(fn (string $state): string => match ($state) {
  234. 'pending' => 'gray',
  235. 'processing' => 'warning',
  236. 'parsed' => 'info',
  237. 'reviewed' => 'primary',
  238. 'completed' => 'success',
  239. 'failed' => 'danger',
  240. default => 'gray',
  241. })
  242. ->getStateUsing(function (?Model $record): string {
  243. if (!$record) {
  244. return '—';
  245. }
  246. return match ($record->status) {
  247. 'pending' => '待处理',
  248. 'processing' => $record->progress_label ?: '处理中',
  249. 'parsed' => '已解析(待校对)',
  250. 'reviewed' => '已校对(待入库)',
  251. 'completed' => '已完成(已入库)',
  252. 'failed' => '失败' . ($record->progress_message ? "({$record->progress_message})" : ''),
  253. default => (string) $record->status,
  254. };
  255. }),
  256. TextColumn::make('progress_message')
  257. ->label('当前步骤')
  258. ->getStateUsing(fn (?Model $record) => $record?->progress_message ?: '—')
  259. ->wrap()
  260. ->limit(60),
  261. TextColumn::make('progress_label')
  262. ->label('进度')
  263. ->getStateUsing(fn (?Model $record) => $record?->progress_label ?: '—')
  264. ->color('gray'),
  265. TextColumn::make('parsed_count')
  266. ->label('候选题数')
  267. ->getStateUsing(fn (?Model $record) => $record?->parsed_count ?? 0)
  268. ->sortable(),
  269. TextColumn::make('accepted_count')
  270. ->label('已接受')
  271. ->getStateUsing(fn (?Model $record) => $record?->accepted_count ?? 0)
  272. ->sortable(),
  273. TextColumn::make('created_at')
  274. ->label('导入时间')
  275. ->dateTime()
  276. ->sortable(),
  277. TextColumn::make('processing_started_at')
  278. ->label('开始')
  279. ->dateTime('m-d H:i')
  280. ->toggleable(isToggledHiddenByDefault: true),
  281. TextColumn::make('processing_finished_at')
  282. ->label('结束')
  283. ->dateTime('m-d H:i')
  284. ->toggleable(isToggledHiddenByDefault: true),
  285. TextColumn::make('error_message')
  286. ->label('错误')
  287. ->visible(fn (?Model $record): bool => $record?->status === 'failed')
  288. ->wrap()
  289. ->limit(80),
  290. ])
  291. ->filters([
  292. Tables\Filters\SelectFilter::make('status')
  293. ->label('状态')
  294. ->options([
  295. 'pending' => '待处理',
  296. 'processing' => '处理中',
  297. 'parsed' => '已解析',
  298. 'reviewed' => '已校对',
  299. 'completed' => '已完成',
  300. 'failed' => '处理失败',
  301. ]),
  302. Tables\Filters\SelectFilter::make('source_type')
  303. ->label('来源类型')
  304. ->options([
  305. 'textbook' => '教材',
  306. 'exam' => '考试',
  307. 'other' => '其他',
  308. ]),
  309. Tables\Filters\SelectFilter::make('filename_parse')
  310. ->label('命名规范')
  311. ->options([
  312. 'valid' => '正常',
  313. 'invalid' => '不规范',
  314. ])
  315. ->query(function (Builder $query, array $data) {
  316. $value = $data['value'] ?? null;
  317. $driver = DB::getDriverName();
  318. $regex = '^.+_[0-9]+_[0-2]_.+_.+$';
  319. if ($value === 'valid') {
  320. if ($driver === 'mysql') {
  321. $query->whereRaw('file_name REGEXP ?', [$regex]);
  322. } else {
  323. $query->where('file_name', 'like', '%_%_%_%_%');
  324. }
  325. }
  326. if ($value === 'invalid') {
  327. if ($driver === 'mysql') {
  328. $query->where(function ($q) use ($regex) {
  329. $q->whereNull('file_name')->orWhereRaw('file_name NOT REGEXP ?', [$regex]);
  330. });
  331. } else {
  332. $query->where(function ($q) {
  333. $q->whereNull('file_name')->orWhere('file_name', 'not like', '%_%_%_%_%');
  334. });
  335. }
  336. }
  337. }),
  338. ], layout: FiltersLayout::AboveContentCollapsible)
  339. ->actions([
  340. EditAction::make()
  341. ->label('编辑'),
  342. Action::make('workbench')
  343. ->label('导入工作台')
  344. ->icon('heroicon-o-rectangle-stack')
  345. ->color('primary')
  346. ->visible(fn (?Model $record): bool => !empty($record?->parseFilename()))
  347. ->url(fn (?Model $record): string => route('filament.admin.pages.markdown-import-workbench', [
  348. 'import_id' => $record?->id,
  349. ])),
  350. Action::make('run_pipeline')
  351. ->label('触发全流程')
  352. ->icon('heroicon-o-play-circle')
  353. ->color('success')
  354. ->requiresConfirmation()
  355. ->modalHeading('触发 Markdown 拆分 + AI 结构化')
  356. ->modalDescription('立即提交队列,按 source_file → source_paper → paper_part → candidate → AI 结构化 执行。')
  357. ->action(function (?Model $record) {
  358. if (!$record) {
  359. return;
  360. }
  361. dispatch(new \App\Jobs\ProcessMarkdownSplit($record->id));
  362. $record->update([
  363. 'status' => MarkdownImport::STATUS_PROCESSING,
  364. 'progress_stage' => MarkdownImport::STAGE_QUEUED,
  365. 'progress_message' => '已进入队列…',
  366. 'processing_started_at' => now(),
  367. 'processing_finished_at' => null,
  368. 'error_message' => null,
  369. ]);
  370. Notification::make()
  371. ->title('已提交解析队列')
  372. ->success()
  373. ->send();
  374. }),
  375. Action::make('parse')
  376. ->label('解析 Markdown')
  377. ->icon('heroicon-o-cog-6-tooth')
  378. ->color('info')
  379. ->visible(fn (?Model $record): bool => in_array($record?->status, ['pending', 'failed']))
  380. ->requiresConfirmation()
  381. ->modalHeading('解析 Markdown')
  382. ->modalDescription('将解析 Markdown 中的题目候选,并使用 AI 进行初步筛选。')
  383. ->action(function (?Model $record) {
  384. if ($record) {
  385. static::parseMarkdown($record);
  386. }
  387. }),
  388. Action::make('review')
  389. ->label('进入校对')
  390. ->icon('heroicon-o-clipboard-document-list')
  391. ->color('success')
  392. ->visible(fn (?Model $record): bool => in_array($record?->status, ['parsed', 'reviewed', 'completed']) && !empty($record?->parseFilename()))
  393. ->url(function (?Model $record): string {
  394. // 根据状态跳转到不同页面
  395. $importId = $record?->id;
  396. $status = $record?->status;
  397. // 兼容 PHP 7.4 的写法
  398. if ($status === 'parsed') {
  399. return route('filament.admin.resources.pre-question-candidates.index', [
  400. 'import_id' => $importId
  401. ]);
  402. } elseif (in_array($status, ['reviewed', 'completed'])) {
  403. return route('filament.admin.resources.pre-question-candidates.index', [
  404. 'import_id' => $importId,
  405. 'tab' => 'reviewed' // 显示已校对标签页
  406. ]);
  407. }
  408. return route('filament.admin.resources.pre-question-candidates.index', [
  409. 'import_id' => $importId
  410. ]);
  411. }),
  412. Action::make('delete')
  413. ->label('删除')
  414. ->icon('heroicon-o-trash')
  415. ->color('danger')
  416. ->requiresConfirmation()
  417. ->modalHeading('删除导入记录')
  418. ->modalDescription('确定要删除这条导入记录吗?此操作不可撤销。')
  419. ->action(function (?Model $record) {
  420. if ($record) {
  421. $record->delete();
  422. Notification::make()
  423. ->title('删除成功')
  424. ->success()
  425. ->send();
  426. }
  427. }),
  428. ])
  429. ->bulkActions([
  430. BulkActionGroup::make([
  431. DeleteBulkAction::make(),
  432. ]),
  433. ])
  434. ->recordClasses(fn (Model $record) => $record->status === 'failed' ? 'bg-rose-50/60' : null)
  435. ->defaultSort('created_at', 'desc')
  436. ->paginated([10, 25, 50, 100]);
  437. }
  438. public static function getEloquentQuery(): Builder
  439. {
  440. // 让 parsed_count / accepted_count 成为可排序的 SQL 字段(避免 order by accessor 报错)
  441. return parent::getEloquentQuery()
  442. ->withCount([
  443. 'candidates as parsed_count',
  444. 'candidates as accepted_count' => fn (Builder $query) => $query->where('is_question_candidate', true),
  445. ]);
  446. }
  447. public static function getPages(): array
  448. {
  449. return [
  450. 'index' => Pages\ListMarkdownImports::route('/'),
  451. 'create' => Pages\CreateMarkdownImport::route('/create'),
  452. 'edit' => Pages\EditMarkdownImport::route('/{record}/edit'),
  453. ];
  454. }
  455. /**
  456. * 解析 Markdown
  457. */
  458. public static function parseMarkdown(Model $record): void
  459. {
  460. try {
  461. // 验证状态
  462. if (!in_array($record->status, ['pending', 'failed'], true)) {
  463. Notification::make()
  464. ->title('只能解析待处理或失败状态的记录')
  465. ->warning()
  466. ->send();
  467. return;
  468. }
  469. // 验证 markdown 内容
  470. if (empty($record->original_markdown)) {
  471. Notification::make()
  472. ->title('Markdown 内容不能为空')
  473. ->warning()
  474. ->send();
  475. return;
  476. }
  477. // 失败状态重试:清空错误信息并重新进入待处理
  478. if ($record->status === 'failed') {
  479. $record->update([
  480. 'status' => 'pending',
  481. 'error_message' => null,
  482. ]);
  483. }
  484. // 先更新状态,确保列表页可见变化(避免“点了没反应”的体验)
  485. $record->update([
  486. 'status' => 'processing',
  487. 'progress_stage' => \App\Models\MarkdownImport::STAGE_QUEUED,
  488. 'progress_message' => '已提交解析任务,等待处理…',
  489. 'progress_current' => 0,
  490. 'progress_total' => 0,
  491. 'progress_updated_at' => now(),
  492. 'processing_started_at' => now(),
  493. 'processing_finished_at' => null,
  494. 'error_message' => null,
  495. ]);
  496. \Log::info('Markdown import parse queued', [
  497. 'import_id' => $record->id,
  498. 'status' => $record->status,
  499. 'stage' => $record->progress_stage,
  500. ]);
  501. // 派发异步任务
  502. \App\Jobs\ProcessMarkdownSplit::dispatch($record->id);
  503. Notification::make()
  504. ->title('已提交解析任务,正在后台处理...')
  505. ->body('列表页将自动刷新显示进度;若长期无进度,请确认 queue worker 正在运行。')
  506. ->success()
  507. ->send();
  508. } catch (\Exception $e) {
  509. Notification::make()
  510. ->title('解析失败:' . $e->getMessage())
  511. ->danger()
  512. ->send();
  513. }
  514. }
  515. }