CorrectDataHandleCommand.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. class CorrectDataHandleCommand extends CConsoleCommand {
  3. public $conn;
  4. public $sConn;
  5. public function init() {
  6. parent::init();
  7. @ini_set('memory_limit', '1024M');
  8. set_time_limit(0);
  9. }
  10. public $authUsername = '';
  11. public function actionIndex($YII_ENV='production'){
  12. $needHandleData=$this->getNeedHandleData();
  13. if (!$needHandleData) {
  14. $this->showMsg(1, '没有需要处理的考试');
  15. return '';
  16. }
  17. $schoolIds = array_keys($needHandleData);
  18. $this->showMsg(1, '共有' . count($schoolIds) . '个学校需要处理 .....');
  19. //查询学校数据库连接地址
  20. $schoolDatabases = $this->getSchoolDatabases($schoolIds);
  21. foreach ($needHandleData as $schoolId=>$datum){
  22. $this->showMsg(1, '当前学校ID:' . $schoolId . ' .......');
  23. $this->databaseInfo = isset($schoolDatabases[$schoolId]) ? $schoolDatabases[$schoolId] : array();
  24. if (!$this->databaseInfo) {
  25. $this->showMsg(1, '学校ID:' . $schoolId . '没有数据库连接信息');
  26. continue;
  27. }
  28. //连接学校数据库
  29. $this->schConnObj = null;
  30. $this->schConnObj = $this->getSchConnObj();
  31. if (!$this->schConnObj) {
  32. $this->showMsg(1, '学校ID:' . $schoolId . '数据库连接失败!!!!!');
  33. continue;
  34. }
  35. MyActiveRecord::$schoolId = $schoolId;
  36. foreach ($datum as $examClass){
  37. $examGroupIds=explode(",", $examClass["exam_group_ids"]);
  38. $classIds = explode(",", $examClass["class_ids"]);
  39. if(!$examGroupIds || !$classIds){
  40. $this->showMsg(1, '学校ID:' . $schoolId . '考试数据,班级数据设置异常');
  41. continue;
  42. }
  43. $noExamClass=array(); //未参加任何考试的班级
  44. $haveExamClass=array(); //参加考试的班级
  45. $examRelationStudent=$this->getAllStudentByExamId($examGroupIds);
  46. foreach ($classIds as $classId){
  47. $classJoinExam=array();
  48. //读取班级学生id
  49. $classStudent=$this->getStudentIdsByClassId($classId);
  50. //查询学生参与的考试id
  51. foreach ($examRelationStudent as $examId => $studentIds){
  52. if(array_intersect($classStudent,$studentIds)){
  53. $classJoinExam[]=$examId;
  54. }
  55. }
  56. if(!$classJoinExam){
  57. $noExamClass[]=$classId;
  58. }else{
  59. $haveExamClass[(string)$classId]=$classJoinExam;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * 需要处理的数据
  67. * @return array|CDbDataReader
  68. */
  69. protected function getNeedHandleData()
  70. {
  71. $schoolRelExam = array();
  72. $time = time();
  73. //获取要生成教师讲案的数据
  74. $sql = "select id,school_id,gp_group_id,class_ids,exam_group_ids from xb_data_handle where status = 1 and error_msg = '' ";
  75. $result = $this->getBusConn()->createCommand($sql)->queryAll();
  76. if ($result) {
  77. foreach ($result as $value) {
  78. $schoolId = $value['school_id'];
  79. if (!isset($schoolRelExam[$schoolId])) {
  80. $schoolRelExam[$schoolId] = array();
  81. }
  82. $schoolRelExam[$schoolId][] = array(
  83. 'class_ids' => $value['class_ids'],
  84. 'gp_group_id' => $value['gp_group_id'],
  85. 'exam_group_id' => $value['exam_group_id'],
  86. 'id' => $value['id'],
  87. );
  88. }
  89. }
  90. return $schoolRelExam;
  91. }
  92. /**
  93. * 业务数据库对象
  94. * @return CDbConnection|null
  95. */
  96. protected function getBusConn()
  97. {
  98. $time = time();
  99. if ($this->busConnObj) {
  100. if ($time - $this->busDbTime > $this->dbTimeout) {
  101. echo '关闭超时业务数据库重新连接' . PHP_EOL;
  102. }else{
  103. return $this->busConnObj;
  104. }
  105. }
  106. $dbParams = Yii::app()->params["default_server"];
  107. $busConnObj = $this->getDbConnection($dbParams['addr'], Yii::app()->params["default_db"]['name'], $dbParams['username'], $dbParams['password']);
  108. $this->busConnObj = $busConnObj;
  109. $this->busDbTime = $time;
  110. return $busConnObj;
  111. }
  112. /**
  113. * 显示信息
  114. * @param $status
  115. * @param $msg
  116. */
  117. protected function showMsg($status,$msg)
  118. {
  119. echo $msg . PHP_EOL;
  120. }
  121. /**
  122. * 获取学校数据库
  123. * @param $schoolIds
  124. * @return array
  125. */
  126. protected function getSchoolDatabases($schoolIds)
  127. {
  128. $schoolDatabases = array();
  129. $sql = "select school_id,database_host,database_user,database_password,database_name,group_id from `database` where school_id in (" . implode(',', $schoolIds) . ")";
  130. $rs = $this->getBusConn()->createCommand($sql)->queryAll();
  131. if ($rs) {
  132. foreach ($rs as $value) {
  133. $schoolDatabases[$value['school_id']] = $value;
  134. }
  135. }
  136. return $schoolDatabases;
  137. }
  138. /**
  139. * 学校数据库对象
  140. * @return mixed
  141. */
  142. public function getSchConnObj()
  143. {
  144. $time = time();
  145. if ($this->schConnObj) {
  146. if ($time - $this->schDbTime > $this->dbTimeout) {
  147. echo '关闭超时学校数据库重新连接' . PHP_EOL;
  148. }else{
  149. return $this->schConnObj;
  150. }
  151. }
  152. $databaseInfo = $this->databaseInfo;
  153. $schConnObj = $this->getDbConnection($databaseInfo['database_host'], $databaseInfo['database_name'], $databaseInfo['database_user'], $databaseInfo['database_password']);
  154. $this->schConnObj = $schConnObj;
  155. $this->schDbTime = $time;
  156. return $schConnObj;
  157. }
  158. public function getDbConnection($databaseHost,$databaseName,$databaseUser,$databasePassword)
  159. {
  160. if($databaseHost && $databaseName && $databaseUser && $databasePassword){
  161. $myDbDsn = 'mysql:host=' . $databaseHost . ';dbname=' . $databaseName;
  162. $myConnection = new CDbConnection($myDbDsn, $databaseUser, $databasePassword);
  163. $myConnection->emulatePrepare = true;
  164. $myConnection->enableProfiling = true;
  165. $myConnection->enableParamLogging = true;
  166. $myDbDsn = null;
  167. return $myConnection;
  168. }else{
  169. return null;
  170. }
  171. }
  172. //读取考试id
  173. private function getExamIds($examGroupId){
  174. $sql="select exam_id from exam where exam_group_id='{$examGroupId}'";
  175. $result = $this->getSchConnObj()->createCommand($sql)->queryAll();
  176. $examIds=array();
  177. if($result){
  178. foreach ($result as $item){
  179. $examIds[]=$item['exam_id'];
  180. }
  181. }
  182. return $examIds;
  183. }
  184. //读取班级学生
  185. private function getStudentIdsByClassId($classId){
  186. $sql="select student_id from student_class_relation where class_id='{$classId}' and status=0";
  187. $result = $this->getSchConnObj()->createCommand($sql)->queryAll();
  188. $studentIds=array();
  189. if($result){
  190. foreach ($result as $item){
  191. $studentIds[]=$item['student_id'];
  192. }
  193. }
  194. return $studentIds;
  195. }
  196. //查询所有参加学生
  197. private function getAllStudentByExamId($examGroupIds){
  198. $studentRelationExamId=array();
  199. foreach ($examGroupIds as $examGroupId){
  200. $examIds=$this->getExamIds($examGroupId);
  201. $sql="select student_id,exam_id from student_paper_relation where exam_id in(".implode(',',$examIds).") and is_del=0 and is_feedback=1";
  202. $result = $this->getSchConnObj()->createCommand($sql)->queryAll();
  203. if($result){
  204. foreach ($result as $item){
  205. $studentRelationExamId[(string)$item['exam_id']][]=$item['student_id'];
  206. }
  207. }
  208. }
  209. return $studentRelationExamId;
  210. }
  211. }