소스 검색

题库增加修改等功能

yemeishu 1 개월 전
부모
커밋
e9d81d9b5e

+ 63 - 0
app/Filament/Pages/QuestionManagement.php

@@ -27,6 +27,8 @@ class QuestionManagement extends Page
     public ?string $selectedType = null;
     public int $currentPage = 1;
     public int $perPage = 25;
+    public bool $showDetailModal = false;
+    public array $editing = [];
 
 
     #[Computed(cache: false)]
@@ -120,6 +122,67 @@ class QuestionManagement extends Page
         $this->currentPage = 1;
     }
 
+    public function viewQuestion(string $questionCode): void
+    {
+        $service = app(QuestionBankService::class);
+        $detail = $service->getQuestion($questionCode);
+
+        if (!$detail) {
+            Notification::make()
+                ->title('获取题目详情失败')
+                ->danger()
+            ->send();
+            return;
+        }
+
+        // 将技能数组转为逗号字符串便于编辑
+        if (isset($detail['skills']) && is_array($detail['skills'])) {
+            $detail['skills_text'] = implode(',', $detail['skills']);
+        } else {
+            $detail['skills_text'] = (string)($detail['skills'] ?? '');
+        }
+
+        $this->editing = $detail;
+        $this->showDetailModal = true;
+    }
+
+    public function saveQuestion(): void
+    {
+        if (empty($this->editing['question_code'])) {
+            Notification::make()->title('缺少题目编号').danger()->send();
+            return;
+        }
+
+        $payload = [
+            'stem' => $this->editing['stem'] ?? '',
+            'answer' => $this->editing['answer'] ?? '',
+            'solution' => $this->editing['solution'] ?? '',
+            'difficulty' => $this->editing['difficulty'] ?? null,
+            'tags' => $this->editing['tags'] ?? null,
+            'question_type' => $this->editing['question_type'] ?? null,
+            'kp_code' => $this->editing['kp_code'] ?? null,
+        ];
+
+        // skills 处理
+        $skillsText = $this->editing['skills_text'] ?? '';
+        $payload['skills'] = $skillsText;
+
+        $service = app(QuestionBankService::class);
+        $ok = $service->updateQuestion($this->editing['question_code'], array_filter(
+            $payload,
+            fn($v) => $v !== null
+        ));
+
+        if ($ok) {
+            Notification::make()->title('保存成功')->success()->send();
+            $this->showDetailModal = false;
+            $this->dispatch('$refresh');
+            $this->dispatch('math:render');
+        } else {
+            Notification::make()->title('保存失败')->danger()->send();
+        }
+    }
+
     public function gotoPage(int $page): void
     {
         $this->currentPage = $page;

+ 55 - 0
app/Services/QuestionBankService.php

@@ -45,6 +45,61 @@ class QuestionBankService
         return ['data' => [], 'meta' => ['total' => 0]];
     }
 
+    /**
+     * 获取题目详情
+     */
+    public function getQuestion(string $questionCode): ?array
+    {
+        try {
+            $response = Http::timeout(10)
+                ->get($this->baseUrl . "/questions/{$questionCode}");
+
+            if ($response->successful()) {
+                return $response->json();
+            }
+
+            Log::warning('获取题目详情失败', [
+                'code' => $questionCode,
+                'status' => $response->status()
+            ]);
+        } catch (\Exception $e) {
+            Log::error('获取题目详情异常', [
+                'code' => $questionCode,
+                'error' => $e->getMessage()
+            ]);
+        }
+
+        return null;
+    }
+
+    /**
+     * 更新题目
+     */
+    public function updateQuestion(string $questionCode, array $payload): bool
+    {
+        try {
+            $response = Http::timeout(10)
+                ->patch($this->baseUrl . "/questions/{$questionCode}", $payload);
+
+            if ($response->successful()) {
+                return true;
+            }
+
+            Log::warning('更新题目失败', [
+                'code' => $questionCode,
+                'status' => $response->status(),
+                'body' => $response->json(),
+            ]);
+        } catch (\Exception $e) {
+            Log::error('更新题目异常', [
+                'code' => $questionCode,
+                'error' => $e->getMessage()
+            ]);
+        }
+
+        return false;
+    }
+
     /**
      * 筛选题目 (支持 kp_codes, skills 等高级筛选)
      */

+ 81 - 2
resources/views/filament/pages/question-management-simple.blade.php

@@ -98,6 +98,7 @@
                 <tr>
                     <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">题目编号</th>
                     <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">知识点</th>
+                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">创建时间</th>
                     <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">题干</th>
                     <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">难度</th>
                     <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">操作</th>
@@ -107,7 +108,19 @@
                 @forelse($questionsData as $question)
                     <tr class="hover:bg-gray-50">
                         <td class="px-6 py-4 whitespace-nowrap">{{ $question['question_code'] ?? 'N/A' }}</td>
