redis = new Redis(); // $server = Yii::app()->params["redis"]["server"]; // $this->redis->connect($server["host"], $server["port"]); // $this->redis->select(2); $this->delExpire(); } /** * 重置错题本 * @throws CDbException * @throws CException */ public function actionWbhtml(){ list($status,$msg) = $this->resetExam(1); echo $status; exit(0); } /** * 重置二步个性化学习宝 * @throws CDbException * @throws CException */ public function actionIsp2html(){ list($status,$msg) = $this->resetExam(2); echo $status; exit(0); } /** * 重置三步个性化学习宝 * @throws CDbException * @throws CException */ public function actionIsp3html(){ list($status,$msg) = $this->resetExam(3); echo $status; exit(0); } /************* 重置多个班级 *************/ /** * 重置多个班级错题本 * @throws CDbException * @throws CException */ public function actionMulWbhtml(){ $classExam = Yii::app()->request->getParam('classExam'); foreach($classExam as $value){ list($status,$msg) = $this->resetExam(1,$value); if (!$status) { break; } } echo $status; } /** * 重置多个班级二步个性化学习宝 * @throws CDbException * @throws CException */ public function actionMulIsp2html(){ $classExam = Yii::app()->request->getParam('classExam'); foreach($classExam as $value){ list($status,$msg) = $this->resetExam(2,$value); if (!$status) { break; } } echo $status; } /** * 重置多个班级三步个性化学习宝 * @throws CDbException * @throws CException */ public function actionMulIsp3html(){ $classExam = Yii::app()->request->getParam('classExam'); foreach($classExam as $value){ list($status,$msg) = $this->resetExam(3,$value); if (!$status) { break; } } echo $status; } /** * 新考试类型产品重置(初高中方法宝等) */ public function actionMulExamProducthtml(){ $productType = Req::get('productType'); $classExam = Yii::app()->request->getParam('classExam'); foreach($classExam as $value){ list($status,$msg) = $this->resetExam($productType,$value,true); if (!$status) { break; } } echo $status; } /** * 重置产品方法 * @param $proType 产品类型(对应ims商品类目) * @param array $examClassIdArr array(0=>classId,1=>examId) * @param boolean $isNewExamProduct * @return array * @throws CDbException * @throws CException */ private function resetExam($proType,$examClassIdArr = array(), $isNewExamProduct=false) { $error = array(); if ($examClassIdArr && count($examClassIdArr) == 2) { $classId = $examClassIdArr[0]; $examId = $examClassIdArr[1]; }else{ $examId = (string)Yii::app()->request->getParam('examId'); $classId = (string) Yii::app()->request->getParam('classId'); } if(! $examId){ $error[] = '请传入examId参数'; } if(! $classId ){ $error[] = '请传入classId参数'; } // if(!in_array($proType,array(1,2,3))){ // $error[] = '产品类型不正确'; // } $feildArr = array(); if (!$error) { //FIXME 新考试类型产品 if($isNewExamProduct){ $feildArr['exam_product_status'] = array( 'is_create_html' => 90, 'is_create_pdf' => 90, 'is_download' => 0, 'pdf_page_num' => 0, 'pdf_sheet_num' => 0, ); $feildArr['exam'] = array( 'is_common_topic' => 90, ); }else{ $feildArr['student_paper_relation_property'] = array( 'is_radar_img' => 90, 'is_studytrend_img' => 90, 'is_enc_lang' => 90, 'is_studytrend' => 90, 'is_kps_analy' => 90, 'is_scoring_detail' => 90, 'is_tips' => 90, ); if ($proType == 1) { $feildArr['student_paper_relation'] = array( 'is_wrongbook_html' => 90, 'is_wrongbook_pdf' => 0, 'is_studytrend_img' => 90, 'is_wrong_plan_created' => 0, 'wb_page_num' => 0, 'is_wrongbook_download' => 0, ); $feildArr['student_paper_relation_property']['is_wb'] = 90; $feildArr['exam'] = array( 'wb_group_id' => 0, 'is_common_topic' => 90, ); }elseif ($proType == 2) { $feildArr['student_paper_relation'] = array( 'is_isp_html' => 90, 'is_isp_pdf' => 0, 'is_plan_created' => 0, 'is_top_plan_created' => 0, 'is_studytrend_img' => 90, 'is_wrong_plan_created' => 0, 'isp_page_num' =>0, 'is_two_isp_download' => 0, ); $feildArr['student_paper_relation_property']['is_isp'] = 90; $feildArr['exam'] = array( 'is_preparation_created' => 90, 'preparation_plan_id' => 0, 'isp_group_id' => 0, 'is_common_topic' => 90, ); }elseif ($proType == 3) { $feildArr['student_paper_relation'] = array( 'is_wbisp_html' => 90, 'is_wbisp_pdf' => 0, 'is_plan_created' => 0, 'is_top_plan_created' => 0, 'is_studytrend_img' => 90, 'is_wrong_plan_created' => 0, 'wbisp_page_num' =>0, 'is_three_isp_download' => 0, ); $feildArr['student_paper_relation_property']['is_wbisp'] = 90; $feildArr['exam'] = array( 'is_preparation_created' => 90, 'preparation_plan_id' => 0, 'wbisp_group_id' => 0, 'is_common_topic' => 90, ); } } //查询重置日志 $examGroup=$this->sConn->createCommand("select exam_group_id,subject_id from exam where exam_id='{$examId}'")->queryRow(); $resetLog=$this->sConn->createCommand("select reset_count,learn_tube_reset_count from product_reset_log where exam_id='{$examId}' and product_type='".$proType."'")->queryRow(); $logSql=''; if(!$resetLog){ $logSql="insert into product_reset_log(`exam_group_id`,`exam_id`,`class_id`,`subject_id`,`product_type`,`semester_id`,`learn_tube_reset_count`,`learn_tube_reset_time`)"; if(!isset(Yii::app()->session['testFlag']) || Yii::app()->session['testFlag']!=1 ) { $logSql .= " values('" . $examGroup['exam_group_id'] . "','" . $examId . "','" . $classId . "','" . $this->subjectId . "','" . $proType . "','" . $this->semesterId . "',1," . time() . ")"; }else{ $logSql .= " values('" . $examGroup['exam_group_id'] . "','" . $examId . "','" . $classId . "','" . $this->subjectId . "','" . $proType . "','" . $this->semesterId . "',0," . 0 . ")"; } }else{ $count=intval($resetLog['reset_count'])+1; if(!isset(Yii::app()->session['testFlag']) || Yii::app()->session['testFlag']!=1 ){ $larnTubeResetCount=intval($resetLog['learn_tube_reset_count'])+1; //学管最多重置3次 //$larnTubeResetCount=$larnTubeResetCount>3?3:$larnTubeResetCount; $learnTubeResetTime=time(); $logSql="update product_reset_log set reset_count=".$count." ,learn_tube_reset_count={$larnTubeResetCount},learn_tube_reset_time={$learnTubeResetTime} where exam_id='{$examId}' and product_type='".$proType."'"; }else{ $logSql="update product_reset_log set reset_count=".$count." where exam_id='{$examId}' and product_type='".$proType."'"; } } $dbConn = SStudentPaperRelation::model()->getDbConnection(); $transaction = $dbConn->beginTransaction(); try { //重置后清除已存在的下载任务 $task_sql = "delete from pack_product_task where unique_key ={$examId} and school_id ={$this->schoolId} and class_id={$classId} and product_type={$proType}"; $this->conn->createCommand($task_sql)->execute(); $sqlArr = $this->getUpdateSql($feildArr, $examId, $classId, $proType); foreach ($sqlArr as $tableName => $sql) { $bool = $dbConn->createCommand($sql)->execute(); if ($bool === false) { throw new Exception("表{$tableName}更新失败"); } } $bool = $this->saveResetExam($dbConn,$examId,$proType); if ($bool === false) { throw new Exception("表数据失败"); } //更新产品重置日志表 if($logSql){ $this->sConn->createCommand($logSql)->execute(); } $transaction->commit(); }catch(Exception $ex){ $transaction->rollback(); $error[] = $ex->getMessage(); } } if ($error) { return array(0, implode(PHP_EOL, $error)); }else{ //发送消息 $sendMsg=array( 'school_id'=>$this->schoolId, 'msg_type'=>90, 'subject_id'=>$examGroup['subject_id'], 'send_type'=>'exam', 'product_type'=>array($proType), 'exam_group_id'=>$examGroup['exam_group_id'], 'exam_ids'=>array($examId) ); sendDataToKafka('xbkc-php-product-html',$sendMsg); return array(1, ''); } } /** * 生成sql语句 * @param $feildArr * @param $examId * @param $classId * @param $productType * @return array */ private function getUpdateSql($feildArr,$examId,$classId,$productType) { $sqlArr = array(); $where = " where class_id={$classId} and exam_id={$examId} "; $whereExam = " where exam_id={$examId} "; foreach ($feildArr as $tableName => $_feilds) { $set_feild_arr = array(); foreach ($_feilds as $k => $v) { $set_feild_arr[] = "{$k} = {$v}"; } $set_feild_str = implode(',', $set_feild_arr); if($tableName == 'exam' || $tableName == 'exam_product_status'){ $sql = "update {$tableName} set {$set_feild_str} {$whereExam}"; $sqlArr[$tableName] = $tableName == 'exam_product_status' ? $sql. " and product_type={$productType}" : $sql; }else{ $sqlArr[$tableName] = "update {$tableName} set {$set_feild_str} {$where}"; } } return $sqlArr; } /** * 重置考试和学生保存到表里 * @param $dbConn * @param $examId * @param $proType * @return bool */ private function saveResetExam(&$dbConn,$examId,$proType){ $sql = 'select student_id,is_feedback,is_complete,is_del from student_paper_relation where exam_id = :exam_id'; $studentPaper = SStudentPaperRelation::model()->findAllBySql($sql,array(':exam_id'=>$examId)); $exam_obj = new SExam(); $subjectId = $exam_obj->getExamSubjectId($examId); unset($exam_obj); $resetData = array(); if($studentPaper){ foreach ($studentPaper as $v){ if(isset(Yii::app()->session['session_duoxueke_subject_id']) && Yii::app()->session['session_duoxueke_subject_id'] == 8){ if($v->is_feedback == 1 && $v->is_complete == 1){ if(!isset($resetData[$examId])){ $resetData[$examId] = array(); } $resetData[$examId][] = array( 'exam_id'=>$examId, 'subject_id'=>$subjectId, 'student_id'=>$v->student_id, 'product_type'=>$proType, 'status'=>90, 'create_time'=>time(), ); } }else{ if(!($v->is_feedback == 0 && $v->is_complete == 0 && $v->is_del == 0)){ if(!isset($resetData[$examId])){ $resetData[$examId] = array(); } $resetData[$examId][] = array( 'exam_id'=>$examId, 'subject_id'=>$subjectId, 'student_id'=>$v->student_id, 'product_type'=>$proType, 'status'=>90, 'create_time'=>time(), ); } } } } unset($studentPaper); $bool = false; if($resetData){ foreach ($resetData as $examId => $value){ $sql = 'insert into can_generate_product (exam_id,subject_id,student_id,product_type,status,create_time) values '; foreach ($value as $k => $v){ $sql .= ($k ? ',':'')."({$examId},{$v['subject_id']},{$v['student_id']},{$v['product_type']},{$v['status']},{$v['create_time']})"; } $del_sql = 'delete from can_generate_product where exam_id = ' . $examId . ' and product_type = ' . $proType; $dbConn->createCommand($del_sql)->execute(); $bool = $dbConn->createCommand($sql)->execute(); } } return $bool; } /** * 删除过期重置记录 */ private function delExpire() { //删除七天前的 $time = time() - 7 * 24 * 3600; $del_sql = 'delete from can_generate_product where create_time < ' . $time; $dbConn = SCanGenerateProduct::model()->getDbConnection(); $dbConn->createCommand($del_sql)->execute(); } }