Ver código fonte

add - 批量导入

liugc 1 ano atrás
pai
commit
609146766c

+ 12 - 2
app/adminapi/controller/training/TrainingCourseController.php

@@ -30,7 +30,7 @@ use app\adminapi\validate\training\TrainingCourseValidate;
 class TrainingCourseController extends BaseAdminController
 {
 
-
+    public array $notNeedLogin = ['classBatchCourses'];
     /**
      * @notes 获取列表
      * @return \think\response\Json
@@ -104,5 +104,15 @@ class TrainingCourseController extends BaseAdminController
         return $this->data($result);
     }
 
-
+    /**
+     * @notes 批量分类课程
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2025/02/14 17:37
+     */
+    public function classBatchCourses()
+    {
+        $result = TrainingCourseLogic::classBatchCourses();
+        return $this->success('', [$result], 1, 1);
+    }
 }

+ 17 - 0
app/adminapi/controller/training/TrainingQuestionsController.php

@@ -104,5 +104,22 @@ class TrainingQuestionsController extends BaseAdminController
         return $this->data($result);
     }
 
+    /**
+     *  试题导入
+     */
+    public function questionImport()
+    {
+        $params = $this->request->post();
+        $result = TrainingQuestionsLogic::questionImport($params);
+        if (true === $result) {
+            return $this->success('导入成功', [], 1, 1);
+        }
+        return $this->fail(TrainingQuestionsLogic::getError());
+    }
+
+
+
+
+
 
 }

+ 54 - 0
app/adminapi/logic/training/TrainingCourseLogic.php

@@ -15,6 +15,7 @@
 namespace app\adminapi\logic\training;
 
 
+use app\adminapi\logic\goods_category\GoodsCategoryLogic;
 use app\common\model\training\TrainingCourse;
 use app\common\logic\BaseLogic;
 use think\facade\Db;
@@ -128,4 +129,57 @@ class TrainingCourseLogic extends BaseLogic
         }
         return $data??[];
     }
+
+
+
+    public static function classBatchCourses()
+    {
+
+        $dataCategory = GoodsCategoryLogic::getTreeData();
+        $randomly_question = 1;
+        $randomly_num = 4;
+        $course_question_score = 70;
+
+        Db::startTrans();
+        try {
+
+            foreach ($dataCategory as $item) {
+                if($item['id'] == 18) {
+                    $course_name = '清洗服务全流程';
+                    $picture_url = 'https://cdnweixiu.kyjlkj.com/uploads/images/20250309/20250309152641ddf4a7638.png';
+                    $mp4_url = 'https://alipreview.zhongdunanpei.com/00474565fa6371ef88d77511979f0102/a0927b14355944c2a1ab876309a7c0e8-08f7fd77e0d39a658cc82fa71dd50586-ld.mp4';
+                }
+                if($item['id'] == 6) {
+                    $course_name = '维修服务全流程';
+                    $picture_url = 'https://cdnweixiu.kyjlkj.com/uploads/images/20250309/20250309152650239af2955.png';
+                    $mp4_url = 'https://alipreview.zhongdunanpei.com/a0cd4665fa6371ef8be00360d1d70102/889fc4a4b97645a4ae8ef0f368f7f08d-dcd5c6e3fbfcb59cd2a71c10984a321d-ld.mp4';
+                }
+                if(in_array($item['id'], [18,6])){
+                    foreach ($item['children'] as $children) {
+                        foreach ($children['children'] as $thr_children) {
+                            TrainingCourse::create([
+                                'course_name' => $course_name,
+                                'course_picture' => $picture_url,
+                                'course_url' => $mp4_url,
+                                'course_lock' => 2,
+                                'course_length' => 0,
+                                'course_question_setting' => [],
+                                'course_question_score' => $course_question_score,
+                                'subclass' => $thr_children['id']??0,
+                                'randomly_question' => $randomly_question,
+                                'randomly_num' => $randomly_num,
+                            ]);
+                        }
+                    }
+                }
+            }
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            return $e->getMessage();
+        }
+    }
+
+
 }

+ 101 - 0
app/adminapi/logic/training/TrainingQuestionsLogic.php

