StudenttargetController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. class StudenttargetController extends Controller
  3. {
  4. //学生目标院校导入
  5. public function actionImport()
  6. {
  7. $returnUrl = $this->createUrl('studentinfo/target');
  8. ini_set ('memory_limit', '300M');
  9. if(!$_FILES){
  10. Yii::app()->jump->error('文件大小超过范围', $returnUrl);
  11. }else {
  12. if (!isset($_FILES['student_file']) || !isset($_FILES['student_file']['size']) || $_FILES['student_file']['size'] > 5242880) {
  13. Yii::app()->jump->error('文件大小超过范围', $returnUrl);
  14. }
  15. }
  16. if (Yii::app()->request->getIsPostRequest()) {
  17. $uploader = new Uploader("upload/tmpDir/UploadStudentTarget/{$this->schoolId}/");
  18. $uploader->allowTypes = array("xls","xlsx");
  19. $uploader->fieldsMappings = array("exname" => array(0 => $this->schoolId));
  20. $file = $uploader->act();
  21. $file = Arr::current(Arr::current($file));
  22. if (!$file || !isset($file["error"]))
  23. Yii::app()->jump->error('请上传的Excel!', $returnUrl);
  24. if ($file["error"] != 0) {
  25. switch ($file["error"]) {
  26. case 2001:
  27. Yii::app()->jump->error('文件类型不符', $returnUrl);
  28. break;
  29. case 2002:
  30. Yii::app()->jump->error('文件大小超出允许范围', $returnUrl);
  31. break;
  32. default:
  33. Yii::app()->jump->error('上传失败', $returnUrl);
  34. break;
  35. }
  36. }
  37. $inputFileName = $file["src"];
  38. // 导入PHPExcel类
  39. Yii::import('application.extensions.*');
  40. require_once('phpexcel/PHPExcel/IOFactory.php');
  41. try{
  42. $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
  43. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
  44. @unlink($file["src"]);
  45. }catch (Exception $e){
  46. @unlink($file["src"]);
  47. Yii::app()->jump->error('Excel格式不正确:'.$e->getMessage(), $returnUrl);
  48. }
  49. if(!$sheetData)
  50. {
  51. Yii::app()->jump->error('请上传正确的Excel!', $returnUrl);
  52. }
  53. unset($objPHPExcel);
  54. unset($sheetData[1]);
  55. if(!$sheetData)
  56. {
  57. Yii::app()->jump->error('请输入学生目标院校相关数据', $returnUrl);
  58. }
  59. //当前学校所在省份
  60. $province_id = $this->conn->createCommand("SELECT province_id FROM school WHERE school_id = '{$this->schoolId}';")->queryScalar();
  61. $insertValue="insert into student_target_university(`student_id`,`student_name`,`student_card`,`class_name`,`grade_name`,`school_id`,`target_university`,`target_score`,`status`,`create_time`) values ";
  62. $values=array();
  63. $insertStudentIds = array();
  64. $studentCard=array();
  65. foreach ($sheetData as $key=>$val){
  66. if(count($val)< 5){
  67. Yii::app()->jump->error('导入Excel格式不匹配! ', $returnUrl);
  68. }
  69. if($val['D']){
  70. $studentCard[$val['D']][]=$val['D'];
  71. }
  72. if(empty($val['D'])){
  73. unset($sheetData[$key]);
  74. }
  75. //$values[]="('".$val['B']."','".$val['A']."','".$sex."','".json_encode($extendKeyValue)."','".$id_number."','".$val['D']."')";
  76. }
  77. $errorMsg = array();
  78. foreach ($sheetData as $key=>$item){
  79. //重复准考证号的行不存
  80. if(isset($studentCard[$item['D']]) && count($studentCard[$item['D']])>1){
  81. $errorMsg[] = implode(',',$item);continue;
  82. }
  83. //查询准考证号是否存在
  84. $student = $this->conn->createCommand("SELECT student_id FROM student WHERE student_card = '{$item['D']}' limit 1;")->queryRow();
  85. if(!$student){
  86. $errorMsg[] = implode(',',$item);continue;
  87. }
  88. //查询目标院校的 本学校所在省分最低录取分数 没查到不存
  89. $targetScoreSql = <<<SQL
  90. select apu.admission_score
  91. from admission_province_university apu
  92. join admission_province ap on ap.ap_id=apu.ap_id and ap.region_id={$province_id} and ap.is_use=1
  93. join admission_university au on au.au_id=apu.au_id and au.name='{$item['E']}'
  94. SQL;
  95. $targetScore = $this->conn->createCommand($targetScoreSql)->queryScalar();
  96. if(!$targetScore){
  97. $errorMsg[] = implode(',',$item);continue;
  98. }
  99. $values[] = sprintf("('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",$student['student_id'],$item['C'],$item['D'],$item['B'],$item['A'],$this->schoolId,$item['E'],$targetScore,1,time());
  100. $insertStudentIds[] = $student['student_id'];
  101. }
  102. // if($errorMsg){
  103. // if(count($errorMsg)>5){
  104. // Yii::app()->jump->error('请检查重新上传问题数据:<br>'.implode(' ',array_slice($errorMsg,0,5)).' ...... ', $returnUrl);
  105. // }else{
  106. // Yii::app()->jump->error('请检查重新上传问题数据:<br>'.implode(' ',$errorMsg), $returnUrl);
  107. // }
  108. // }
  109. if($values){
  110. $sql=$insertValue.implode(',',$values);
  111. $transaction = $this->conn->beginTransaction();
  112. try{
  113. if($insertStudentIds) {
  114. $studentIds = implode(',', $insertStudentIds);
  115. $this->conn->createCommand("update student_target_university set status=0 where student_id in ({$studentIds})")->execute();
  116. }
  117. $this->conn->createCommand($sql)->execute();
  118. $transaction->commit();
  119. // Yii::app()->jump->error('Excel导入成功!');
  120. //跳转导入页面
  121. if($errorMsg){
  122. $this->redirect($this->createUrl('studenttarget/error?error='.implode('_',$errorMsg)));
  123. }
  124. $this->redirect($this->createUrl('studentinfo/target'));
  125. }catch(Exception $e){
  126. $transaction->rollBack();
  127. //var_dump($e->getMessage());exit;
  128. Yii::app()->jump->error('Excel导入失败!', $returnUrl);
  129. }
  130. }else{
  131. if($errorMsg){
  132. $this->redirect($this->createUrl('studenttarget/error?error='.implode('_',$errorMsg)));
  133. }
  134. Yii::app()->jump->error('Excel导入失败!', $returnUrl);
  135. }
  136. }
  137. }
  138. //导入的错误数据页面
  139. public function actionError()
  140. {
  141. $errorData = Req::get('error') ? Req::get('error') : '';
  142. $dataList = !empty($errorData)?explode('_',$errorData):array();
  143. return $this->render('target_error', array(
  144. 'dataList' => $dataList,
  145. ));
  146. }
  147. }