DownloadStatusBatchCommand.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. Yii::import('application.models.*');
  3. /**
  4. * 批量更新超印发送学生打印状态(一次性)
  5. */
  6. class DownloadStatusBatchCommand extends CConsoleCommand {
  7. public function init() {
  8. parent::init();
  9. @ini_set('memory_limit', '1024M');
  10. set_time_limit(0);
  11. }
  12. public function actionIndex($YII_ENV='development')
  13. {
  14. echo 'start 批量更新超印任务学生下载状态脚本开始处理.....'.PHP_EOL;
  15. $schools = $this->getSchools();
  16. foreach ($schools as $school){
  17. echo '开始处理学校'.$school['business_school_id'].PHP_EOL;
  18. $tasks = $this->getTaskBySchoolId($school['business_school_id']);
  19. foreach ($tasks as $task){
  20. $studentIds = $this->getStudentIdsByTaskId($task['task_id']);
  21. $schoolDbCon = $this->getSchoolDbCon($school['business_school_id']);
  22. if(!$schoolDbCon){
  23. echo '学校库连接失败'.PHP_EOL;
  24. }
  25. if($studentIds) {
  26. $this->updateDownloadStatus($schoolDbCon, $task['exam_id'], $task['clazz_id'], $studentIds, $task['goods_type_id'], $task['download_time']);
  27. $schoolDbCon->close();
  28. }
  29. echo $task['task_id'].'超印任务处理完成'.PHP_EOL;
  30. }
  31. echo '学校'.$school['business_school_id'].'处理完毕'.PHP_EOL;
  32. sleep(0.5);
  33. }
  34. $this->finishCommand();
  35. echo 'end 更新超印任务学生下载状态脚本处理完成'.PHP_EOL;
  36. exit;
  37. }
  38. /**
  39. * 获取所有有云印任务的学校
  40. * @return mixed
  41. */
  42. private function getSchools()
  43. {
  44. $imsDb = Yii::app()->imsDb;
  45. $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;";
  46. $schools = $imsDb->createCommand($sql)->queryAll();
  47. $imsDb->close();
  48. return $schools;
  49. }
  50. /**
  51. * 获取学校下所有超印任务
  52. * @param $schoolId
  53. * @return mixed
  54. */
  55. private function getTaskBySchoolId($schoolId)
  56. {
  57. $imsDb = Yii::app()->imsDb;
  58. $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;";
  59. $tasks = $imsDb->createCommand($sql)->queryAll();
  60. $imsDb->close();
  61. return $tasks;
  62. }
  63. /**
  64. * 获取任务所有关联学生
  65. * @param $taskId
  66. * @return mixed
  67. */
  68. private function getStudentIdsByTaskId($taskId)
  69. {
  70. $imsDb = Yii::app()->imsDb;
  71. $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}';";
  72. $students = $imsDb->createCommand($sql)->queryAll();
  73. $imsDb->close();
  74. $studentIds = array();
  75. foreach ($students as $student){
  76. $studentIds[] = $student['student_id'];
  77. }
  78. return $studentIds;
  79. }
  80. /**
  81. * 更新班级考试打印及学生下载状态
  82. * @param $con //数据库连接
  83. * @param $examId //考试id
  84. * @param $classId //班级id
  85. * @param $studentIds //学生id数组
  86. * @param $type //商品类型
  87. * @param $time //下载时间
  88. */
  89. private function updateDownloadStatus($con, $examId, $classId, $studentIds, $type, $time)
  90. {
  91. $time = strtotime($time);
  92. $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();
  93. $stuIds = trim(implode(',',$studentIds),',');
  94. $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();
  95. $tableName = 'student_paper_relation_'.$referCode;
  96. $isExist = $con->createCommand("select * from information_schema.TABLES where TABLE_SCHEMA=(select database()) and `table_name` ='{$tableName}';")->queryAll();
  97. if(!$isExist){
  98. $tableName = 'student_paper_relation';
  99. }
  100. switch ($type){
  101. case 1:
  102. $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();
  103. break;
  104. case 2:
  105. $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();
  106. break;
  107. case 3:
  108. $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();
  109. break;
  110. default:
  111. echo '商品类型异常'.PHP_EOL;
  112. break;
  113. }
  114. }
  115. /**
  116. * 学校库连接
  117. * @param $schoolId
  118. * @return bool|CDbConnection
  119. */
  120. public function getSchoolDbCon($schoolId){
  121. $db = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>$schoolId));
  122. if(empty($db)){
  123. return false;
  124. }
  125. $myDbDsn = 'mysql:host='.$db->database_host.';dbname='.$db->database_name;
  126. $my_connection = new CDbConnection($myDbDsn,$db->database_user,$db->database_password);
  127. $my_connection->emulatePrepare = true;
  128. $my_connection->enableProfiling = true;
  129. $my_connection->enableParamLogging = true;
  130. $myDbDsn = null;
  131. return $my_connection;
  132. }
  133. public function finishCommand()
  134. {
  135. $connection = BusinessCloudPrintTask::model()->getDbConnection();
  136. $time = time();
  137. $connection->createCommand("INSERT INTO cloud_print_task_pack (pack_status,create_time) VALUES (3,{$time});")->execute();
  138. }
  139. }