KnowledgeGraphManagement.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\KnowledgeGraphService;
  4. use Filament\Actions\Action;
  5. use Filament\Forms\Components\FileUpload;
  6. use Filament\Forms\Components\Select;
  7. use Filament\Forms\Components\Textarea;
  8. use Filament\Forms\Components\TextInput;
  9. use Filament\Notifications\Notification;
  10. use Filament\Pages\Page;
  11. use Filament\Support\Enums\ActionSize;
  12. use Illuminate\Support\Facades\Storage;
  13. class KnowledgeGraphManagement extends Page
  14. {
  15. protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-rectangle-stack';
  16. protected static string|\UnitEnum|null $navigationGroup = '知识图谱系统';
  17. protected static ?int $navigationSort = 1;
  18. protected static ?string $navigationLabel = '知识图谱管理';
  19. protected static ?string $title = '知识图谱管理';
  20. protected string $view = 'filament.pages.knowledge-graph-management';
  21. public array $knowledgePoints = [];
  22. public function mount(KnowledgeGraphService $service): void
  23. {
  24. $this->knowledgePoints = $service->listKnowledgePoints(1, 1000);
  25. }
  26. public function edit(string $code): void
  27. {
  28. $this->mountAction('edit', ['code' => $code]);
  29. }
  30. public function delete(string $code): void
  31. {
  32. $this->mountAction('delete', ['code' => $code]);
  33. }
  34. protected function getHeaderActions(): array
  35. {
  36. return [
  37. Action::make('create')
  38. ->label('新增知识点')
  39. ->form([
  40. TextInput::make('cn_name')->label('中文名称')->required(),
  41. TextInput::make('en_name')->label('英文名称'),
  42. TextInput::make('kp_code')->label('知识点编码')->placeholder('自动生成或手动输入'),
  43. Select::make('phase')->label('学段')->options([
  44. '小学' => '小学', '初中' => '初中', '高中' => '高中'
  45. ])->required(),
  46. Select::make('grade')->label('年级')->options([
  47. 7 => '七年级', 8 => '八年级', 9 => '九年级'
  48. ]),
  49. TextInput::make('category')->label('分类')->default('数学'),
  50. TextInput::make('importance')->label('重要性')->numeric()->default(5),
  51. Textarea::make('description')->label('描述'),
  52. ])
  53. ->action(function (array $data, KnowledgeGraphService $service) {
  54. if ($service->createKnowledgePoint($data)) {
  55. Notification::make()->title('创建成功')->success()->send();
  56. $this->mount($service);
  57. } else {
  58. Notification::make()->title('创建失败')->danger()->send();
  59. }
  60. }),
  61. Action::make('import')
  62. ->label('导入图谱数据')
  63. ->color('gray')
  64. ->form([
  65. FileUpload::make('tree_file')
  66. ->label('Tree JSON (知识点结构)')
  67. ->required()
  68. ->storeFiles(false),
  69. FileUpload::make('edges_file')
  70. ->label('Edges JSON (依赖关系)')
  71. ->required()
  72. ->storeFiles(false),
  73. ])
  74. ->action(function (array $data, KnowledgeGraphService $service) {
  75. try {
  76. $treePath = $data['tree_file'];
  77. $edgesPath = $data['edges_file'];
  78. if (is_array($treePath)) $treePath = reset($treePath);
  79. if (is_array($edgesPath)) $edgesPath = reset($edgesPath);
  80. $treeContent = json_decode(Storage::disk('public')->get($treePath), true);
  81. $edgesContent = json_decode(Storage::disk('public')->get($edgesPath), true);
  82. if (!$treeContent || !$edgesContent) {
  83. Notification::make()->title('文件解析失败')->danger()->send();
  84. return;
  85. }
  86. if ($service->importGraph($treeContent, $edgesContent)) {
  87. Notification::make()->title('导入成功')->success()->send();
  88. $this->mount($service);
  89. } else {
  90. Notification::make()->title('导入失败')->danger()->send();
  91. }
  92. } catch (\Exception $e) {
  93. Notification::make()->title('导入异常')->body($e->getMessage())->danger()->send();
  94. }
  95. })
  96. ];
  97. }
  98. public function editAction(): Action
  99. {
  100. return Action::make('edit')
  101. ->label('编辑')
  102. ->modalHeading('编辑知识点')
  103. ->form([
  104. TextInput::make('cn_name')->label('中文名称')->required(),
  105. TextInput::make('en_name')->label('英文名称'),
  106. Select::make('phase')->label('学段')->options([
  107. '小学' => '小学', '初中' => '初中', '高中' => '高中'
  108. ])->required(),
  109. Select::make('grade')->label('年级')->options([
  110. 7 => '七年级', 8 => '八年级', 9 => '九年级'
  111. ]),
  112. TextInput::make('category')->label('分类'),
  113. TextInput::make('importance')->label('重要性')->numeric(),
  114. Textarea::make('description')->label('描述'),
  115. ])
  116. ->fillForm(function (array $arguments, KnowledgeGraphService $service) {
  117. $code = $arguments['code'];
  118. $data = $service->getKnowledgePoint($code);
  119. return $data ?? [];
  120. })
  121. ->action(function (array $data, array $arguments, KnowledgeGraphService $service) {
  122. if ($service->updateKnowledgePoint($arguments['code'], $data)) {
  123. Notification::make()->title('更新成功')->success()->send();
  124. $this->mount($service);
  125. } else {
  126. Notification::make()->title('更新失败')->danger()->send();
  127. }
  128. });
  129. }
  130. public function deleteAction(): Action
  131. {
  132. return Action::make('delete')
  133. ->label('删除')
  134. ->requiresConfirmation()
  135. ->action(function (array $arguments, KnowledgeGraphService $service) {
  136. if ($service->deleteKnowledgePoint($arguments['code'])) {
  137. Notification::make()->title('删除成功')->success()->send();
  138. $this->mount($service);
  139. } else {
  140. Notification::make()->title('删除失败')->danger()->send();
  141. }
  142. });
  143. }
  144. }