Ver código fonte

处理试题总分为0问题

罗永浩 5 meses atrás
pai
commit
47a2326b67
1 arquivos alterados com 95 adições e 1 exclusões
  1. 95 1
      app/Services/Exam/PaperService.php

+ 95 - 1
app/Services/Exam/PaperService.php

@@ -62,7 +62,6 @@ class PaperService extends BaseService
             'total' => isset($list['total']) ? $list['total'] : 0,
             'list' => isset($list['data']) ? $list['data'] : []
         ];
-        return message("操作成功", true, $list);
     }
 
     public function createPaperWithTopics(array $formData, array $topicsData)
@@ -119,6 +118,101 @@ class PaperService extends BaseService
     }
 
     /**
+     * 获取记录详情
+     * @return array
+     * @since 2020/11/11
+     * @author laravel开发员
+     */
+    public function info()
+    {
+        // 记录ID
+        $id = request()->input("id", 0);
+        $info = [];
+        if ($id) {
+            $info = $this->model->getInfo($id);
+
+            // 如果试题数量或总分为空,从数据库重新获取
+            if ($info && (empty($info['topic_count']) || empty($info['score_total']))) {
+                // 获取该试卷下所有有效题目的统计信息
+                $topicModel = new ExamTopicModel();
+                $statistics = $topicModel
+                    ->where('paper_id', $id)
+                    ->where('mark', 1)
+                    ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
+                    ->first();
+
+                if ($statistics) {
+                    // 如果试题数量为空,使用数据库中的值
+                    if (empty($info['topic_count'])) {
+                        $info['topic_count'] = intval($statistics->topic_count ?? 0);
+                    }
+                    // 如果总分为空,使用数据库中的值
+                    if (empty($info['score_total'])) {
+                        $info['score_total'] = intval($statistics->score_total ?? 0);
+                    }
+                }
+            }
+        }
+        return message(MESSAGE_OK, true, $info);
+    }
+
+    /**
+     * 添加或编辑记录
+     * @return array
+     * @since 2020/11/11
+     * @author laravel开发员
+     */
+    public function edit()
+    {
+        // 获取参数
+        $argList = func_get_args();
+        // 查询条件
+        $data = isset($argList[0]) ? $argList[0] : [];
+        // 是否打印SQL
+        $is_sql = isset($argList[1]) ? $argList[1] : false;
+        if (!$data) {
+            $data = request()->all();
+        }
+
+        // 获取试卷ID
+        $paperId = isset($data['id']) ? intval($data['id']) : 0;
+
+        // 如果试题数量或总分为空,从数据库重新获取
+        // 检查字段是否为 null、未设置、空字符串或0(如果为空,从数据库重新计算)
+        $topicCountEmpty = !isset($data['topic_count']) || $data['topic_count'] === null || $data['topic_count'] === '' || $data['topic_count'] === 0;
+        $scoreTotalEmpty = !isset($data['score_total']) || $data['score_total'] === null || $data['score_total'] === '' || $data['score_total'] === 0;
+
+        if ($paperId > 0 && ($topicCountEmpty || $scoreTotalEmpty)) {
+            // 获取该试卷下所有有效题目的统计信息
+            $topicModel = new ExamTopicModel();
+            $statistics = $topicModel
+                ->where('paper_id', $paperId)
+                ->where('mark', 1)
+                ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
+                ->first();
+
+            if ($statistics) {
+                // 如果试题数量为空,使用数据库中的值
+                if ($topicCountEmpty) {
+                    $data['topic_count'] = intval($statistics->topic_count ?? 0);
+                }
+                // 如果总分为空,使用数据库中的值
+                if ($scoreTotalEmpty) {
+                    $data['score_total'] = intval($statistics->score_total ?? 0);
+                }
+            }
+        }
+
+        // 调用父类的 edit 方法
+        $error = '';
+        $rowId = $this->model->edit($data, $error, $is_sql);
+        if ($rowId) {
+            return message(MESSAGE_OK, true, ['id' => $rowId]);
+        }
+        return message($error, false);
+    }
+
+    /**
      * 删除七天之前标记软删除的数据
      */
     public function delete()