Bladeren bron

操作试题修改总分

罗永浩 5 maanden geleden
bovenliggende
commit
2170c53b12
1 gewijzigde bestanden met toevoegingen van 57 en 4 verwijderingen
  1. 57 4
      app/Services/Exam/TopicService.php

+ 57 - 4
app/Services/Exam/TopicService.php

@@ -1,10 +1,12 @@
 <?php
+
 namespace App\Services\Exam;
 
 use App\Models\ExamTopicModel;
 use App\Models\ActionLogModel;
 use App\Services\BaseService;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
 
 /**
  * 题目管理-服务类
@@ -129,7 +131,7 @@ class TopicService extends BaseService
         }
     }
     /**
-     * 编辑或新增视频集
+     * 编辑或新增题目
      */
     public function edit()
     {
@@ -162,6 +164,7 @@ class TopicService extends BaseService
         }
 
         $id = $data['id'] ?? 0;
+        $paperId = $data['paper_id'] ?? 0;
 
         // 默认字段处理
         $data['update_time'] = time();
@@ -169,7 +172,44 @@ class TopicService extends BaseService
             $data['create_time'] = time();
         }
 
-        return parent::edit($data); // 调用父类的 edit 方法
+        // 调用父类的 edit 方法
+        $result = parent::edit($data);
+
+        // 如果保存成功,更新试卷统计信息
+        if ($result['code'] == 0 && $paperId) {
+            $this->updatePaperStatistics($paperId);
+        }
+
+        return $result;
+    }
+
+    /**
+     * 更新试卷统计信息(总分和题目数量)
+     */
+    private function updatePaperStatistics($paperId)
+    {
+        try {
+            // 获取该试卷下所有有效题目的统计信息
+            $statistics = $this->model
+                ->where('paper_id', $paperId)
+                ->where('mark', 1)
+                ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
+                ->first();
+
+            if ($statistics) {
+                // 更新试卷表
+                DB::table('exam_papers')
+                    ->where('id', $paperId)
+                    ->update([
+                        'topic_count' => $statistics->topic_count ?? 0,
+                        'score_total' => $statistics->score_total ?? 0,
+                        'update_time' => time()
+                    ]);
+            }
+        } catch (\Exception $e) {
+            // 记录错误但不影响题目保存
+            Log::error('更新试卷统计信息失败: ' . $e->getMessage());
+        }
     }
 
     /**
@@ -180,8 +220,21 @@ class TopicService extends BaseService
         // 设置日志标题
         ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除题目信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
         ActionLogModel::record();
+
+        // 获取要删除的题目信息
+        $id = request()->input('id', 0);
+        $topic = $this->model->find($id);
+        $paperId = $topic ? $topic->paper_id : 0;
+
+        // 执行删除操作
         $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
-        return parent::delete();
-    }
+        $result = parent::delete();
 
+        // 如果删除成功,更新试卷统计信息
+        if ($result['code'] == 0 && $paperId) {
+            $this->updatePaperStatistics($paperId);
+        }
+
+        return $result;
+    }
 }