1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| public static function batchFreshmanStudentUpload($data){ date_default_timezone_set('PRC'); $log = LogBatchModel::beforeBatch($data); $res['success'] = false; if($log['success']===false) { $res['logBatch']=$log; return $res; } $LBID=$log['data']['LBID']; $filePath=$data['FilePath']; try{ $StudentArray = self::readFreshmanFromExcel($filePath); }catch (Exception $e) { $res['filePath']=$filePath; return $res; } $key=['SID','StudentName','Grade','Education','Major','IDCard','Passwd','Tutor'];
$transaction=Yii::$app->db->beginTransaction('SERIALIZABLE'); try { for ($i = 0; $i < count($StudentArray); $i++) { $row = $StudentArray[$i][0]; $student=[]; for($j=0;$j<count($key);$j++) { $student[$key[$j]]=$row[$j]; } if($student['SID']===null)throw new \Exception('学号不能为空!'); $student['SID']=strval($student['SID']); if($student['StudentName']===null)throw new \Exception('姓名不能为空!'); $EducationLevel=EducationLevelModel::findOne(['LevelName' => $student['Education']]); if($EducationLevel===null)throw new \Exception('层次不能为空!'); $EducationLevelID = $EducationLevel['ELID']; $Major=MajorModel::findOne(['EducationLevelID'=>$EducationLevelID,'MajorName'=>$student['Major']]); if($Major===null)throw new \Exception('专业不能为空!'); $MID = $Major['MID']; $majorEnrollYear=new MajorEnrollyearModel(); $majorEnrollYear->Grade=$student['Grade']; if($majorEnrollYear===null)throw new \Exception('年级不能为空!'); $majorEnrollYear->MajorID=$MID; $majorEnrollYear->save(); $MajorEnrollyear=MajorEnrollyearModel::findOne(['MajorID'=>$MID,'Grade'=>$student['Grade']]); if($MajorEnrollyear===null)throw new \Exception('学号 '.$student['SID'].' 的年级、专业、层次对应关系错误!'); $student['MajorEnrollYearID']=$MajorEnrollyear['MEYID'];
$Tutor=TeachersModel::findOne(['TeacherName'=>$student['Tutor']]); if($Tutor===null) $student['TutorID']=null; else $student['TutorID']=intval($Tutor['TID']);
if($student['Passwd']===null)$student['Passwd']='111111'; $student['Passwd']=hash_hmac('SHA256', 'cc-frontend-vue',$student['Passwd']); $user = new User(); $user->username = $student['StudentName']; $user->setPassword($student['Passwd']); $user->generateAuthKey(); if (!$user->save()){ $res['errors'] = $user->errors; throw new \Exception("user 保存失败!"); }
$stu = new StudentModel(); $stu->id = $user->id; $stu->MajorEnrollYearID =$student['MajorEnrollYearID']; $stu->SID = $student['SID']; $stu->StudentName = $student['StudentName']; $stu->TutorID=$student['TutorID']; $stu->IDCard=$student['IDCard']; if (!$stu->save()) { $res['errors'] = $stu->errors; throw new \Exception("student 保存失败!"); }
$assignment = new Assignment($user->id); $res['authRes']=$assignment->assign(['学生','游客']); if($res['authRes']!==2) { throw new \Exception("auth 保存失败!"); } } $transaction->commit(); for ($i = 0; $i < count($StudentArray); $i++) $res['data'][] = $StudentArray[$i][0][0]; }catch (\Exception $exception) { $transaction->rollBack(); LogBatchModel::afterBatch(false,$LBID); $res['data'] = $exception->getMessage(); return $res; } LogBatchModel::afterBatch(true,$LBID); $res['success'] = true; return $res; }
|