EnglishReadingCommand.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. Yii::import('application.models.*');
  3. Yii::import('application.components.*');
  4. /**
  5. * 英语外刊宝脚本同步
  6. */
  7. class EnglishReadingCommand extends CConsoleCommand
  8. {
  9. public function init()
  10. {
  11. parent::init();
  12. @ini_set('memory_limit', '1024M');
  13. set_time_limit(0);
  14. }
  15. /***
  16. * 脚本可选参数 学校id:schoolId
  17. *
  18. *
  19. * 英语外刊宝脚本同步/usr/local/php5.3/bin/php /home/www/vhosts/zxhx.testing/zsyas2/protected/shell/yiic.php englishreading index schoolId=3980 --YII_ENV=testing
  20. *
  21. *
  22. */
  23. public function actionIndex($YII_ENV = 'development')
  24. {
  25. echo 'start 英语外刊宝同步脚本.....' . PHP_EOL;
  26. foreach ($_SERVER['argv'] as $k => $v) {
  27. if (strpos($v, 'schoolId=') !== FALSE) {
  28. $schoolId = substr($v, strlen('schoolId='));
  29. }
  30. }
  31. $schools = isset($schoolId) ? array(0=>array('school_id'=> $schoolId)) : $this->getSchools();
  32. foreach ($schools as $school) {
  33. echo 'start school_id:' . $school['school_id'] . '...' . PHP_EOL;
  34. $con = $this->getSchoolDbCon($school['school_id']);
  35. if (!$con) {
  36. echo 'school database cannot connect' . PHP_EOL;
  37. continue;
  38. }
  39. try {
  40. self::syncTpl($con);
  41. echo 'school_id:'.$school['school_id'] . 'done' . PHP_EOL;
  42. } catch (\Exception $e) {
  43. echo $e->getMessage() . PHP_EOL;
  44. continue;
  45. }
  46. sleep(0.1);
  47. }
  48. echo 'end 处理完成' . PHP_EOL;
  49. exit;
  50. }
  51. /**
  52. * 获取所有正常可用的学校
  53. * @return mixed
  54. */
  55. private function getSchools()
  56. {
  57. $db = Yii::app()->businessDb;
  58. $sql = "SELECT school_id,school_name FROM `school` WHERE `status`=0 ";
  59. $schools = $db->createCommand($sql)->queryAll();
  60. $db->close();
  61. return $schools;
  62. }
  63. /**
  64. * 学校库连接
  65. * @param $schoolId
  66. * @return bool|CDbConnection
  67. */
  68. public function getSchoolDbCon($schoolId)
  69. {
  70. $db = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $schoolId));
  71. if (empty($db)) {
  72. return false;
  73. }
  74. $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
  75. $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
  76. $my_connection->emulatePrepare = true;
  77. $my_connection->enableProfiling = true;
  78. $my_connection->enableParamLogging = true;
  79. $myDbDsn = null;
  80. return $my_connection;
  81. }
  82. /**
  83. * 同步模板数据
  84. * @param $con object 数据库连接
  85. */
  86. private function syncTpl($con)
  87. {
  88. //找到当前的学期
  89. $semesterSql='select * from semester where status=1 order by start_time desc';
  90. $semester = $con->createCommand($semesterSql)->queryRow();
  91. if(!$semester){
  92. echo '没有可用的学期信息'.PHP_EOL;
  93. return;
  94. }
  95. //当前学期的考试
  96. $examsSql="select * from (
  97. SELECT spr.class_id,spr.exam_id,e.create_time,e.subject_id FROM student_paper_relation as spr LEFT JOIN `exam` as e ON spr.exam_id = e.exam_id
  98. WHERE spr.semester_id='{$semester['semester_id']}' AND e.exam_group_id > 0 AND spr.is_feedback=1 AND spr.is_complete=1 AND spr.is_del = 0 AND e.status = 1 AND e.subject_id=8
  99. ORDER BY e.create_time DESC
  100. ) t group by t.class_id";
  101. $paperStudentInfo=array();
  102. $exam = $con->createCommand($examsSql)->queryAll();
  103. if($exam) {
  104. $examIds = array();
  105. foreach ($exam as $item) {
  106. $examIds[] = $item['exam_id'];
  107. }
  108. $examIds = implode(',', $examIds);
  109. //考试成绩
  110. $sql = "select spr.student_id,spr.scoring,p.score,spr.class_id,p.exam_id,p.paper_id from paper p
  111. inner join student_paper_relation spr on p.paper_id=spr.paper_id
  112. where p.exam_id in ({$examIds}) and spr.is_feedback=1 and spr.is_complete=1 order by spr.class_id asc ";
  113. $paperStudentInfo = $con->createCommand($sql)->queryAll();
  114. }
  115. if(!$paperStudentInfo){
  116. //学期数据
  117. $sql="SELECT semester_id,semester_name,`status`,refer_code,start_time,end_time FROM `semester` ORDER BY start_time DESC";
  118. $semesters=$con->createCommand($sql)->queryAll();
  119. $isLatelySemester=false;
  120. $latelySemester=array();//当前学期的上学期
  121. foreach($semesters as $item){
  122. if($isLatelySemester){
  123. $latelySemester=$item;
  124. }
  125. if(!$isLatelySemester && $item['status']){
  126. $isLatelySemester=true;
  127. continue;
  128. }
  129. }
  130. if($latelySemester){
  131. //上学期考试
  132. $examsSql="select * from (
  133. SELECT spr.class_id,spr.exam_id,e.create_time,e.subject_id FROM student_paper_relation_{$latelySemester['refer_code']} as spr LEFT JOIN `exam` as e ON spr.exam_id = e.exam_id
  134. WHERE spr.semester_id='{$latelySemester['semester_id']}' AND e.exam_group_id > 0 AND spr.is_feedback=1 AND spr.is_complete=1 AND spr.is_del = 0 AND e.status = 1 AND e.subject_id=8
  135. ORDER BY e.create_time DESC
  136. ) t group by t.class_id";
  137. $exam = $con->createCommand($examsSql)->queryAll();
  138. if(!$exam){
  139. echo '没有可用的考试信息!!'.PHP_EOL;
  140. return;
  141. }
  142. $examIds=array();
  143. foreach($exam as $item){
  144. $examIds[]=$item['exam_id'];
  145. }
  146. $examIds=implode(',',$examIds);
  147. //考试成绩
  148. $sql="select spr.student_id,spr.scoring,p.score,spr.class_id,p.exam_id,p.paper_id from paper p
  149. inner join student_paper_relation_{$latelySemester['refer_code']} spr on p.paper_id=spr.paper_id
  150. where p.exam_id in ({$examIds}) and spr.is_feedback=1 and spr.is_complete=1 order by spr.class_id asc";
  151. $paperStudentInfo=$con->createCommand($sql)->queryAll();
  152. }
  153. }
  154. $studentLevel=array();
  155. foreach($paperStudentInfo as $item){
  156. if(!$item['score'] || intval($item['score'])<intval($item['scoring'])){
  157. continue;
  158. }
  159. $rate=round(intval($item['scoring'])/intval($item['score']),4)*100;
  160. $level=2;
  161. if($rate<=60){
  162. $level=1;
  163. }elseif($rate>60 && $rate<=80){
  164. $level=2;
  165. }elseif($rate>80){
  166. $level=3;
  167. }
  168. $studentLevel[$level][]=$item['student_id'];
  169. }
  170. if($studentLevel){
  171. foreach($studentLevel as $key=>$studentsIds){
  172. $studentsIdsStr=implode(',',$studentsIds);
  173. $sql = "update student_info set reading_level={$key} where student_id in ({$studentsIdsStr})";
  174. echo $sql.PHP_EOL;
  175. $con->createCommand($sql)->execute();
  176. }
  177. }
  178. echo 'end'.PHP_EOL;
  179. }
  180. }