-                        <td class="px-6 py-4 whitespace-nowrap">{{ $question['kp_code'] ?? 'N/A' }}</td>
+                        <td class="px-6 py-4 whitespace-nowrap">
+                            <div class="text-sm font-medium text-gray-900">{{ $question['kp_name'] ?? $question['kp_code'] ?? 'N/A' }}</div>
+                            @if(!empty($question['kp_code']))
+                                <div class="text-xs text-gray-500">{{ $question['kp_code'] }}</div>
+                            @endif
+                        </td>
+                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600">
+                            @if(!empty($question['created_at']))
+                                {{ \Carbon\Carbon::parse($question['created_at'])->toDateTimeString() }}
+                            @else
+                                -
+                            @endif
+                        </td>
                         <td class="px-6 py-4" style="word-wrap: break-word; white-space: normal; line-height: 1.8; max-width: 400px;">
                             <span class="text-sm">
                                 @math($question['stem'] ?? 'N/A')
@@ -129,6 +142,7 @@
                             @endif
                         </td>
                         <td class="px-6 py-4 whitespace-nowrap">
+                            <button wire:click="viewQuestion('{{ $question['question_code'] }}')" class="text-blue-600 hover:underline mr-3">详情</button>
                             <button wire:click="deleteQuestion('{{ $question['question_code'] }}')" class="text-red-600 hover:underline">删除</button>
                         </td>
                     </tr>
@@ -151,6 +165,71 @@
             </div>
         @endif
     </div>
+
+    {{-- 详情侧边栏 --}}
+    @if($showDetailModal)
+        <div class="fixed inset-0 z-40 flex">
+            <div class="flex-1 bg-black/40" wire:click="$set('showDetailModal', false)"></div>
+            <div class="w-full max-w-3xl bg-white shadow-xl overflow-y-auto">
+                <div class="p-6 border-b flex justify-between items-center">
+                    <div>
+                        <h3 class="text-lg font-semibold">{{ $editing['question_code'] ?? '题目详情' }}</h3>
+                        <p class="text-sm text-gray-500">
+                            {{ $editing['kp_name'] ?? ($editing['kp_code'] ?? '') }}
+                            @if(!empty($editing['kp_code'])) ({{ $editing['kp_code'] }}) @endif
+                        </p>
+                    </div>
+                    <button class="text-gray-500 hover:text-gray-700" wire:click="$set('showDetailModal', false)">&times;</button>
+                </div>
+                <div class="p-6 space-y-4">
+                    <div>
+                        <label class="text-sm font-medium text-gray-700">知识点代码</label>
+                        <input type="text" wire:model="editing.kp_code" class="w-full border rounded px-3 py-2 text-sm">
+                    </div>
+                    <div>
+                        <label class="text-sm font-medium text-gray-700">题干</label>
+                        <textarea wire:model="editing.stem" rows="4" class="w-full border rounded px-3 py-2 text-sm"></textarea>
+                    </div>
+                    <div>
+                        <label class="text-sm font-medium text-gray-700">答案</label>
+                        <textarea wire:model="editing.answer" rows="3" class="w-full border rounded px-3 py-2 text-sm"></textarea>
+                    </div>
+                    <div>
+                        <label class="text-sm font-medium text-gray-700">解析</label>
+                        <textarea wire:model="editing.solution" rows="4" class="w-full border rounded px-3 py-2 text-sm"></textarea>
+                    </div>
+                    <div class="grid grid-cols-2 gap-4 text-sm">
+                        <div>
+                            <label class="text-sm font-medium text-gray-700">难度 (0-1)</label>
+                            <input type="number" step="0.01" min="0" max="1" wire:model="editing.difficulty" class="w-full border rounded px-3 py-2 text-sm">
+                        </div>
+                        <div>
+                            <label class="text-sm font-medium text-gray-700">题型</label>
+                            <input type="text" wire:model="editing.question_type" class="w-full border rounded px-3 py-2 text-sm" placeholder="choice/fill/answer">
+                        </div>
+                        <div>
+                            <label class="text-sm font-medium text-gray-700">来源</label>
+                            <input type="text" wire:model="editing.source" class="w-full border rounded px-3 py-2 text-sm">
+                        </div>
+                        <div>
+                            <label class="text-sm font-medium text-gray-700">标签</label>
+                            <input type="text" wire:model="editing.tags" class="w-full border rounded px-3 py-2 text-sm">
+                        </div>
+                        <div class="col-span-2">
+                            <label class="text-sm font-medium text-gray-700">技能(逗号分隔)</label>
+                            <input type="text" wire:model="editing.skills_text" class="w-full border rounded px-3 py-2 text-sm">
+                        </div>
+                        <div><span class="font-medium">创建时间:</span>{{ $editing['created_at'] ?? '-' }}</div>
+                        <div><span class="font-medium">更新时间:</span>{{ $editing['updated_at'] ?? '-' }}</div>
+                    </div>
+                </div>
+                <div class="p-6 border-t flex justify-end gap-3">
+                    <button class="px-4 py-2 border rounded text-sm" wire:click="$set('showDetailModal', false)">取消</button>
+                    <button class="px-4 py-2 bg-blue-600 text-white rounded text-sm hover:bg-blue-700" wire:click="saveQuestion">保存</button>
+                </div>
+            </div>
+        </div>
+    @endif
 </div>
 
-</x-filament-panels::page>
+</x-filament-panels::page>