'学号',
'school_student_card'=>'学校准考证号',
'zhixue_student_card'=>'智学网准考证号',
'id_number'=>'身份证号',
'student_phone'=>'手机号',
'is_outer'=>'在籍生'
);
public function actionIndex(){
$this->sConn->createCommand("DELETE FROM `import_student_temp` where import_type=2 ")->execute();
$student_all_data = $this->sConn->createCommand("SELECT id FROM `import_student_temp` limit 1 ")->queryRow();
if($student_all_data){
$this->render('check');
}else{
$this->render('index');
}
}
public function actionTemplate(){
$fileName=array(
'userno'=>'学号',
'school_student_card'=>'学校准考证号',
'zhixue_student_card'=>'智学网准考证号',
'id_number'=>'身份证号',
'student_phone'=>'手机号',
'is_outer'=>'在籍生'
);
//扩展字段
$studentExtend=$this->schoolManager->getStudentExtend();
if($studentExtend){
foreach ($studentExtend as $key=> $value){
if($value['is_enable']==1){
$fileName[$key]=$value['field_mean'];
}
}
}
$data['fileName']=$fileName;
$template=$this->sConn->createCommand("select tpl_data from import_student_template limit 1")->queryRow();
if($template && isset($template['tpl_data'])){
$data['tpl_data']=json_decode($template['tpl_data'],true);
}
$this->render('template',$data);
}
public function actionSave_template(){
$tpl_data=Req::post('data');
$result['status']=0;
if(!$tpl_data) exit(json_encode($result));
$table=$tpl_data[0];
$tpl_data=array_slice($table,5);
foreach ($tpl_data as $key=>$val){
if($val=='-1'){
unset($tpl_data[$key]);
}
}
$template=$this->sConn->createCommand("select id from import_student_template limit 1")->queryRow();
if($template){
$this->sConn->createCommand("update import_student_template set tpl_data='".json_encode($tpl_data)."'")->execute();
}else{
$this->sConn->createCommand("insert into import_student_template set tpl_data='".json_encode($tpl_data)."'")->execute();
}
$result['status']=1;
exit(json_encode($result));
}
public function actionImport(){
ini_set ('memory_limit', '300M');
if(!$_FILES){
Yii::app()->jump->error('文件大小超过范围');
}else {
if (!isset($_FILES['student_file']) || !isset($_FILES['student_file']['size']) || $_FILES['student_file']['size'] > 5242880) {
Yii::app()->jump->error('文件大小超过范围');
}
}
if (Yii::app()->request->getIsPostRequest()) {
//读取模板
$template=$this->schoolManager->getImportStudentTemplate();
if(!$template){
Yii::app()->jump->error('请先设置导入学生模板');
}
@$tpl_data=json_decode($template,true);
if(!$tpl_data){
//Yii::app()->jump->error('模板数据异常,请重新设置模板');
}
$uploader = new Uploader("upload/tmpDir/UploadClassesStudentsList/{$this->schoolId}/");
$uploader->allowTypes = array("xls","xlsx");
$uploader->fieldsMappings = array("exname" => array(0 => $this->schoolId));
$file = $uploader->act();
$file = Arr::current(Arr::current($file));
if (!$file || !isset($file["error"]))
Yii::app()->jump->error('请上传的Excel!');
if ($file["error"] != 0) {
switch ($file["error"]) {
case 2001:
Yii::app()->jump->error('文件类型不符');
break;
case 2002:
Yii::app()->jump->error('文件大小超出允许范围');
break;
default:
Yii::app()->jump->error('上传失败');
break;
}
}
$inputFileName = $file["src"];
if(Yii::app()->params['handle_log_on_off'])
{
writeFileLog(jsonEncode(array(
"exam_group_id" => 0,
"operate_project" => 'zsyas2',
"school_id" => $this->schoolId,
"title" => '导入学生',
"operate_account" => Yii::app()->session['coachInfo']['coach_name'],
"operate_method" => $this->action,
"operate_url" => $this->getRoute(),
"operate_sql" =>'',
"operate_param" =>json_encode(array('post'=>$_POST,'get'=>$_GET,'file'=>$inputFileName)),
"date"=>date('Y-m-d H:i:j')
)));
}
// 导入PHPExcel类
Yii::import('application.extensions.*');
require_once('phpexcel/PHPExcel/IOFactory.php');
try{
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
//FIXME 2019-12-23
@unlink($file["src"]);
}catch (Exception $e){
//FIXME 2019-12-23
@unlink($file["src"]);
Yii::app()->jump->error('Excel格式不正确:'.$e->getMessage());
}
if(!$sheetData)
{
Yii::app()->jump->error('请正确的Excel!');
}
unset($objPHPExcel);
//扩展字段
$studentExtend=$this->schoolManager->getStudentExtend();
if($studentExtend){
foreach ($studentExtend as $key=> $value){
if($value['is_enable']==1){
$this->fileName[$key]=$value['field_mean'];
}
}
}
//检查模板
$tempHeader=array(
'班级','姓名','性别(男/女)','系统准考证号'
);
foreach ($tpl_data as $datum){
$tempHeader[]=$this->fileName[$datum];
}
$sheetHead=$sheetData[1];
if(count($sheetHead)!=count($tempHeader)){
Yii::app()->jump->error('导入学生Excel格式与模板不匹配! ');
}
$sheetHead=array_values($sheetHead);
foreach ($tempHeader as $key => $value){
if(!isset($sheetHead[$key]) || trim($sheetHead[$key])!=$value){
Yii::app()->jump->error('导入学生Excel格式与模板不匹配:'.$value);
}
}
unset($sheetData[1]);
if(!$sheetData)
{
Yii::app()->jump->error('请输入学生数据');
}
//获取当前学期的所有班级
$semesterId = $this->semesterId;
$class_semester_data = ClassModel::model()->findAll('semester_id=:semester_id',array(':semester_id'=>$semesterId));
if($class_semester_data)
{
foreach($class_semester_data as $v)
{
$class_ids[$v->class_id] = $v->class_id;
$c_class_names[$v->class_id] = (string)$v->class_name;
}
}
unset($class_semester_data);
if(!$class_ids)
{
Yii::app()->jump->error('当前学期无班级信息,导入失败');
}
$insertValue="insert into import_student_temp(`student_name`,`class_name`,`sex`,`extend`,`id_number`,`student_card`) values ";
$values=array();
$studentCard=array();
$schoolStudentCard=array();
foreach ($sheetData as $val){
if(!$val['A'] || !$val['B'] || !$val['C']){
//Yii::app()->jump->error('班级,姓名,性别不能为空');
continue;
}
if(count($val)!=count($tpl_data)+4){
Yii::app()->jump->error('导入学生Excel格式与模板不匹配! ');
}
if($val['D']){
$studentCard[$val['D']][]=$val['D'];
}
//扩展信息基本校验
$extendKeyValue=array();
if($tpl_data){
$extend=array_slice($val,4);
$preg_card='/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i';
$preg_phone='/^1[34578]\d{9}$/ims';
$extend=array_values($extend);
foreach ($extend as $key=>$v){
if(!$v){
$extendKeyValue[$tpl_data[$key]]='';
continue;
}
switch ($tpl_data[$key]){
case 'userno':
if(!is_numeric($v)){
Yii::app()->jump->error('学号只能是数字! ');
}
break;
case 'school_student_card':
if(!is_numeric($v)){
Yii::app()->jump->error('学校准考证号格式错误! ');
}
$schoolStudentCard[$v][]=$v;
break;
case 'id_number':
if(!preg_match($preg_card,$v)){
Yii::app()->jump->error('身份证格式不正确! ');
}
break;
case 'student_phone':
if(!preg_match($preg_phone,$v)){
Yii::app()->jump->error('手机号格式不正确! ');
}
break;
case 'zhixue_student_card':
if(!is_numeric($v)){
Yii::app()->jump->error('智学网准考证号格式错误! ');
}
break;
case 'is_outer':
$isOuter=0;
if(!isset($v) || !$v || $v=='是') {
$v = '是';
$isOuter = 0;
}elseif($v=='否'){
$isOuter = 1;
}elseif(!in_array($v,array('是','否'))){
Yii::app()->jump->error('在籍生填写错误! ');
}
$v=$isOuter;
break;
}
$extendKeyValue[$tpl_data[$key]]=$v;
}
}
$sex=1;
if($val['C']=='女'){
$sex=2;
}
$id_number=0;
if(isset($extendKeyValue['id_number'])){
$id_number=$extendKeyValue['id_number'];
}
if(!$val['D']){
$val['D']=0;
}
$values[]="('".$val['B']."','".$val['A']."','".$sex."','".jsonEncode($extendKeyValue)."','".$id_number."','".$val['D']."')";
}
//检测系统准考证号是否重复
$errorStudentCard=array();
if($studentCard){
foreach ($studentCard as $item){
if(count($item)>1){
$errorStudentCard[]=$item[0];
}
}
}
if($errorStudentCard){
if(count($errorStudentCard)>3){
Yii::app()->jump->error('系统准考证号重复: '.implode(',',array_slice($errorStudentCard,0,3)).' ...... ');
}else{
Yii::app()->jump->error('系统准考证号重复: '.implode(',',$errorStudentCard));
}
}
$errorSchoolStudentCard=array();
if($schoolStudentCard){
foreach ($schoolStudentCard as $item){
if(count($item)>1){
$errorSchoolStudentCard[]=$item[0];
}
}
}
if($errorSchoolStudentCard){
if(count($errorSchoolStudentCard)>3){
Yii::app()->jump->error('学校准考证号重复: '.implode(',',array_slice($errorSchoolStudentCard,0,3)).' ...... ');
}else{
Yii::app()->jump->error('学校准考证号重复: '.implode(',',$errorSchoolStudentCard));
}
}
if($values){
$sql=$insertValue.implode(',',$values);
$transaction = $this->sConn->beginTransaction();
try{
$this->sConn->createCommand($sql)->execute();
$transaction->commit();
// Yii::app()->jump->error('Excel导入成功!');
//跳转检测页
$this->redirect($this->createUrl('importstudent/check'));
}catch(Exception $e){
$transaction->rollBack();
Yii::app()->jump->error('Excel导入失败!');
}
}else{
Yii::app()->jump->error('Excel导入失败!');
}
}
}
public function actionCheck(){
$this->render('check');
}
//检测姓名
public function actionCheck_student_name(){
ini_set('memory_limit','512M');
set_time_limit(0);
$result['status']=0;
$result['data']=0;
$id_number=array();
$clearIdNumber=array(); //需要清除身份证
$studentCard=array();
$student_all_data = $this->sConn->createCommand("SELECT id,student_name,id_number FROM `import_student_temp` `t` ")->queryAll();
if($student_all_data){
//去除非中文字符
$updateArr=array();
$preg="/[^\x{2E80}-\x{FE4F}0-9^·]+/u";
$count=0;
foreach($student_all_data as $val){
if($val['id_number']){
//判断系统中是否存在
if($this->schoolManager->checkStudentIdNumber($val['id_number'])){
$clearIdNumber[]=$val['id'];
}
$id_number[$val['id_number']][]=$val['id'];
}
if(preg_match($preg,$val['student_name'])){
$ModifyName = preg_replace($preg, "", $val['student_name']);
if($ModifyName){
$updateArr[$val['id']]=$ModifyName;
$count++;
}
}else{
$updateArr[$val['id']]=$val['student_name'];
}
}
//判断身份证是否有重复
if($id_number){
foreach ($id_number as $number=>$vid){
if(count($vid)>1){
$clearIdNumber=array_merge($clearIdNumber,$vid);
}
}
}
//组织更新语句
if($updateArr){
$arrNumber=ceil(count($updateArr)/500);
$Arr=array_chunk($updateArr,$arrNumber,true);
$transcation = $this->sConn->beginTransaction();
try {
if($clearIdNumber){
$this->sConn->createCommand("update import_student_temp set id_number=0 where id in(".implode(',',$clearIdNumber).")")->execute();
}
foreach ($Arr as $item){
$sql='update import_student_temp set `modify_name`= case id ';
foreach ($item as $key=> $val){
$sql.=" WHEN ".$key." THEN '".$val."' ";
}
$sql.=" End ";
$sql.=" where modify_name='' or modify_name is null";
$this->sConn->createCommand($sql)->execute();
}
$transcation->commit();
$result['status']=1;
}catch (Exception $e){
$transcation->rollback();
}
}
$result['count']=$count;
}
exit(json_encode($result));
}
//匹配学生信息
public function actionMatching(){
$classNotFound=array(); //班级名称不一致
$repeat=array(); //重名
$success=array(); //正常
$updateClassValue=array(); //需要更新的班级不存在数据
$updateRepeatValue=array(); //需要更新重复数据
$studentName=array();
$studentCardValue=array(); //准考证号不存在
$updateStudentCard=array();
$idNumber=array();
$updateStudentId=array(); //匹配成功需要更新student_id数据
$updateClassId=array(); //更新班级id
$result['status']=0;
$student_all_data = $this->sConn->createCommand("SELECT id,student_name,class_name,modify_name,student_id,is_repeat,class_not_found,is_normal,id_number,student_card FROM `import_student_temp` `t` ")->queryAll();
if(!$student_all_data){
$result['msg']='请先导入excel数据';
exit(json_encode($result));
}
$preSemesterId=$this->schoolManager->getPrevSemesterId();
$nowSemester=$this->semesterId;
foreach ($student_all_data as $key=>$val){
if($val['is_repeat']==1){
$repeat[$val['modify_name']][]=$val;
unset($student_all_data[$key]);
}
if($val['class_not_found']==1){
$classNotFound[$val['class_name']][]=$val;
unset($student_all_data[$key]);
}
if($val['class_not_found']==2){
$studentCardValue[]=$val['id'];
unset($student_all_data[$key]);
}
if($val['is_normal']==1){
$success[]=1;
unset($student_all_data[$key]);
}
}
if(!$student_all_data){
$result['studentCard']=count($studentCardValue);
$result['repeat']=count($repeat);
$result['success']=count($success);
$result['noExistent']=count($classNotFound);
$result['status']=1;
exit(json_encode($result));
}
//判断班级是否存在
foreach($student_all_data as $val){
$class_id=$this->schoolManager->classNameExists($val['class_name'],$nowSemester);
if(!$class_id){
$classNotFound[$val['class_name']][]=$val;
$updateClassValue[]="'".$val['class_name']."'"; //班级不存在
}else{
//判断准考证号
if($val['student_card']){
$sql="select student_id from student where student_card ='".$val['student_card']."' and status=0 and school_id='".$this->schoolId."'";
$b_student=$this->conn->createCommand($sql)->queryRow();
if($b_student){
//判断姓名
$student_card_name=$this->sConn->createCommand("select realname from student_info where student_id='".$b_student['student_id']."'")->queryRow();
if(!$student_card_name){
$studentCardValue[]=$val['id'];
$updateClassId[$val['class_name']]="update import_student_temp set class_id='".$class_id."' where class_name='".$val['class_name']."'";
}elseif($student_card_name['realname']!=$val['student_name']){
$studentCardValue[]=$val['id'];
$updateClassId[$val['class_name']]="update import_student_temp set class_id='".$class_id."' where class_name='".$val['class_name']."'";
}else{
//存在学生
$updateStudentCard[]="update import_student_temp set class_id='".$class_id."',student_id='".$b_student['student_id']."',is_normal=1 where student_card='".$val['student_card']."'";
$success[]=$val['id'];
}
}else{
$updateClassId[$val['class_name']]="update import_student_temp set class_id='".$class_id."' where class_name='".$val['class_name']."'";
$studentCardValue[]=$val['id'];
}
}else{
$updateClassId[$val['class_name']]="update import_student_temp set class_id='".$class_id."' where class_name='".$val['class_name']."'";
$studentName[$val['modify_name']][]=$val['id'];
$idNumber[$val['id']]=$val['id_number'];
}
}
}
//判断姓名是否正常
if($studentName){
foreach ($studentName as $k=>$v){
if(count($v)>1){ //当前excel中学生(去掉姓名中非汉字字符后)存在重名
$repeat[]=$v;
$updateRepeatValue=array_merge($updateRepeatValue,$v);
}else{
//读取系统中数据
$student=$this->schoolManager->getStudentByNameClass($k,$nowSemester,$idNumber[$v[0]]);
if(!$student){
//不存在,正常数据 需要创建
$success[]=$v[0];
// }elseif(count($student)==1){
//存在且唯一,正常数据 需要转班 !!!没有系统准考证号,都需要重新确认
// $success[]=$v[0];
// $updateStudentId[$v[0]]=(string)$student[0]['student_id'];
}else{
//系统中重名
$repeat[]=$v[0];
$updateRepeatValue[]=$v[0];
}
}
}
}
//更新检测成功的数据
if($updateRepeatValue || $updateStudentId || $updateClassValue || $success || $studentCardValue || $updateClassId || $updateStudentCard){
$transcation = $this->sConn->beginTransaction();
try {
if($studentCardValue){
//准考证号不存在
$this->sConn->createCommand("update import_student_temp set class_not_found=2 where id in(".implode(',',$studentCardValue).")")->execute();
}
if($updateStudentId) {
$sql = 'update import_student_temp set `student_id`= case id ';
foreach ($updateStudentId as $key => $val) {
$sql .= " WHEN " . $key . " THEN '" . $val . "' ";
}
$sql .= " End ";
$sql .= " where student_id='' or student_id is null";
$this->sConn->createCommand($sql)->execute();
}
if($updateRepeatValue){
$this->sConn->createCommand("update import_student_temp set is_repeat=1 where id in(".implode(',',$updateRepeatValue).")")->execute();
}
if($updateClassValue){
$updateClassValue=array_values($updateClassValue);
$this->sConn->createCommand("update import_student_temp set class_not_found=1 where class_name in(".implode(',',$updateClassValue).")")->execute();
}
if($updateClassId){
foreach ($updateClassId as $query){
$this->sConn->createCommand($query)->execute();
}
}
if($updateStudentCard){
foreach ($updateStudentCard as $query){
$this->sConn->createCommand($query)->execute();
}
}
if($success){
$success=array_values($success);
$this->sConn->createCommand("update import_student_temp set is_normal=1 where id in(".implode(',',$success).")")->execute();
}
$transcation->commit();
$result['status']=1;
}catch (Exception $e){
$result['status']=0;
$transcation->rollback();
}
}
$result['studentCard']=count($studentCardValue);
$result['repeat']=count($repeat);
$result['success']=count($success);
$result['noExistent']=count($classNotFound);
$result['status']=1;
exit(json_encode($result));
}
//重新上传
public function actionGiveup(){
//$delSql="truncate table import_student_temp;";
$delSql="delete from import_student_temp;";
$this->sConn->createCommand($delSql)->execute();
$result['status']=1;
exit(json_encode($result));
}
//系统准考证不存在
public function actionHandle_existent_card(){
$notFound=$this->sConn->createCommand("select id,student_name,modify_name,class_name,student_card from import_student_temp where class_not_found=2")->queryAll();
if(!$notFound) $this->redirect($this->createUrl('importstudent/check'));
$data['student']=$notFound;
$this->render('handle_existent_card',$data);
}
//系统不存在
public function actionHandle_existent(){
$notFound=$this->sConn->createCommand("select id,class_name from import_student_temp where class_not_found=1")->queryAll();
if(!$notFound) $this->redirect($this->createUrl('importstudent/check'));
$className=array();
foreach ($notFound as $val){
$className[$val['class_name']][]=$val;
}
$data['className']=$className;
$this->render('handle_existent',$data);
}
//修改班级名称
public function actionChange_value(){
$old_class_name=Req::post('data');
$value=Req::post('value');
$result['status']=0;
if(!$old_class_name || !$value){
$result['msg']='参数不正确';
exit(json_encode($result));
}
$valueArr=array();
$sql="select id,modify_name,student_name,sex,extend,student_id,student_card from import_student_temp where class_name='".$old_class_name."' ";
$data=$this->sConn->createCommand($sql)->queryAll();
if(!$data){
$result['msg']='数据异常';
exit(json_encode($result));
}
$nowSemester=$this->semesterId;
$class_id=$this->schoolManager->classNameExists($value,$nowSemester);
$updateClassId=array();
if(!$class_id){
$result['status']=0;
$result['msg']='班级在系统中不存在';
exit(json_encode($result));
}else{
foreach ($data as $key => $val){
//判断准考证号
if($val['student_card']){
$sql="select student_id from student where student_card ='".$val['student_card']."' and status=0 and school_id='".$this->schoolId."'";
$b_student=$this->conn->createCommand($sql)->queryRow();
if($b_student){
//存在学生
$valueArr[]="('".$val['student_name']."','".$value."','".$val['sex']."','".$val['modify_name']."','".$val['extend']."','".$b_student['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
unset($data[$key]);
}else{
$valueArr[]="('".$val['student_name']."','".$value."','".$val['sex']."','".$val['modify_name']."','".$val['extend']."','".$b_student['student_id']."','".$class_id."','".$val['student_card']."',0,2,0)";
unset($data[$key]);
}
}
}
if($data){
//班级存在,判断姓名
//读取系统中数据
$nameArr=array();
foreach ($data as $val){
$nameArr[$val['modify_name']][]=$val;
}
foreach ($nameArr as $Name=>$val){
if(count($val)>1){ //当前excel中学生(去掉姓名中非汉字字符后)存在重名
foreach ($val as $v){
if(!$v['student_id']){
$v['student_id']=0;
}
$valueArr[]="('".$v['student_name']."','".$value."','".$v['sex']."','".$v['modify_name']."','".$v['extend']."','".$v['student_id']."','".$class_id."','".$v['student_card']."',1,0,0)";
}
}else{
$student=$this->schoolManager->getStudentByNameClass($Name,$nowSemester);
if(!$val[0]['student_id']){
$val[0]['student_id']=0;
}
if(!$student){
//不存在,正常数据 需要创建
$valueArr[]="('".$val[0]['student_name']."','".$value."','".$val[0]['sex']."','".$val[0]['modify_name']."','".$val[0]['extend']."','".$val[0]['student_id']."','".$class_id."','".$val[0]['student_card']."',0,0,1)";
// }elseif(count($student)==1){
//存在且唯一,正常数据 需要转班
// $valueArr[]="('".$val[0]['student_name']."','".$value."','".$val[0]['sex']."','".$val[0]['modify_name']."','".$val[0]['extend']."','".$student[0]['student_id']."','".$class_id."',0,0,1)";
}else{
//系统中重名
$valueArr[]="('".$val[0]['student_name']."','".$value."','".$val[0]['sex']."','".$val[0]['modify_name']."','".$val[0]['extend']."','".$val[0]['student_id']."','".$class_id."','".$val[0]['student_card']."',1,0,0)";
}
}
}
}
$transcation = $this->sConn->beginTransaction();
try {
if($valueArr) {
$insert="insert into import_student_temp(`student_name`,`class_name`,`sex`,`modify_name`,`extend`,`student_id`,`class_id`,`student_card`,`is_repeat`,`class_not_found`,`is_normal`) values ";
$this->sConn->createCommand("delete from import_student_temp where class_name='".$old_class_name."'")->execute();
$this->sConn->createCommand($insert.implode(',',$valueArr))->execute();
}
$transcation->commit();
$result['status']=1;
}catch (Exception $e){
$result['status']=0;
$transcation->rollback();
}
exit(json_encode($result));
}
}
//修改身份证号码
public function actionChange_idnumber(){
$id=Req::post('id');
$value=Req::post('value');
$result['status']=0;
if(!$id || !$value){
$result['msg']='参数不正确';
exit(json_encode($result));
}
//判断系统中是否存在
if($this->schoolManager->checkStudentIdNumber($value)){
$result['status']=0;
$result['msg']='系统中已存在相同身份证,请检查后重新确认';
exit(json_encode($result));
}
//判断表格中是否存在
$sql="select id,modify_name from import_student_temp where id_number='".$value."' ";
$data=$this->sConn->createCommand($sql)->queryRow();
if($data){
$result['msg']='导入的数据中中已存在相同身份证,请检查后重新确认';
exit(json_encode($result));
}
if($this->sConn->createCommand("update import_student_temp set id_number='".$value."',is_normal=1,is_repeat=0 where id='".$id."'")->execute()){
$result['status']=1;
}
exit(json_encode($result));
}
//修改准考证号码
public function actionChange_studentcard(){
$id=Req::post('id');
$value=Req::post('value');
$result['status']=0;
if(!$id || !$value){
$result['msg']='参数不正确';
exit(json_encode($result));
}
$excel_data=$this->sConn->createCommand("select student_name from import_student_temp where id='".$id."'")->queryRow();
if(!$excel_data){
$result['msg']='参数不正确';
exit(json_encode($result));
}
//判断表格中是否存在
$sql="select id,student_name from import_student_temp where student_card='".$value."' and id!='".$id."' ";
$data=$this->sConn->createCommand($sql)->queryRow();
if($data){
$result['msg']='导入的数据中中已存在相同准考证'.$data['student_name'].',请检查后重新确认';
exit(json_encode($result));
}
//验证系统中是否存在
$sql="select student_id from student where student_card ='".$value."' and status=0 and school_id='".$this->schoolId."'";
$b_student=$this->conn->createCommand($sql)->queryRow();
if(!$b_student){
$result['msg']='准考证号不存在,请检查后重新确认';
exit(json_encode($result));
}else{
//判断姓名
$student_card_name=$this->sConn->createCommand("select realname from student_info where student_id='".$b_student['student_id']."'")->queryRow();
if(!$student_card_name){
$result['msg']='准考证号不存在,请检查后重新确认';
exit(json_encode($result));
}elseif($student_card_name['realname']!=$excel_data['student_name']){
$result['msg']='新的准考证号与表格姓名不匹配,请检查后重新确认';
exit(json_encode($result));
}else{
if($this->sConn->createCommand("update import_student_temp set student_card='".$value."',is_normal=1,is_repeat=0,class_not_found=0,student_id='".$b_student['student_id']."' where id='".$id."'")->execute()){
$result['status']=1;
}
}
}
exit(json_encode($result));
}
//重名学生
public function actionHandle_repeat(){
$page=Req::post('page');
$name_like=Req::post('name');
if(!$page) $page=1;
$pageSize=10;
$offset=($page-1)*$pageSize;
$sql="select count(*) as count, id,student_name,class_name,sex,modify_name,extend from import_student_temp where is_repeat=1 group by modify_name ";
if($name_like){
$sql.=" having check_field_value='".$name_like."'";
}
$sql.="order by count desc";
$all_data=$this->sConn->createCommand($sql)->queryAll();
if(!$all_data){
Yii::app()->jump->error('没有重名的数据! ');
}
$total=count($all_data);
$data['total_page']=ceil($total/$pageSize);
$data['name_group']=array_slice($all_data,$offset,$pageSize);
$data['page']=$page;
if(Yii::app()->request->isAjaxRequest){
$result['status']=1;
$result['data']=$data['name_group'];
$result['total_page']=$data['total_page'];
exit(json_encode($result));
}else{
$this->render('handle_card', $data);
}
}
//根据名称加载重名学生
public function actionShowrepeatstudent(){
$name=Req::post('name');
$fileName=array(
'userno'=>'学号',
'school_student_card'=>'学校准考证号',
'zhixue_student_card'=>'智学网准考证号',
'id_number'=>'身份证号',
'student_phone'=>'手机号',
'is_outer'=>'在籍生'
);
//扩展字段
$studentExtend=$this->schoolManager->getStudentExtend();
if($studentExtend){
foreach ($studentExtend as $key=> $value){
$fileName[$key]=$value['field_mean'];
}
}
$classIds=array();
$sql="select * from import_student_temp where modify_name='".$name."' and is_repeat=1";
$nameArr=array();
$systemStudent=array();
$numberToId=array();
$excel_data = $this->sConn->createCommand($sql)->queryAll();
if($excel_data){
$topic_score=json_decode($excel_data[0]['extend'],true);
$sex[1]='男';
$sex[2]="女";
$excel_html='
';
$sys_sql="SELECT sc.realname,sc.id_number,sc.class_id,sc.student_id,sc.is_outer,sc.id_number,c.class_name,s.semester_id,s.semester_name,s.school_year,s.end_time,sc.field_1,sc.field_2,sc.field_3,sc.field_4,sc.field_5 FROM `student_info` sc ";
$sys_sql.="LEFT JOIN class c on sc.class_id=c.class_id ";
$sys_sql.="LEFT JOIN semester s on s.semester_id=c.semester_id ";
$sys_sql.=" where `realname` = '".$name."' and s.semester_id ='".$this->semesterId."' ";
$sys_sql.="order by end_time desc ;";
$_allStudent = $this->sConn->createCommand($sys_sql)->queryAll();
$nameArr=array();
$studentData=array();
$businessData=array();
$semester=array();
$studentId=array();
$html='';
if($_allStudent){
foreach ($_allStudent as $val){
$val['serial_number']=0;
$val['userno']=0;
//班级信息
$sql_class_relation="select * from student_class_relation where student_id='".$val['student_id']."' and status=0 ";
$student_class_realtion=$this->sConn->createCommand($sql_class_relation)->queryRow();
if($student_class_realtion){
$val['serial_number']=$student_class_realtion['serial_number'];
$val['userno']=$student_class_realtion['userno'];
}else{
continue; //没有班级信息则过滤
}
//查询副号
$sql_fuhao="select * from student_relation where find_in_set('".$val['student_id']."',relation_student_id)";
$student_fuhao=$this->sConn->createCommand($sql_fuhao)->queryRow();
$val['fuhao']=0;
if($student_fuhao){
if($val['student_id']!=$student_fuhao['master_student_id']){
continue;
}else{
$fuhao_arr=explode(',',$student_fuhao['relation_student_id']);
$val['fuhao']=count($fuhao_arr)-1;
}
}
$studentData[$val['semester_id']][]=$val;
$semester[$val['semester_id']]=$val['semester_name'];
$studentId[]=$val['student_id'];
}
//查询business
$criteria = new CDbCriteria();
$b_student = array();
$criteria->addInCondition('student_id',$studentId);
$b_student_data = BusinessStudent::model()->findAll($criteria);
if($b_student_data){
foreach($b_student_data as $v)
{
$businessData[$v->student_id]=array(
'username'=>$v->username,
'student_card'=>$v->student_card,
'school_student_card'=>$v->school_student_card,
'zhixue_student_card'=>$v->zhixue_student_card,
);
}
}
$i=1;
$currSemester = $this->schoolManager->getCurrSemester();
$smid=$currSemester['semester_id'];
foreach ($semester as $key=> $val){
$semesterStatus='非当前学期';
if($smid==$key){
$semesterStatus='当前学期';
}
$html.='';
$html.='
';
$html.='
';
$html.='
';
$html.='
';
$html.='
';
if(isset($studentData[$key])){
// debug($studentData[$key]);
foreach ($studentData[$key] as $item){
$isZj='是';
if($item['is_outer']==1){
$isZj='否';
}
$html.='- ';
$html.='
';
$html.='
';
$html.='
';
$html.='';
$html.='
';
$html.='
';
$html.='
';
$html.='';
$html.=''.$item['realname'].'';
$html.='
';
$html.='
';
$html.='';
$html.=''.$item['class_name'].'';
$html.='
';
$html.='
';
$html.='
';
$html.='';
$html.=''.$val.'';
$html.='
';
$html.='
';
$html.='';
$html.=''.$semesterStatus.'';
$html.='
';
$html.='
';
$html.='';
$html.=''.$item['serial_number'].'';
$html.='
';
$html.='
';
$html.='';
$html.=''.$item['userno'].'';
$html.='
';
$html.='
';
$html.='';
if(isset($businessData[$item['student_id']])){
$html.=''.$businessData[$item['student_id']]['username'].'';
}else{
$html.='';
}
$html.='
';
$html.='
';
$html.='';
if(isset($businessData[$item['student_id']])){
$html.=''.$businessData[$item['student_id']]['student_card'].'';
}else{
$html.='';
}
$html.='
';
$html.='
';
$html.='';
if(isset($businessData[$item['student_id']])){
$html.=''.$businessData[$item['student_id']]['school_student_card'].'';
}else{
$html.='';
}
$html.='
';
$html.='
';
$html.='';
if(isset($businessData[$item['student_id']])){
$html.=''.$businessData[$item['student_id']]['zhixue_student_card'].'';
}else{
$html.='';
}
$html.='
';
$html.='
';
$html.='';
$html.=''.$item['id_number'].'';
$html.='
';
$html.='
';
$html.='';
$html.=''.$isZj.'';
$html.='
';
$html.='
';
$html.='';
$html.='查看';
$html.='
';
$html.='
';
$html.='';
$html.=''.$item['fuhao'].'';
$html.='
';
$html.='
';
$html.='
展开';
$html.='
';
$html.=' ';
$i++;
}
}
$html.='
';
$html.='
';
$html.='
';
$html.='
';
$html.='
';
}
}
}
$result['html']=$html;
$result['excel_html']=$excel_html;
$result['status']=1;
exit(json_encode($result));
}
//导入学生执行
public function actionConfirm_import(){
$result['status']=0;
$all_student=$this->sConn->createCommand("select modify_name,sex,extend,class_id,student_id,id_number,student_card from import_student_temp where is_repeat=0 and class_not_found=0 and is_normal=1")->queryAll();
if(!$all_student){
// Yii::app()->jump->error('没有可导入的数据! ');
$result['msg']='没有可导入的数据!';
exit(json_encode($result));
}
$grade = SGrade::model()->findAll(array('order'=>'id asc'));
$cardGrade=array();
$cardLengthGrade=array();
foreach ($grade as $val){
$cardGrade[$val->id]=$val->card_status;
$cardLengthGrade[$val->id]=$val->card_length;
}
$updateBusiness = array();
$updateStudentInfo = array();
$updateStudentClassRelation = array();
$insertStudentClassRelation = array();
$school=$this->schoolManager->getSchoolInfo();
$semesterId = $this->semesterId;
$class_semester_data = ClassModel::model()->findAll('semester_id=:semester_id',array(':semester_id'=>$semesterId));
$classGrade=array();
if($class_semester_data)
{
foreach($class_semester_data as $v)
{
$class_ids[$v->class_id] = $v->class_id;
$classGrade[$v->class_id] = $v->grade;
}
}
unset($class_semester_data);
if(!$class_ids)
{
//Yii::app()->jump->error('无班级信息,导入失败');
$result['msg']='无班级信息,导入失败!';
exit(json_encode($result));
}
$studentInClass=array();
$getClassByStudent=array();
$class_serial_number = array();
// 获取在线学生
$criteria = new CDbCriteria();
$criteria->addInCondition('class_id',$class_ids);
$criteria->addCondition('status=0');
$criteria->addCondition('class_type=1');
$semester_student_class_data = SStudentClassRelation::model()->findAll($criteria);
if($semester_student_class_data)
{
foreach($semester_student_class_data as $v)
{
//在线学生ID
$s_student_ids[(string)$v->student_id] = $v->student_id;
//获取班级中的最大序号
if(isset($class_serial_number[(string)$v->class_id]))
{
if($class_serial_number[(string)$v->class_id]<$v->serial_number)
{
$class_serial_number[(string)$v->class_id] = $v->serial_number;
}
}else
{
$class_serial_number[(string)$v->class_id] = $v->serial_number;
}
$studentInClass[(string)$v->class_id][]=(string)$v->student_id;
$getClassByStudent[(string)$v->student_id]=(string)$v->class_id;
}
}
$needCreateStudent = array();
foreach($all_student as $val){
$extend=array();
if($val['extend']){
$extend=json_decode($val['extend'],true);
//检验扩展信息
$preg_card='/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i';
$preg_phone='/^1[34578]\d{9}$/ims';
foreach ($extend as $key=>$item){
if(!$item && $key!='is_outer'){
unset($extend[$key]);
continue;
}
if($key=='school_student_card'){
// if($cardGrade[$classGrade[$val['class_id']]]==0 || $cardLengthGrade[$classGrade[$val['class_id']]]!=mb_strlen($item)){
// unset($extend[$key]);
// continue;
// }
$students=$this->schoolManager->getStudentIdBySchoolCard($item,$this->schoolId);
if($students){
foreach ($students as $sid){
if(isset($s_student_ids[(string)$sid['student_id']]) && strcmp((string)$sid['student_id'],$val['student_id'])!=0){
$result['msg']='学校准考证号('.$item.')系统中已存在,导入失败!';
exit(json_encode($result));
}
}
}
}elseif($key=='id_number'){
unset($extend[$key]);
continue;
}elseif($key=='student_phone'){
if(!preg_match($preg_phone,$item)){
unset($extend[$key]);
continue;
}
}
}
}
$val=array_merge($val,$extend);
//处理扩展数据
$school_student_card='';
$zhixue_student_card='';
$userno='';
if(isset($val['school_student_card'])){
$school_student_card=$val['school_student_card'];
unset($extend['school_student_card']);
}
if(isset($val['zhixue_student_card'])){
$zhixue_student_card=$val['zhixue_student_card'];
unset($extend['zhixue_student_card']);
}
if(isset($val['userno'])){
$userno=$val['userno'];
unset($extend['userno']);
}
if($val['student_id']){
//转班
$studentInfo = $this->schoolManager->getStudentByStudentIds(array($val['student_id']));
if(!$studentInfo){
// Yii::app()->jump->error('数据异常! ');
$result['msg']='数据异常,导入失败!';
exit(json_encode($result));
}
$studentInfo=$studentInfo[0];
$oldClass=isset($getClassByStudent[$val['student_id']])?$getClassByStudent[$val['student_id']]:0;
//读取当前班级
if(!isset($studentInClass[$val['class_id']]) || !inArray($val['student_id'],$studentInClass[$val['class_id']])){
//班级不同,转班,补充扩展信息
if($oldClass){
$updateStudentClassRelation[$oldClass]['status'][$val['student_id']]=1;
$updateStudentClassRelation[$oldClass]['operation'][$val['student_id']]=2;
}
if(!isset($class_serial_number[$val['class_id']])){
$class_serial_number[$val['class_id']]=0;
}
$insertStudentClassRelation[$val['student_id']]="('".$val['class_id']."','".$val['student_id']."','".($class_serial_number[$val['class_id']]+1)."','".$userno."','".time()."',0)";
$class_serial_number[$val['class_id']]++;
$updateStudentInfo['class_id'][$val['student_id']]="'".$val['class_id']."'";
}else{
//验证是否已删除
if(!isset($s_student_ids[(string)$val['student_id']])){
if(!isset($class_serial_number[$val['class_id']])){
$class_serial_number[$val['class_id']]=0;
}
$insertStudentClassRelation[$val['student_id']]="('".$val['class_id']."','".$val['student_id']."','".($class_serial_number[$val['class_id']]+1)."','".$userno."','".time()."',0)";
$class_serial_number[$val['class_id']]++;
$updateStudentInfo['class_id'][$val['student_id']]="'".$val['class_id']."'";
}
if($oldClass){
if($userno){
$updateStudentClassRelation[$oldClass]['userno'][$val['student_id']]=$userno;
}
}
}
if(isset($val['is_outer'])){
$updateStudentInfo['is_outer'][$val['student_id']]="'".$val['is_outer']."'";
}
//更新business
if($school_student_card || $zhixue_student_card){
if($school_student_card){
$updateBusiness['school_student_card'][$val['student_id']]=$school_student_card;
}
if($zhixue_student_card){
$updateBusiness['zhixue_student_card'][$val['student_id']]=$zhixue_student_card;
}
}
//更新student_info
$updateStudentInfoSql=array();
if($extend){
foreach ($extend as $field=>$value){
if($value){
$updateStudentInfo[$field][$val['student_id']]="'".$value."'";
}
}
}
if($val['id_number'] && !$this->schoolManager->checkStudentIdNumber($val['id_number'])){
$updateStudentInfo['id_number'][$val['student_id']]="'".$val['id_number']."'";
}
}else{
//新学生,创建
$needCreateStudent[]=$val;
}
}
$student_sql=array();
$student_info_sql=array();
$class_student_sql=array();
//创建学生
if($needCreateStudent){
$create_card_data = $this->createCard(count($needCreateStudent));
$studentIdArr=getBatchUuid($this->schoolId,count($needCreateStudent));
foreach ($needCreateStudent as $key => $val){
$student_cart = 0;
foreach ($create_card_data as $v) {
if ($v['use'] == 0) {
$student_cart = $v['student_card'];
$create_card_data[$student_cart]['use'] = 1;
break;
}
}
//$studentId = getUUID();
$studentId=current($studentIdArr);
unset($studentIdArr[$studentId]);
$student_cart = (int)$student_cart;
$password = $student_cart;
$school_student_card='';
$zhixue_student_card='';
$userno='';
$id_number='';
$student_phone=0;
$field_1='';
$field_2='';
$field_3='';
$field_4='';
$field_5='';
if(isset($val['school_student_card']) && !empty($val['school_student_card'])){
$school_student_card=$val['school_student_card'];
}
if(isset($val['zhixue_student_card']) && !empty($val['zhixue_student_card'])){
$zhixue_student_card=$val['zhixue_student_card'];
}
if(isset($val['userno']) && !empty($val['userno'])){
$userno=$val['userno'];
}
if(isset($val['id_number']) && !empty($val['id_number']) && !$this->schoolManager->checkStudentIdNumber($val['id_number'])){
$id_number=$val['id_number'];
}
if(isset($val['student_phone']) && !empty($val['student_phone'])){
$student_phone=$val['student_phone'];
}
if(isset($val['field_1']) && !empty($val['field_1'])){
$field_1=$val['field_1'];
}
if(isset($val['field_2']) && !empty($val['field_2'])){
$field_2=$val['field_2'];
}
if(isset($val['field_3']) && !empty($val['field_3'])){
$field_3=$val['field_3'];
}
if(isset($val['field_4']) && !empty($val['field_4'])){
$field_4=$val['field_4'];
}
if(isset($val['field_5']) && !empty($val['field_5'])){
$field_5=$val['field_5'];
}
// 将学生登录账号插入主数据
if(!isset($class_serial_number[$val['class_id']])){
$class_serial_number[$val['class_id']]=0;
}
$is_outer=0;
if(isset($val['is_outer'])){
$is_outer=$val['is_outer'];
}
$student_sql [$studentId]= "(".$studentId.",'".$student_cart."',".$student_cart.",'".$school_student_card."','".$zhixue_student_card."','".md5(sha1($password . 'wy') . 'fengche')."',".Yii::app()->session['coachInfo']['school_id'].",".time().")";
$student_info_sql[$studentId]= "(".$studentId.",'".$val['modify_name']."',".$val['sex'].",".$this->schoolId.",".$val['class_id'].",'','','".$student_phone."','','','".$is_outer."',".time().",".time().",0,0.00,0,'".$id_number."','".$field_1."','".$field_2."','".$field_3."','".$field_4."','".$field_5."')";
$class_student_sql[$studentId] = "(".$val['class_id'].",".$studentId.",".($class_serial_number[$val['class_id']] +$key+ 1).",'".$userno."',".time().",0)";
}
}
//执行sql
$transaction_conn = $this->conn->beginTransaction();
try {
if($student_sql){
$sql="insert into student (`student_id`,`username`,`student_card`,`school_student_card`,`zhixue_student_card`,`password`,`school_id`,`register_time`) values ";
$sql.=implode(',',$student_sql);
$this->conn->createCommand($sql)->execute();
}
if($updateBusiness){
$businessUpdateSql="update student set ";
$businessUpdateSetArr=array();
$studentId=array();
foreach ($updateBusiness as $field =>$val){
$businessUpdateSetStr=" `".$field."`= case student_id ";
foreach ($val as $id=>$value){
$businessUpdateSetStr.=" WHEN {$id} THEN '{$value}' ";
$studentId[]=$id;
}
$businessUpdateSetStr.="ELSE `{$field}`";
$businessUpdateSetStr.=" END ";
$businessUpdateSetArr[]=$businessUpdateSetStr;
}
if($businessUpdateSetArr){
$businessUpdateSql.=implode(',',$businessUpdateSetArr)." where student_id in (".implode(',',$studentId).") and school_id = '".$this->schoolId."' ";
$this->conn->createCommand($businessUpdateSql)->execute();
}
}
$businessRs=true;
}catch (Exception $ec) {
$transaction_conn->rollback();
//Yii::app()->jump->error('导入失败');
exit(json_encode($result));
}
if($businessRs){
$transaction = $this->sConn->beginTransaction();
try {
if($updateStudentInfo){
$studentInfoUpdateSql="update student_info set ";
$studentInfoUpdateSetArr=array();
$studentId=array();
foreach ($updateStudentInfo as $field =>$val){
$studentInfoUpdateSetStr=" `".$field."`= case student_id ";
foreach ($val as $id=>$value){
$studentInfoUpdateSetStr.=" WHEN {$id} THEN {$value} ";
$studentId[]=$id;
}
$studentInfoUpdateSetStr.="ELSE `{$field}`";
$studentInfoUpdateSetStr.=" END ";
$studentInfoUpdateSetArr[]=$studentInfoUpdateSetStr;
}
if($studentInfoUpdateSetArr){
$studentInfoUpdateSql.=implode(',',$studentInfoUpdateSetArr)." where student_id in (".implode(',',$studentId).") ";
$this->sConn->createCommand($studentInfoUpdateSql)->execute();
}
}
if($updateStudentClassRelation){
foreach ($updateStudentClassRelation as $class_id =>$updateValue){
$studentClassUpdateSql="update student_class_relation set ";
$studentClassUpdateSetArr=array();
foreach($updateValue as $field =>$val){
$studentClassUpdateSetStr=" `".$field."`= case student_id ";
foreach ($val as $id=>$value){
$studentClassUpdateSetStr.=" WHEN {$id} THEN '{$value}' ";
}
$studentClassUpdateSetStr.="ELSE `{$field}`";
$studentClassUpdateSetStr.=" END ";
$studentClassUpdateSetArr[]=$studentClassUpdateSetStr;
}
if($studentClassUpdateSetArr){
$studentClassUpdateSql.=implode(',',$studentClassUpdateSetArr)." where class_id ='".$class_id."' ";
$this->sConn->createCommand($studentClassUpdateSql)->execute();
}
}
}
if($insertStudentClassRelation){
$class_sql="insert into student_class_relation(`class_id`,`student_id`,`serial_number`,`userno`,`update_time`,`operation`) values";
$class_sql.=implode(',',$insertStudentClassRelation);
$this->sConn->createCommand($class_sql)->execute();
}
if($student_info_sql){
$s_sql = 'INSERT INTO `student_info`
(`student_id`,`realname`,`sex`,`school_id`,`class_id`,`family_tel`,`telephone`,`student_phone`,`picture`,`signature`,`is_outer`,`add_time`,`update_time`,`level`,`ave_score_rate`,`game_stars`,`id_number`,`field_1`,`field_2`,`field_3`,`field_4`,`field_5`)VALUES';
$s_sql.=implode(',',$student_info_sql);
$this->sConn->createCommand($s_sql)->execute();
}
if($class_student_sql){
$class_sql="insert into student_class_relation(`class_id`,`student_id`,`serial_number`,`userno`,`update_time`,`operation`) values ";
$class_sql.=implode(',',$class_student_sql);
$this->sConn->createCommand($class_sql)->execute();
}
//$delSql="truncate table import_student_temp;";
$delSql="delete from import_student_temp;";
$this->sConn->createCommand($delSql)->execute();
$transaction_conn->commit();
$transaction->commit();
if(Yii::app()->params['handle_log_on_off'])
{
writeFileLog(jsonEncode(array(
"exam_group_id" => 0,
"operate_project" => 'zsyas2',
"school_id" => $this->schoolId,
"title" => '导入学生',
"operate_account" => Yii::app()->session['coachInfo']['coach_name'],
"operate_method" => $this->action,
"operate_url" => $this->getRoute(),
"operate_sql" =>'',
"operate_param" =>'',
"date"=>date('Y-m-d H:i:j')
)));
}
$result['status']=1;
}catch (Exception $e) {
$transaction->rollback();
$transaction_conn->rollback();
//Yii::app()->jump->error('导入失败');
exit(json_encode($result));
}
}
unset($transaction);
unset($transaction_conn);
// 导入成功
//$this->redirect(Yii::app()->createUrl('importstudent/index'));
exit(json_encode($result));
}
//预生成系统准考证号
private function createCard($num)
{
$cards = array();
$student_cart = 0;
if(!$num)
{
return $cards;
}
/*
$b_student_data = BusinessStudent::model()->find(array(
'select' => 'student_card',
'order' => 'student_card desc',
'condition' => 'school_id=:school_id',
'limit' => 1,
'params' => array(':school_id'=>$this->schoolId)
));
if($b_student_data)
{
if(!$b_student_data->student_card)
{
if($this->schoolId<100)
{
$student_cart = '9'.$this->schoolId.'00100';
}else
{
$student_cart = $this->schoolId.'00100';
}
}else
{
$student_cart = $b_student_data->student_card;
}
}else
{
if($this->schoolId<100)
{
$student_cart = '9'.$this->schoolId.'00100';
}else
{
$student_cart = $this->schoolId.'00100';
}
}
*/
$b_student_data=$this->conn->createCommand("SELECT student_card FROM (SELECT student_card FROM `student` `t` WHERE school_id='".$this->schoolId."') sc ORDER BY student_card DESC LIMIT 1;")->queryRow();
if($b_student_data)
{
if(!$b_student_data['student_card'])
{
if($this->schoolId<100)
{
$student_cart = '9'.$this->schoolId.'00100';
}else
{
$student_cart = $this->schoolId.'00100';
}
}else
{
$student_cart = $b_student_data['student_card'];
}
}else
{
if($this->schoolId<100)
{
$student_cart = '9'.$this->schoolId.'00100';
}else
{
$student_cart = $this->schoolId.'00100';
}
}
unset($b_student_data);
for($i=1;$i<=$num;$i++)
{
$cards[$student_cart+$i] = array(
'student_card'=>$student_cart+$i,
'use'=>0,
'order'=>$i
);
}
return $cards;
}
//重名处理后检查
public function actionRepeat_check(){
$str=Req::post('str');
$name=Req::post('name');
$result['status']=0;
$studentIds=array();
$tempIds=array();
if($str){
foreach($str as $val){
@$nToId=explode('///',$val);
if(isset($nToId[0])){
if(in_array((string)$nToId[0],$studentIds,true)){
$result['msg']='不能学生重复选择';
exit(json_encode($result));
}else{
$studentIds[]=(string)$nToId[0];
}
}
if(isset($nToId[1])){
if(in_array((string)$nToId[1],$tempIds,true)){
$result['msg']='不能学生重复选择';
exit(json_encode($result));
}else{
$tempIds[]=(string)$nToId[1];
}
}
$nToId=null;
}
$updateSql=array();
foreach($str as $val){
@$nToId=explode('///',$val);
if(isset($nToId[0]) && isset($nToId[1])){
//判断学生id是否已关联过
$related=$this->sConn->createCommand("select modify_name from import_student_temp where student_id='".$nToId[0]."' limit 1")->queryRow();
if($related){
$result['msg']='学生已经被关联,请重新检查:'.$related['modify_name'];
exit(json_encode($result));
}
$updateSql[]="update import_student_temp set student_id='".$nToId[0]."',is_repeat=0,is_normal=1 where id='".$nToId[1]."'";
}
}
if($updateSql){
$transaction = $this->sConn->beginTransaction();
try {
foreach ($updateSql as $query) {
$this->sConn->createCommand($query)->execute();
}
$transaction->commit();
}catch (Exception $e) {
$transaction->rollback();
$result['msg']='关联失败';
exit(json_encode($result));
}
}
}
unset($transaction);
$updateIdNumberSql=array();
$sql="select id,id_number from import_student_temp where modify_name='".$name."' and is_repeat=1";
$excel_data = $this->sConn->createCommand($sql)->queryAll();
if($excel_data){
foreach ($excel_data as $val){
if($val['id_number']){
$updateIdNumberSql[]="update import_student_temp set is_repeat=0,is_normal=1 where id='".$val['id']."'";
}
}
}
if($updateIdNumberSql){
$transaction = $this->sConn->beginTransaction();
try {
foreach($updateIdNumberSql as $query){
$this->sConn->createCommand($query)->execute();
}
$transaction->commit();
}catch (Exception $e) {
$transaction->rollback();
$result['msg']='关联失败';
exit(json_encode($result));
}
}
$result['status']=1;
exit(json_encode($result));
}
//正常数据列表
public function actionNormal(){
$condition = array();
$class_id = safe_replace(Yii::app()->request->getQuery('classId'));
$realname = safe_replace(Yii::app()->request->getQuery('realname'));
$grade = safe_replace(Yii::app()->request->getQuery('grade'));
if($class_id){
$condition[]=" class_id='".$class_id."'";
}
if($realname){
$condition[]=" modify_name like '%".$realname."%'";
}
$condition[]="is_normal=1";
$rs=$this->schoolManager->getNormalStudent($condition,20);
$fileName=array(
'userno'=>'学号',
'school_student_card'=>'学校准考证号',
'zhixue_student_card'=>'智学网准考证号',
'id_number'=>'身份证号',
'student_phone'=>'手机号',
'is_outer'=>'在籍生'
);
//扩展字段
$studentExtend=$this->schoolManager->getStudentExtend();
if($studentExtend){
foreach ($studentExtend as $key=> $value){
$fileName[$key]=$value['field_mean'];
}
}
$data['pages'] = $rs['pager'];
$data['page_total'] = $rs['pager']->rowsCount;
$data['list']=$rs['rs'];
$data['class_id']=$class_id;
$data['grade']=$grade;
$data['realname']=$realname;
$extendTh='';
if($data['list']){
foreach ($data['list'] as $key=>$val){
$extendTd='';
$json=json_decode($val['extend'],true);
unset($json['id_number']);
if(!$extendTh){
foreach ($json as $k=>$v){
$extendTh.=''.$fileName[$k].' | ';
}
}
foreach ($json as $k=>$v){
if($k=='is_outer'){
if($v){
$v='否';
}else{
$v='是';
}
}
$extendTd.=''.$v.' | ';
}
$data['list'][$key]['ext']=$extendTd;
}
}
$data['extTh']=$extendTh;
$this->render('normal',$data);
}
//根据班级读取学生
public function actionLoadStudentByClassName(){
$realname = safe_replace(Req::post('classname'));
$result['status']=0;
if(!$realname){
exit(json_encode($result));
}
$fileName=array(
'userno'=>'学号',
'school_student_card'=>'学校准考证号',
'zhixue_student_card'=>'智学网准考证号',
'id_number'=>'身份证号',
'student_phone'=>'手机号',
'is_outer'=>'在籍生'
);
//扩展字段
$studentExtend=$this->schoolManager->getStudentExtend();
if($studentExtend){
foreach ($studentExtend as $key=> $value){
$fileName[$key]=$value['field_mean'];
}
}
$sex[1]='男';
$sex[2]='女';
$all_student=$this->sConn->createCommand("select student_name,modify_name,sex,extend,class_id,student_id,id_number from import_student_temp where class_name='".$realname."' and class_not_found=1")->queryAll();
$extendTh='';
$html='';
$html.="姓名 | ";
$html.="性别 | ";
$html.="身份证 | ";
if($all_student){
if($all_student){
foreach ($all_student as $key=>$val){
$extendTd='
';
$extendTd.="{$val['modify_name']}(原{$val['student_name']}) | ";
$extendTd.="{$sex[$val['sex']]} | ";
$extendTd.="{$val['id_number']} | ";
$json=json_decode($val['extend'],true);
unset($json['id_number']);
if(!$extendTh){
foreach ($json as $k=>$v){
$extendTh.=''.$fileName[$k].' | ';
}
$html.=$extendTh.'
';
}
foreach ($json as $k=>$v){
$extendTd.=''.$v.' | ';
}
$extendTd.="";
$html.=$extendTd;
}
$html.="";
}
}
$result['html']=$html;
$result['status']=1;
exit(json_encode($result));
}
private function getInClassStudents($class_ids)
{
$data = array();
$student_ids = array();
$student_names = array();
$student_cards = array();
$school_student_cards = array();
$userno = array();
$serial_number = array();
$student_class_ids = array();
$student_info = array();
$class_student = array();
$criteria = new CDbCriteria();
$criteria->addInCondition('class_id',$class_ids);
$criteria->addCondition('status=:status');
$criteria->params[':status'] = 0;
$criteria->order = 'serial_number desc,class_id desc';
$student_class_data = SStudentClassRelation::model()->findAll($criteria);
if($student_class_data)
{
foreach($student_class_data as $key=>$v)
{
if(isset($serial_number[$v->class_id]))
{
if($serial_number[$v->class_id]<$v->serial_number)
{
$serial_number[$v->class_id] = $v->serial_number;
}
}else
{
$serial_number[$v->class_id] = $v->serial_number;
}
$student_ids[$v->student_id] = $v->student_id;
$userno[$v->student_id] = $v->userno;
$class_student[$v->student_id] = $v;
$student_class_ids[$v->student_id] = $v->class_id;
}
$criteria = new CDbCriteria();
$criteria->addInCondition('student_id',$student_ids);
$student_data = SStudentInfo::model()->findAll($criteria);
if($student_data)
{
foreach($student_data as $v)
{
$student_names[$v->student_id] = $v->realname;
$student_info[$v->student_id] = $v;
}
}
unset($student_data);
unset($criteria);
$criteria = new CDbCriteria();
$criteria->addInCondition('student_id',$student_ids);
$b_student_data = BusinessStudent::model()->findAll($criteria);
if($b_student_data)
{
foreach($b_student_data as $v)
{
$student_cards[$v->student_id] = $v->student_card;
if($v->school_student_card)
{
$school_student_cards[$v->student_id] = $v->school_student_card;
}
}
}
unset($b_student_data);
$data['student_names'] = $student_names;
$data['student_cards'] = $student_cards;
$data['student_ids'] = $student_ids;
$data['school_student_cards'] = $school_student_cards;
$data['student_userno'] = $userno;
$data['student_info'] = $student_info;
$data['class_student'] = $class_student;
$data['student_class_ids'] = $student_class_ids;
$data['serial_number'] = $serial_number;
}
return $data;
}
//教学班导入学生
public function actionImportStudentClassified(){
$this->sConn->createCommand("DELETE FROM `import_student_temp` where import_type=1 ")->execute();
$student_all_data = $this->sConn->createCommand("SELECT id FROM `import_student_temp` limit 1 ")->queryRow();
if($student_all_data){
$this->render('check_classified');
}else{
//读取科目设置
$subjects=array();
$rs=$this->schoolManager->getSubjectByGrade(0,0,$this->semesterId);
if($rs){
foreach ($rs as $val){
$subjects[$val['subject_id']]=Yii::app()->params['subjectId'][$val['subject_id']];
}
}
$data['subjects']=$subjects;
$this->render('import_classified',$data);
}
}
public function actionImportClassified(){
ini_set ('memory_limit', '300M');
if(!$_FILES){
Yii::app()->jump->error('文件大小超过范围');
}else {
if (!isset($_FILES['student_file']) || !isset($_FILES['student_file']['size']) || $_FILES['student_file']['size'] > 5242880) {
Yii::app()->jump->error('文件大小超过范围');
}
}
$subjectId=Req::post('subject');
if (Yii::app()->request->getIsPostRequest()) {
$uploader = new Uploader("upload/tmpDir/UploadClassesStudentsList/{$this->schoolId}/");
$uploader->allowTypes = array("xls","xlsx");
$uploader->fieldsMappings = array("exname" => array(0 => $this->schoolId));
$file = $uploader->act();
$file = Arr::current(Arr::current($file));
if (!$file || !isset($file["error"]))
Yii::app()->jump->error('请上传的Excel!');
if ($file["error"] != 0) {
switch ($file["error"]) {
case 2001:
Yii::app()->jump->error('文件类型不符');
break;
case 2002:
Yii::app()->jump->error('文件大小超出允许范围');
break;
default:
Yii::app()->jump->error('上传失败');
break;
}
}
$inputFileName = $file["src"];
if(Yii::app()->params['handle_log_on_off'])
{
writeFileLog(jsonEncode(array(
"exam_group_id" => 0,
"operate_project" => 'zsyas2',
"school_id" => $this->schoolId,
"title" => '导入教学班学生excel',
"operate_account" => Yii::app()->session['coachInfo']['coach_name'],
"operate_method" => $this->action,
"operate_url" => $this->getRoute(),
"operate_sql" =>'',
"operate_param" =>json_encode(array('post'=>$_POST,'get'=>$_GET,'file'=>$inputFileName)),
"date"=>date('Y-m-d H:i:j')
)));
}
// 导入PHPExcel类
Yii::import('application.extensions.*');
require_once('phpexcel/PHPExcel/IOFactory.php');
try{
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
//FIXME 2019-12-23
@unlink($file["src"]);
}catch (Exception $e){
//FIXME 2019-12-23
@unlink($file["src"]);
Yii::app()->jump->error('Excel格式不正确:'.$e->getMessage());
}
if(!$sheetData)
{
Yii::app()->jump->error('请正确的Excel!');
}
unset($objPHPExcel);
unset($sheetData[1]);
if(!$sheetData)
{
Yii::app()->jump->error('请输入学生数据');
}
//获取当前学期的所有班级
$semesterId = $this->semesterId;
$class_semester_data = ClassModel::model()->findAll('semester_id=:semester_id and class_type=2',array(':semester_id'=>$semesterId));
if($class_semester_data)
{
foreach($class_semester_data as $v)
{
$class_ids[$v->class_id] = $v->class_id;
$c_class_names[$v->class_id] = (string)$v->class_name;
}
}
unset($class_semester_data);
if(!$class_ids)
{
Yii::app()->jump->error('当前学期无班级信息,导入失败');
}
$insertValue="insert into import_student_temp(`student_name`,`class_name`,`student_card`,`import_type`,`subject_id`) values ";
$values=array();
$studentCard=array();
foreach ($sheetData as $val){
if(!$val['A'] || !$val['B'] ){
//Yii::app()->jump->error('班级,姓名,性别不能为空');
continue;
}
if($val['C']){
$studentCard[$val['C']][]=$val['C'];
}
$values[]="('".$val['B']."','".$val['A']."','".$val['C']."',2,'".$subjectId."')";
}
//检测系统准考证号是否重复
$errorStudentCard=array();
if($studentCard){
foreach ($studentCard as $item){
if(count($item)>1){
$errorStudentCard[]=$item[0];
}
}
}
if($errorStudentCard){
if(count($errorStudentCard)>3){
Yii::app()->jump->error('系统准考证号重复: '.implode(',',array_slice($errorStudentCard,0,3)).' ...... ');
}else{
Yii::app()->jump->error('系统准考证号重复: '.implode(',',$errorStudentCard));
}
}
if($values){
$sql=$insertValue.implode(',',$values);
$transaction = $this->sConn->beginTransaction();
try{
$this->sConn->createCommand($sql)->execute();
$transaction->commit();
// Yii::app()->jump->error('Excel导入成功!');
//跳转检测页
$this->redirect($this->createUrl('importstudent/checkClassified'));
}catch(Exception $e){
$transaction->rollBack();
Yii::app()->jump->error('Excel导入失败!');
}
}else{
Yii::app()->jump->error('Excel导入失败!');
}
}
}
public function actionCheckClassified(){
$this->render('check_classified');
}
//检测姓名
public function actionCheck_student_name_classified(){
ini_set('memory_limit','512M');
set_time_limit(0);
$result['status']=0;
$result['data']=0;
$id_number=array();
$clearIdNumber=array(); //需要清除身份证
$studentCard=array();
$student_all_data = $this->sConn->createCommand("SELECT id,student_name,id_number FROM `import_student_temp` `t` ")->queryAll();
if($student_all_data){
//去除非中文字符
$updateArr=array();
$preg="/[^\x{4E00}-\x{9FFF}^·]+/u";
$count=0;
foreach($student_all_data as $val){
if(preg_match($preg,$val['student_name'])){
$ModifyName = preg_replace($preg, "", $val['student_name']);
if($ModifyName){
$updateArr[$val['id']]=$ModifyName;
$count++;
}
}else{
$updateArr[$val['id']]=$val['student_name'];
}
}
//组织更新语句
if($updateArr){
$arrNumber=ceil(count($updateArr)/500);
$Arr=array_chunk($updateArr,$arrNumber,true);
$transcation = $this->sConn->beginTransaction();
try {
foreach ($Arr as $item){
$sql='update import_student_temp set `modify_name`= case id ';
foreach ($item as $key=> $val){
$sql.=" WHEN ".$key." THEN '".$val."' ";
}
$sql.=" End ";
$sql.=" where modify_name='' or modify_name is null";
$this->sConn->createCommand($sql)->execute();
}
$transcation->commit();
$result['status']=1;
}catch (Exception $e){
$transcation->rollback();
}
}
$result['count']=$count;
}
exit(json_encode($result));
}
//匹配学生信息
public function actionMatchingClassified(){
$classNotFound=array(); //班级名称不一致
$repeat=array(); //重名
$success=array(); //正常
$updateClassValue=array(); //需要更新的班级不存在数据
$updateRepeatValue=array(); //需要更新重复数据
$studentName=array();
$studentCardValue=array(); //准考证号不存在
$updateStudentCard=array();
$idNumber=array();
$updateStudentId=array(); //匹配成功需要更新student_id数据
$updateClassId=array(); //更新班级id
$result['status']=0;
$student_all_data = $this->sConn->createCommand("SELECT id,student_name,class_name,modify_name,student_id,is_repeat,class_not_found,is_normal,id_number,student_card,subject_id FROM `import_student_temp` `t` ")->queryAll();
if(!$student_all_data){
$result['msg']='请先导入excel数据';
exit(json_encode($result));
}
$nowSemester=$this->semesterId;
foreach ($student_all_data as $key=>$val){
if($val['is_repeat']==1){
$repeat[]=$val['id'];
unset($student_all_data[$key]);
}
if($val['class_not_found']==1){
$classNotFound[$val['class_name']][]=$val;
unset($student_all_data[$key]);
}
if($val['class_not_found']==2){
$studentCardValue[]=$val['id'];
unset($student_all_data[$key]);
}
if($val['is_normal']==1){
$success[]=1;
unset($student_all_data[$key]);
}
}
if(!$student_all_data){
$result['studentCard']=count($studentCardValue);
$result['repeat']=count($repeat);
$result['success']=count($success);
$result['noExistent']=count($classNotFound);
$result['status']=1;
exit(json_encode($result));
}
//判断班级是否存在
foreach($student_all_data as $val){
$class_id=$this->schoolManager->classNameExists($val['class_name'],$nowSemester,2);
if(!$class_id){
$classNotFound[$val['class_name']][]=$val;
$updateClassValue[]="'".$val['class_name']."'"; //班级不存在
}else{
//校验科目是否匹配
$targetSubjects=$this->schoolManager->getSubjectByClass($class_id,'Arr');
if(!in_array($val['subject_id'],$targetSubjects)){
$classNotFound[$val['class_name']][]=$val;
$updateClassValue[]="'".$val['class_name']."'"; //班级不存在
continue;
}
$isFoundCard=false;
//判断准考证号
if($val['student_card']){
$sql="select student_id from student where student_card ='".$val['student_card']."' and status=0 and school_id='".$this->schoolId."'";
$b_student=$this->conn->createCommand($sql)->queryRow();
if($b_student){
//验证是否有行政班数据
$checkXZB=$this->sConn->createCommand("select student_id from student_class_relation where student_id='".$b_student['student_id']."' and class_type=1 and status=0")->queryRow();
if($checkXZB){
//验证是否有教学班数据,有则转班
$classifiedSql="SELECT c.class_id,c.class_name,subject_id,scr.student_id FROM `class_subject_relation` csr ";
$classifiedSql.="join class c on csr.class_id=c.class_id ";
$classifiedSql.="join student_class_relation scr on scr.class_id=c.class_id ";
$classifiedSql.="where subject_id=".$val['subject_id']." and c.class_type=2 and student_id='".$b_student['student_id']."';";
$studentClassified=$this->sConn->createCommand($classifiedSql)->queryRow();
if($studentClassified){
$updateStudentCard[]="update import_student_temp set class_id='".$class_id."',student_id='".$b_student['student_id']."',is_normal=2 where student_card='".$val['student_card']."'";
}else{
$updateStudentCard[]="update import_student_temp set class_id='".$class_id."',student_id='".$b_student['student_id']."',is_normal=1 where student_card='".$val['student_card']."'";
}
$success[]=$val['id'];
$isFoundCard=true;
}
}
}
if(!$isFoundCard){
//用学生姓名匹配
$getAllStudentName = $this->schoolManager->getStudentByNameClass($val['modify_name'],$nowSemester);
if(count($getAllStudentName)!=1){
$repeat[]=$val['id'];
}else{
//判断教学班
$subjectRepeat=false;
$classifiedStudent=$this->schoolManager->getStudentByNameClassified($val['modify_name'],$nowSemester);
if($classifiedStudent){
//判断学科
foreach ($classifiedStudent as $item){
$subjects=$this->schoolManager->getSubjectByClass($item['class_id'],'Arr');
if($subjects && in_array($val['subject_id'],$subjects)){ //已有教学班科目
$subjectRepeat=true;
$updateStudentCard[]="update import_student_temp set class_id='".$class_id."',student_id='".$item['student_id']."',is_normal=2 where modify_name='".$val['modify_name']."'";
break;
}
}
}
if(!$subjectRepeat){
$updateStudentCard[]="update import_student_temp set class_id='".$class_id."',student_id='".$getAllStudentName[0]['student_id']."',is_normal=1 where modify_name='".$val['modify_name']."'";
}
$success[]=$val['id'];
}
}
}
}
//更新检测成功的数据
if( $updateClassValue || $success || $updateStudentCard || $repeat){
$transcation = $this->sConn->beginTransaction();
try {
if($updateClassValue){
$updateClassValue=array_values($updateClassValue);
$this->sConn->createCommand("update import_student_temp set class_not_found=1 where class_name in(".implode(',',$updateClassValue).")")->execute();
}
if($updateStudentCard){
foreach ($updateStudentCard as $query){
$this->sConn->createCommand($query)->execute();
}
}
if($repeat){
$this->sConn->createCommand("update import_student_temp set is_repeat=1 where id in(".implode(',',$repeat).")")->execute();
}
if($success){
$success=array_values($success);
$this->sConn->createCommand("update import_student_temp set is_normal=1 where id in(".implode(',',$success).")")->execute();
}
$transcation->commit();
$result['status']=1;
}catch (Exception $e){
$result['status']=0;
$transcation->rollback();
}
}
$result['studentCard']=count($studentCardValue);
$result['repeat']=count($repeat);
$result['success']=count($success);
$result['noExistent']=count($classNotFound);
$result['status']=1;
exit(json_encode($result));
}
//班级名称不匹配
public function actionHandle_classified_existent(){
$notFound=$this->sConn->createCommand("select id,class_name from import_student_temp where class_not_found=1")->queryAll();
if(!$notFound) $this->redirect($this->createUrl('importstudent/checkClassified'));
$className=array();
foreach ($notFound as $val){
$className[$val['class_name']][]=$val;
}
$data['className']=$className;
$this->render('handle_classified_existent',$data);
}
//修改教学班级名称
public function actionChange_classified_value(){
$old_class_name=Req::post('data');
$value=Req::post('value');
$result['status']=0;
if(!$old_class_name || !$value){
$result['msg']='参数不正确';
exit(json_encode($result));
}
$valueArr=array();
$sql="select id,modify_name,student_name,sex,extend,student_id,student_card,subject_id from import_student_temp where class_name='".$old_class_name."' ";
$data=$this->sConn->createCommand($sql)->queryAll();
if(!$data){
$result['msg']='数据异常';
exit(json_encode($result));
}
$nowSemester=$this->semesterId;
$class_id=$this->schoolManager->classNameExists($value,$nowSemester,2);
if(!$class_id){
$result['status']=0;
$result['msg']='班级在系统中不存在';
exit(json_encode($result));
}else{
//校验科目是否匹配
$targetSubjects=$this->schoolManager->getSubjectByClass($class_id,'Arr');
foreach ($data as $key => $val){
if(!in_array($val['subject_id'],$targetSubjects)){
$result['status']=0;
$result['msg']='班级和选择的学科不匹配';
exit(json_encode($result));
}
$isFoundCard=false;
//判断准考证号
if($val['student_card']){
$sql="select student_id from student where student_card ='".$val['student_card']."' and status=0 and school_id='".$this->schoolId."'";
$b_student=$this->conn->createCommand($sql)->queryRow();
if($b_student){
//验证是否有教学班数据,有则转班
$classifiedSql="SELECT c.class_id,c.class_name,subject_id,scr.student_id FROM `class_subject_relation` csr ";
$classifiedSql.="join class c on csr.class_id=c.class_id ";
$classifiedSql.="join student_class_relation scr on scr.class_id=c.class_id ";
$classifiedSql.="where subject_id=".$val['subject_id']." and c.class_type=2 and student_id='".$b_student['student_id']."';";
$studentClassified=$this->sConn->createCommand($classifiedSql)->queryRow();
if($studentClassified){
$valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','".$b_student['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
}else{
$valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','".$b_student['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
}
$isFoundCard=true;
}
}
if(!$isFoundCard){
//用学生姓名匹配
$getAllStudentName = $this->schoolManager->getStudentByNameClass($val['modify_name'],$nowSemester);
if(count($getAllStudentName)!=1){
$valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','','".$class_id."','".$val['student_card']."',1,0,0)";
}else{
//判断教学班
$subjectRepeat=false;
$classifiedStudent=$this->schoolManager->getStudentByNameClassified($val['modify_name'],$nowSemester);
if($classifiedStudent){
//判断学科
foreach ($classifiedStudent as $item){
$subjects=$this->schoolManager->getSubjectByClass($item['class_id']);
if($subjects && in_array($val['subject_id'],$subjects)){ //已有教学班科目
$subjectRepeat=true;
$valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','".$item['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
break;
}
}
}
if(!$subjectRepeat){
$valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','".$getAllStudentName[0]['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
}
}
}
}
$transcation = $this->sConn->beginTransaction();
try {
if($valueArr) {
$insert="insert into import_student_temp(`student_name`,`class_name`,`sex`,`modify_name`,`extend`,`student_id`,`class_id`,`student_card`,`is_repeat`,`class_not_found`,`is_normal`) values ";
$this->sConn->createCommand("delete from import_student_temp where class_name='".$old_class_name."'")->execute();
$this->sConn->createCommand($insert.implode(',',$valueArr))->execute();
}
$transcation->commit();
$result['status']=1;
}catch (Exception $e){
$result['status']=0;
$transcation->rollback();
}
exit(json_encode($result));
}
}
//修改准考证号码
public function actionChangeClassifiedStudentCard(){
$id=Req::post('id');
$value=Req::post('value');
$result['status']=0;
if(!$id || !$value){
$result['msg']='参数不正确';
exit(json_encode($result));
}
$excel_data=$this->sConn->createCommand("select student_name,subject_id from import_student_temp where id='".$id."'")->queryRow();
if(!$excel_data){
$result['msg']='参数不正确';
exit(json_encode($result));
}
//判断表格中是否存在
$sql="select id,student_name from import_student_temp where student_card='".$value."' and id!='".$id."' ";
$data=$this->sConn->createCommand($sql)->queryRow();
if($data){
$result['msg']='导入的数据中中已存在相同准考证'.$data['student_name'].',请检查后重新确认';
exit(json_encode($result));
}
//验证系统中是否存在
$sql="select student_id from student where student_card ='".$value."' and status=0 and school_id='".$this->schoolId."'";
$b_student=$this->conn->createCommand($sql)->queryRow();
if(!$b_student){
$result['msg']='准考证号不存在,请检查后重新确认';
exit(json_encode($result));
}else{
//判断姓名
$student_card_name=$this->sConn->createCommand("select realname from student_info where student_id='".$b_student['student_id']."'")->queryRow();
if(!$student_card_name){
$result['msg']='准考证号不存在,请检查后重新确认';
exit(json_encode($result));
}elseif($student_card_name['realname']!=$excel_data['student_name']){
$result['msg']='新的准考证号与表格姓名不匹配,请检查后重新确认';
exit(json_encode($result));
}else{
//验证是否有教学班数据,有则转班
$classifiedSql="SELECT c.class_id,c.class_name,subject_id,scr.student_id FROM `class_subject_relation` csr ";
$classifiedSql.="join class c on csr.class_id=c.class_id ";
$classifiedSql.="join student_class_relation scr on scr.class_id=c.class_id ";
$classifiedSql.="where subject_id=".$excel_data['subject_id']." and c.class_type=2 and student_id='".$b_student['student_id']."';";
$studentClassified=$this->sConn->createCommand($classifiedSql)->queryRow();
if($studentClassified){
if($this->sConn->createCommand("update import_student_temp set student_card='".$value."',is_normal=2,is_repeat=0,class_not_found=0,student_id='".$b_student['student_id']."' where id='".$id."'")->execute()){
$result['status']=1;
}
}else{
if($this->sConn->createCommand("update import_student_temp set student_card='".$value."',is_normal=1,is_repeat=0,class_not_found=0,student_id='".$b_student['student_id']."' where id='".$id."'")->execute()){
$result['status']=1;
}
}
}
}
exit(json_encode($result));
}
//正常数据列表
public function actionNormalClassified(){
$condition = array();
$class_id = safe_replace(Yii::app()->request->getQuery('classId'));
$realname = safe_replace(Yii::app()->request->getQuery('realname'));
$grade = safe_replace(Yii::app()->request->getQuery('grade'));
if($class_id){
$condition[]=" class_id='".$class_id."'";
}
if($realname){
$condition[]=" modify_name like '%".$realname."%'";
}
$condition[]="is_normal!=0";
$rs=$this->schoolManager->getNormalStudent($condition,20);
$data['pages'] = $rs['pager'];
$data['page_total'] = $rs['pager']->rowsCount;
$data['list']=$rs['rs'];
$data['class_id']=$class_id;
$data['grade']=$grade;
$data['realname']=$realname;
$this->render('normal_classified',$data);
}
//根据年级读取教学班
public function actionGetClassifiedByGrade(){
$grade=Req::post('grade');
$semester=$this->semesterId;
$class=ClassModel::model()->getClassBySemesternianji($semester,$grade,2);
$result=array(
'status'=>1
);
$data=array();
if($class){
foreach($class as $val){
$data[]=array(
'class_id'=>$val->class_id,
'class_name'=>$val->class_name,
);
}
}
$result['data']=$data;
exit(json_encode($result));
}
//匹配失败
public function actionHandleClassifiedCard(){
$notFound=$this->sConn->createCommand("select id,student_name,modify_name,class_name,student_card from import_student_temp where is_repeat=1")->queryAll();
if(!$notFound) $this->redirect($this->createUrl('importstudent/checkClassified'));
$data['student']=$notFound;
$this->render('handle_classified_card',$data);
}
//导入学生执行
public function actionConfirm_classified_import(){
$result['status']=0;
$all_student=$this->sConn->createCommand("select modify_name,sex,extend,class_id,student_id,id_number,student_card,subject_id,is_normal from import_student_temp where is_repeat=0 and class_not_found=0 and is_normal<>0")->queryAll();
if(!$all_student){
$result['msg']='没有可导入的数据!';
exit(json_encode($result));
}
$updateStudentClassRelation = array();
$insertStudentClassRelation = array();
$semesterId = $this->semesterId;
$class_semester_data = ClassModel::model()->findAll('semester_id=:semester_id and class_type=2',array(':semester_id'=>$semesterId));
$classGrade=array();
if($class_semester_data)
{
foreach($class_semester_data as $v)
{
$class_ids[$v->class_id] = $v->class_id;
$classGrade[$v->class_id] = $v->grade;
}
}
unset($class_semester_data);
if(!$class_ids)
{
//Yii::app()->jump->error('无班级信息,导入失败');
$result['msg']='无班级信息,导入失败!';
exit(json_encode($result));
}
$class_serial_number = array();
// 获取在线学生
$criteria = new CDbCriteria();
$criteria->addInCondition('class_id',$class_ids);
$criteria->addCondition('status=0');
$semester_student_class_data = SStudentClassRelation::model()->findAll($criteria);
if($semester_student_class_data)
{
foreach($semester_student_class_data as $v)
{
//获取班级中的最大序号
if(isset($class_serial_number[(string)$v->class_id]))
{
if($class_serial_number[(string)$v->class_id]<$v->serial_number)
{
$class_serial_number[(string)$v->class_id] = $v->serial_number;
}
}else
{
$class_serial_number[(string)$v->class_id] = $v->serial_number;
}
}
}
foreach($all_student as $val){
if($val['student_id']){
//转班
$studentInfo = $this->schoolManager->getStudentByStudentIds(array($val['student_id']));
if(!$studentInfo){
$result['msg']='数据异常,导入失败!';
exit(json_encode($result));
}
//查询目标班是否已存在
$checkRepeat=$this->sConn->createCommand("select student_id,status from student_class_relation where class_id='".$val['class_id']."' and student_id='".$val['student_id']."'")->queryRow();
if($checkRepeat){
if($checkRepeat['status']==0){
continue;
}
}
if(!isset($class_serial_number[$val['class_id']])){
$class_serial_number[$val['class_id']]=0;
}
$insertStudentClassRelation[$val['student_id']]="('".$val['class_id']."','".$val['student_id']."','".($class_serial_number[$val['class_id']]+1)."','0','".time()."',0,2)";
//删除相同科目其它教学班数据
$classifiedSql="SELECT c.class_id,c.class_name,subject_id,scr.student_id FROM `class_subject_relation` csr ";
$classifiedSql.="join class c on csr.class_id=c.class_id ";
$classifiedSql.="join student_class_relation scr on scr.class_id=c.class_id ";
$classifiedSql.="where subject_id=".$val['subject_id']." and c.class_type=2 and student_id='".$val['student_id']."';";
$studentClassified=$this->sConn->createCommand($classifiedSql)->queryRow();
if($studentClassified){
$updateStudentClassRelation[$studentClassified['class_id']]['status'][$val['student_id']]=1;
$updateStudentClassRelation[$studentClassified['class_id']]['operation'][$val['student_id']]=2;
}
$class_serial_number[$val['class_id']]++;
}
}
//执行sql
$transaction = $this->sConn->beginTransaction();
try {
if($updateStudentClassRelation){
foreach ($updateStudentClassRelation as $class_id =>$updateValue){
$studentClassUpdateSql="update student_class_relation set ";
$studentClassUpdateSetArr=array();
foreach($updateValue as $field =>$val){
$studentClassUpdateSetStr=" `".$field."`= case student_id ";
foreach ($val as $id=>$value){
$studentClassUpdateSetStr.=" WHEN {$id} THEN '{$value}' ";
}
$studentClassUpdateSetStr.="ELSE `{$field}`";
$studentClassUpdateSetStr.=" END ";
$studentClassUpdateSetArr[]=$studentClassUpdateSetStr;
}
if($studentClassUpdateSetArr){
$studentClassUpdateSql.=implode(',',$studentClassUpdateSetArr)." where class_id ='".$class_id."' ";
$this->sConn->createCommand($studentClassUpdateSql)->execute();
}
}
}
if($insertStudentClassRelation){
$class_sql="insert into student_class_relation(`class_id`,`student_id`,`serial_number`,`userno`,`update_time`,`operation`,`class_type`) values";
$class_sql.=implode(',',$insertStudentClassRelation);
$this->sConn->createCommand($class_sql)->execute();
}
//$delSql="truncate table import_student_temp;";
$delSql="delete from import_student_temp;";
$this->sConn->createCommand($delSql)->execute();
$transaction->commit();
if(Yii::app()->params['handle_log_on_off'])
{
writeFileLog(jsonEncode(array(
"exam_group_id" => 0,
"operate_project" => 'zsyas2',
"school_id" => $this->schoolId,
"title" => '导入教学班学生',
"operate_account" => Yii::app()->session['coachInfo']['coach_name'],
"operate_method" => $this->action,
"operate_url" => $this->getRoute(),
"operate_sql" =>'',
"operate_param" =>'',
"date"=>date('Y-m-d H:i:s')
)));
}
$result['status']=1;
}catch (Exception $e) {
$transaction->rollback();
exit(json_encode($result));
}
unset($transaction);
exit(json_encode($result));
}
//根据教学班级读取学生
public function actionLoadStudentByClassified(){
$realname = safe_replace(Req::post('classname'));
$result['status']=0;
if(!$realname){
exit(json_encode($result));
}
$all_student=$this->sConn->createCommand("select class_name,student_name,modify_name,class_id,student_id,student_card from import_student_temp where class_name='".$realname."' and class_not_found=1")->queryAll();
$extendTh='';
$html='';
$html.="教学班名称 | ";
$html.="姓名 | ";
$html.="准考证号 | ";
if($all_student){
if($all_student){
foreach ($all_student as $key=>$val){
$extendTd='
';
$extendTd.="{$val['class_name']} | ";
$extendTd.="{$val['modify_name']}(原{$val['student_name']}) | ";
$extendTd.="{$val['student_card']} | ";
$extendTd.="
";
$html.=$extendTd;
}
$html.="";
}
}
$result['html']=$html;
$result['status']=1;
exit(json_encode($result));
}
}