getSchools(); foreach ($schools as $school){ echo '开始处理学校'.$school['business_school_id'].PHP_EOL; $tasks = $this->getTaskBySchoolId($school['business_school_id']); foreach ($tasks as $task){ $studentIds = $this->getStudentIdsByTaskId($task['task_id']); $schoolDbCon = $this->getSchoolDbCon($school['business_school_id']); if(!$schoolDbCon){ echo '学校库连接失败'.PHP_EOL; } if($studentIds) { $this->updateDownloadStatus($schoolDbCon, $task['exam_id'], $task['clazz_id'], $studentIds, $task['goods_type_id'], $task['download_time']); $schoolDbCon->close(); } echo $task['task_id'].'超印任务处理完成'.PHP_EOL; } echo '学校'.$school['business_school_id'].'处理完毕'.PHP_EOL; sleep(0.5); } $this->finishCommand(); echo 'end 更新超印任务学生下载状态脚本处理完成'.PHP_EOL; exit; } /** * 获取所有有云印任务的学校 * @return mixed */ private function getSchools() { $imsDb = Yii::app()->imsDb; $sql = "select DISTINCT business_school_id from cloud_print_task where status in (1,2,3) and exam_id<>0 GROUP BY business_school_id;"; $schools = $imsDb->createCommand($sql)->queryAll(); $imsDb->close(); return $schools; } /** * 获取学校下所有超印任务 * @param $schoolId * @return mixed */ private function getTaskBySchoolId($schoolId) { $imsDb = Yii::app()->imsDb; $sql = "select task_id,clazz_id,exam_id,task_name,goods_type_id,download_time from cloud_print_task WHERE business_school_id = '{$schoolId}' and status in (1,2,3) and exam_id<>0;"; $tasks = $imsDb->createCommand($sql)->queryAll(); $imsDb->close(); return $tasks; } /** * 获取任务所有关联学生 * @param $taskId * @return mixed */ private function getStudentIdsByTaskId($taskId) { $imsDb = Yii::app()->imsDb; $sql = "SELECT d.student_id FROM cloud_print_task t JOIN cloud_print_task_detail d ON t.task_id=d.task_id WHERE t.task_id='{$taskId}';"; $students = $imsDb->createCommand($sql)->queryAll(); $imsDb->close(); $studentIds = array(); foreach ($students as $student){ $studentIds[] = $student['student_id']; } return $studentIds; } /** * 更新班级考试打印及学生下载状态 * @param $con //数据库连接 * @param $examId //考试id * @param $classId //班级id * @param $studentIds //学生id数组 * @param $type //商品类型 * @param $time //下载时间 */ private function updateDownloadStatus($con, $examId, $classId, $studentIds, $type, $time) { $time = strtotime($time); $con->createCommand("UPDATE class_exam_printer SET is_print=1,print_time={$time} WHERE class_id='{$classId}' AND exam_id='{$examId}' AND type={$type} AND is_print=0;")->execute(); $stuIds = trim(implode(',',$studentIds),','); $referCode = $con->createCommand("SELECT s.refer_code FROM exam e JOIN semester s ON e.semester_id=s.semester_id WHERE e.exam_id = '{$examId}';")->queryScalar(); $tableName = 'student_paper_relation_'.$referCode; $isExist = $con->createCommand("select * from information_schema.TABLES where TABLE_SCHEMA=(select database()) and `table_name` ='{$tableName}';")->queryAll(); if(!$isExist){ $tableName = 'student_paper_relation'; } switch ($type){ case 1: $con->createCommand("UPDATE {$tableName} SET is_wrongbook_download=1,download_wrongbook_time={$time} WHERE class_id='{$classId}' AND exam_id='{$examId}' AND student_id IN ({$stuIds}) AND is_wrongbook_download=0;")->execute(); break; case 2: $con->createCommand("UPDATE {$tableName} SET is_two_isp_download=1,download_two_isp_time={$time} WHERE class_id='{$classId}' AND exam_id='{$examId}' AND student_id IN ({$stuIds}) AND is_two_isp_download=0;")->execute(); break; case 3: $con->createCommand("UPDATE {$tableName} SET is_three_isp_download=1,download_three_isp_time={$time} WHERE class_id='{$classId}' AND exam_id='{$examId}' AND student_id IN ({$stuIds}) AND is_three_isp_download=0;")->execute(); break; default: echo '商品类型异常'.PHP_EOL; break; } } /** * 学校库连接 * @param $schoolId * @return bool|CDbConnection */ public function getSchoolDbCon($schoolId){ $db = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>$schoolId)); if(empty($db)){ return false; } $myDbDsn = 'mysql:host='.$db->database_host.';dbname='.$db->database_name; $my_connection = new CDbConnection($myDbDsn,$db->database_user,$db->database_password); $my_connection->emulatePrepare = true; $my_connection->enableProfiling = true; $my_connection->enableParamLogging = true; $myDbDsn = null; return $my_connection; } public function finishCommand() { $connection = BusinessCloudPrintTask::model()->getDbConnection(); $time = time(); $connection->createCommand("INSERT INTO cloud_print_task_pack (pack_status,create_time) VALUES (3,{$time});")->execute(); } }