@@ -134,5 +134,106 @@ class TrainingQuestionsLogic extends BaseLogic
         return $data??[];
     }
 
+    public static function questionImport($questions)
+    {
+        $newQuestionArray = [];
+        $time = time();
+        foreach ($questions as $key => $question) {
+            $newQuestionArray[$key]['title'] = $question['title'];
+            $newQuestionArray[$key]['question_analysis'] = '暂无';
+            $newQuestionArray[$key]['subclass'] = self::getSubclass($question['subclass']??'');
+            $newQuestionArray[$key]['create_time'] = $time;
+            $newQuestionArray[$key]['update_time'] = $time;
+
+            $typeAnswer = self::getQuestionTypeAnswer($question['question_answer']);
+            $newQuestionArray[$key]['question_type'] = $typeAnswer['question_type']??0;
+            $newQuestionArray[$key]['question_answer'] = $typeAnswer['question_answer']??'';
+            $newQuestionArray[$key]['question_selects'] = self::getQuestionSelects($question,$typeAnswer['question_answer'])??'';
+
+        }
+
+        Db::startTrans();
+        try {
+
+            TrainingQuestions::insertAll($newQuestionArray);
+
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    public static function getSubclass($subclass)
+    {
+        if(empty($subclass)) return 0;
+        if(is_numeric($subclass)) return $subclass;
+        return \app\common\model\goods_category\GoodsCategory::where('name', $subclass)->value('id')??0;
+    }
+
+    public static function getQuestionTypeAnswer($answer)
+    {
+        // 统一转大写
+        $answer = strtoupper($answer);
+        $optionMap = [
+            'A' => 1,
+            'B' => 2,
+            'C' => 3,
+            'D' => 4,
+            'E' => 5,
+            'F' => 6
+        ];
+        $cleanedAnswer = preg_replace('/[^A-Z]/', '', $answer);
+        if (strlen($cleanedAnswer) === 0) {
+            $question_type = 0;
+        } elseif (strlen($cleanedAnswer) === 1) {
+            $question_type = 1;
+        } else {
+            $question_type = 2;
+        }
+        $newArray = [];
+        for ($i = 0; $i < strlen($cleanedAnswer); $i++) {
+            $letter = $cleanedAnswer[$i];
+            if (isset($optionMap[$letter])) {
+                $newArray[] = $optionMap[$letter];
+            }
+        }
+        $question_answer = implode(',', $newArray);
+        return ['question_type'=>$question_type,'question_answer'=>$question_answer];
+    }
+    public static function getQuestionSelects($options_data,$question_answer)
+    {
+        $keyArr = [
+                "options_a" => "0",
+                "options_b" => "1",
+                "options_c" => "2",
+                "options_d" => "3",
+                "options_e" => "4",
+                "options_f" => "5"
+        ];
+        $res = [];
+        foreach ($options_data as $key => $item) {
+            isset($keyArr[$key]) && $res[$keyArr[$key]] = $item;
+        }
+
+        $arr = array_values($res);
+        $answerArr = explode(',', $question_answer);
+        $data = [];
+        foreach ($arr as $key => $name) {
+            $data[$key]['block_key'] = $key+1;
+            $data[$key]['name'] = trim($name);
+            if(in_array($data[$key]['block_key'],$answerArr)){
+                $data[$key]['select_value'] = \true;
+            }else{
+                $data[$key]['select_value'] = \false;
+            }
+        }
+        return json_encode($data);
+    }
+
+
 
 }

+ 2 - 2
app/adminapi/validate/training/TrainingCourseValidate.php

@@ -68,7 +68,7 @@ class TrainingCourseValidate extends BaseValidate
      */
     public function sceneAdd()
     {
-        return $this->only(['course_name','course_picture','course_url','course_lock','course_question_setting','course_question_score']);
+        return $this->only(['course_name','course_picture','course_url','course_lock','course_question_score']);
     }
 
 
@@ -80,7 +80,7 @@ class TrainingCourseValidate extends BaseValidate
      */
     public function sceneEdit()
     {
-        return $this->only(['id','course_name','course_picture','course_url','course_lock','course_question_setting','course_question_score']);
+        return $this->only(['id','course_name','course_picture','course_url','course_lock','course_question_score']);
     }