|
|
@@ -0,0 +1,262 @@
|
|
|
+<?php
|
|
|
+namespace app\workerapi\logic;
|
|
|
+use app\adminapi\logic\master_worker_register\MasterWorkerRegisterLogic;
|
|
|
+use app\common\enum\worker\WorkerAccountLogEnum;
|
|
|
+use app\common\enum\YesNoEnum;
|
|
|
+use app\common\logic\BaseLogic;
|
|
|
+use app\common\model\bank_account\BankAccount;
|
|
|
+use app\common\model\master_worker\MasterWorker;
|
|
|
+use app\common\model\master_worker\MasterWorkerAccountLog;
|
|
|
+use app\common\model\master_worker\MasterWorkerAgree;
|
|
|
+use app\common\model\master_worker\MasterWorkerInfo;
|
|
|
+use app\common\model\master_worker\MasterWorkerTeam;
|
|
|
+use app\common\model\shops\ShopOrderGoods;
|
|
|
+use app\common\model\shops\ShopOrders;
|
|
|
+use app\common\model\training\TrainingCourse;
|
|
|
+use app\common\model\training\TrainingQuestions;
|
|
|
+use app\common\model\training\TrainingWorkerCourse;
|
|
|
+use app\common\model\training\TrainingWorkerQuestion;
|
|
|
+use app\common\model\training\TrainingWorkerTask;
|
|
|
+use app\common\model\works\ServiceWork;
|
|
|
+use app\common\service\FileService;
|
|
|
+use app\workerapi\lists\MasterWorkerLists;
|
|
|
+use app\workerapi\lists\ServiceWorkLists;
|
|
|
+use think\Exception;
|
|
|
+use think\facade\Config;
|
|
|
+use think\facade\Db;
|
|
|
+use think\facade\Log;
|
|
|
+
|
|
|
+
|
|
|
+class TrainingLogic extends BaseLogic
|
|
|
+{
|
|
|
+ public static function getDetail(int $masterWorkerId)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ TrainingWorkerTaskLogic::upTaskOrder($masterWorkerId);
|
|
|
+
|
|
|
+ $task_info = ['id'=>0];
|
|
|
+ $task = TrainingWorkerTask::where('master_worker_id',$masterWorkerId)->findOrEmpty();
|
|
|
+ if (!$task->isEmpty()) {
|
|
|
+ $task_info = $task->toArray();
|
|
|
+ // 0 不展示按钮 1 去学习 2 已学完
|
|
|
+ $task_info['learning_status'] = $task_info['shop_orders_id']>0?($task_info['training_status'] <2?1:2):0;
|
|
|
+ $task_info['bank_status'] = BankAccount::where('worker_id',$masterWorkerId)->where('audit_state',1)->value('id')?1:0;
|
|
|
+ $masterWorker = MasterWorker::where('id',$masterWorkerId)->find();
|
|
|
+ $task_info['category_status'] = empty($masterWorker['category_ids'])?0:1;
|
|
|
+ $task_info['service_status'] = (empty($masterWorker['lon']) || empty($masterWorker['lat']))?0:1;
|
|
|
+ }
|
|
|
+ $task_info['free_videos'] = TrainingCourse::where('course_lock',1)->select()->toArray();
|
|
|
+ return $task_info;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ self::setError($e->getMessage());
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取工程师的课程列表
|
|
|
+ * @param int $masterWorkerId
|
|
|
+ * @param array $params
|
|
|
+ * @return array id => 工程师的课程id
|
|
|
+ */
|
|
|
+ public static function getCourseList(int $masterWorkerId,$params = [])
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $list = TrainingWorkerCourse::with(['trainingCourse'])->where('master_worker_id',$masterWorkerId)->where('training_task_id',$params['training_task_id'])->select()->toArray();
|
|
|
+ foreach ($list as &$item) {
|
|
|
+ $item['course_name'] = $item['trainingCourse']['course_name'];
|
|
|
+ $item['course_picture'] = FileService::getFileUrl($item['trainingCourse']['course_picture']);
|
|
|
+ $item['course_url'] = FileService::getFileUrl($item['trainingCourse']['course_url']);
|
|
|
+ }
|
|
|
+ return $list;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ self::setError($e->getMessage());
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改学习记录状态 播放开始 播放暂停 播放结束 考试开始 考试结束
|
|
|
+ * @param array $params
|
|
|
+ * @param int $masterWorkerId
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public static function upWorkerCourse(array $params)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $logCourse = TrainingWorkerCourse::where('id',$params['id'])->findOrEmpty();
|
|
|
+ if ($logCourse->isEmpty()) {
|
|
|
+ throw new \Exception('课程不存在');
|
|
|
+ }
|
|
|
+ // 播放开始 播放暂停 播放结束
|
|
|
+ switch ($params['status']) {
|
|
|
+ case 1:
|
|
|
+ $logCourse->play_time = 1;
|
|
|
+ $logCourse->study_status = 1;
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ $logCourse->play_time = $params['play_time']??10;
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ $logCourse->study_status = 2;
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ $logCourse->exam_start_time = time();
|
|
|
+ $logCourse->study_status = 3;
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ $logCourse->exam_end_time = time();
|
|
|
+ $logCourse->study_status = 4;
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ $logCourse->exam_end_time = time();
|
|
|
+ $logCourse->study_status = 5;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ $logCourse->save();
|
|
|
+ return true;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ self::setError($e->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取工程师的考试试题列表(即开始考试)
|
|
|
+ * @param array $params worker_course_id 工程师的课程id
|
|
|
+ * @return array id 工程师的试题id
|
|
|
+ */
|
|
|
+ public static function getQuestionList(array $params)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $workerCourse = TrainingWorkerCourse::where('id',$params['worker_course_id'])->findOrEmpty();
|
|
|
+ $study_status = 0;
|
|
|
+ if(isset($params['is_resit']) && $params['is_resit'] == 1 && $workerCourse['study_status'] == 4){
|
|
|
+ // 重置
|
|
|
+ TrainingWorkerCourse::update(['exam_start_time'=>0,'exam_end_time'=>0,'study_status'=>2,'exam_score'=>0],['id'=>$params['worker_course_id']]);
|
|
|
+ TrainingWorkerQuestion::update(['worker_answer'=>'','exam_score'=>0],['worker_course_id'=>$params['worker_course_id']]);
|
|
|
+ $study_status = 2;
|
|
|
+ }
|
|
|
+ $list = [];
|
|
|
+ if($workerCourse['study_status'] == 2 || $study_status == 2) {
|
|
|
+ // 开始考试
|
|
|
+ self::upWorkerCourse(['id'=>$params['worker_course_id'],'status'=> 4]);
|
|
|
+ $list = TrainingWorkerQuestion::with(['trainingQuestions'])->where('worker_course_id',$params['worker_course_id'])->select()->toArray();
|
|
|
+ foreach ($list as &$item) {
|
|
|
+ $item['title'] = $item['trainingQuestions']['title'];
|
|
|
+ $item['question_type'] = $item['trainingQuestions']['question_type'];
|
|
|
+ $item['question_selects'] = $item['trainingQuestions']['question_selects'];
|
|
|
+ $item['question_answer'] = $item['trainingQuestions']['question_answer'];
|
|
|
+ $item['question_analysis'] = $item['trainingQuestions']['question_analysis'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $list;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new \Exception($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 提交考试 该工程师的课程id + 所有的工程师的试题id、答案
|
|
|
+ * @param array
|
|
|
+ * @param int $masterWorkerId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static function submitExam($params){
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 课程试卷详情
|
|
|
+ $exam_paper_details = self::getExamPaperDetails(['worker_course_id'=>$params['worker_course_id']]);
|
|
|
+
|
|
|
+ // 提交的答案 [['id'=> ,'answer'=>],['id'=> ,'answer'=>]]
|
|
|
+ !is_array($params['worker_answer']) && $params['worker_answer'] = json_decode($params['worker_answer'],true);
|
|
|
+ $worker_answer = array_column($params['worker_answer'],'answer','id');
|
|
|
+
|
|
|
+ //对比答案 更新答题记录实际每题得分,总得分
|
|
|
+ $all_exam_score = 0;
|
|
|
+ $course_question_setting = $exam_paper_details['course']['course_question_setting'];
|
|
|
+ foreach ($course_question_setting as &$item) {
|
|
|
+ $exam_score = 0;
|
|
|
+ ($worker_answer_value = explode(',',$worker_answer[$item['select_value']])) && sort($worker_answer_value) && ($worker_answer_value = implode(',',$worker_answer_value));
|
|
|
+ if($worker_answer_value == $item['question_answer']){
|
|
|
+ $exam_score = $item['score'];
|
|
|
+ }
|
|
|
+ $all_exam_score += $exam_score;
|
|
|
+ TrainingWorkerQuestion::update(
|
|
|
+ ['worker_answer' => $worker_answer_value,'exam_score' => $exam_score],
|
|
|
+ ['worker_course_id' => $params['worker_course_id'],'questions_id' => $item['select_value']]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新课程得分、状态
|
|
|
+ if((float)$all_exam_score < (float)$exam_paper_details['course']['course_question_score']){
|
|
|
+ $study_status = 4;
|
|
|
+ }else{
|
|
|
+ $study_status = 5;
|
|
|
+ }
|
|
|
+ TrainingWorkerCourse::update(
|
|
|
+ ['exam_score' => $all_exam_score,'study_status'=>$study_status,'exam_start_time'=>time()],
|
|
|
+ ['id' => $params['worker_course_id']]
|
|
|
+ );
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ return true;
|
|
|
+ }catch(\Exception $e){
|
|
|
+ Db::rollback();
|
|
|
+ self::setError($e->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function getExamPaperDetails($params){
|
|
|
+ // 试卷详情
|
|
|
+ $workerCourse = [];
|
|
|
+ if(isset($params['worker_course_id'])){
|
|
|
+ $workerCourse = TrainingWorkerCourse::where('id',$params['worker_course_id'])->findOrEmpty()->toArray();
|
|
|
+ // 课程详情
|
|
|
+ $course = TrainingCourse::where('id',$workerCourse['training_course_id'])->findOrEmpty()->toArray();
|
|
|
+ // 该课程所有试题 答案、分数
|
|
|
+ $question_ids = array_column($course['course_question_setting'],'select_value');
|
|
|
+ $question_column = TrainingQuestions::where('id',$question_ids)->column('question_answer','id');
|
|
|
+ foreach ($course['course_question_setting'] as &$item) {
|
|
|
+ $item['question_answer'] = $question_column[$item['select_value']];
|
|
|
+ }
|
|
|
+ $workerCourse['course'] = $course;
|
|
|
+ }
|
|
|
+ Log::info('exam_paper_details:'.json_encode($workerCourse));
|
|
|
+ return $workerCourse;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 考试结果统计
|
|
|
+ * @param int $teamId
|
|
|
+ * @param int $masterWorkerId
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function examStatistics($params){
|
|
|
+ // 计算考试结果: 总题数 正确率(>0的题数) 总得分(每题得分累计) | 总用时 是否合格
|
|
|
+ // training_worker_question training_worker_course
|
|
|
+ /*total_questions 总题数 correct_questions 正确题数 correct_rate_percentage 正确率 total_score 总得分
|
|
|
+
|
|
|
+ exam_time 总用时秒*/
|
|
|
+
|
|
|
+ $res = [];
|
|
|
+ $res['statistics'] = TrainingWorkerQuestion::where('worker_course_id',$params['worker_course_id'])
|
|
|
+ ->field(['worker_course_id',Db::raw('COUNT(id) AS total_questions,SUM(CASE WHEN exam_score > 0 THEN 1 ELSE 0 END) AS correct_questions,ROUND((SUM(CASE WHEN exam_score > 0 THEN 1 ELSE 0 END) / COUNT(id)) * 100, 2) AS correct_rate_percentage,SUM(exam_score) AS total_score')])
|
|
|
+ ->group('worker_course_id')
|
|
|
+ ->find();
|
|
|
+ $workerCourse = TrainingWorkerCourse::where('id',$params['worker_course_id'])->findOrEmpty()->toArray();
|
|
|
+ $res['statistics']['exam_time'] = $workerCourse['exam_end_time'] - $workerCourse['exam_start_time'];
|
|
|
+ $res['statistics']['study_status'] = $workerCourse['study_status'];
|
|
|
+ $res['statistics_list'] = TrainingWorkerQuestion::where('worker_course_id',$params['worker_course_id'])->select();
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|