ImportstudentController.php 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860
  1. <?php
  2. /**
  3. * 导入学生控制器类
  4. * @author li
  5. * @date 2018-12-24 9:50:00
  6. * @company 上海风车教育有限公司.
  7. */
  8. class ImportstudentController extends Controller
  9. {
  10. public $fileName=array(
  11. 'userno'=>'学号',
  12. 'school_student_card'=>'学校准考证号',
  13. 'zhixue_student_card'=>'智学网准考证号',
  14. 'id_number'=>'身份证号',
  15. 'student_phone'=>'手机号',
  16. 'is_outer'=>'在籍生'
  17. );
  18. public function actionIndex(){
  19. $this->sConn->createCommand("DELETE FROM `import_student_temp` where import_type=2 ")->execute();
  20. $student_all_data = $this->sConn->createCommand("SELECT id FROM `import_student_temp` limit 1 ")->queryRow();
  21. if($student_all_data){
  22. $this->render('check');
  23. }else{
  24. $this->render('index');
  25. }
  26. }
  27. public function actionTemplate(){
  28. $fileName=array(
  29. 'userno'=>'学号',
  30. 'school_student_card'=>'学校准考证号',
  31. 'zhixue_student_card'=>'智学网准考证号',
  32. 'id_number'=>'身份证号',
  33. 'student_phone'=>'手机号',
  34. 'is_outer'=>'在籍生'
  35. );
  36. //扩展字段
  37. $studentExtend=$this->schoolManager->getStudentExtend();
  38. if($studentExtend){
  39. foreach ($studentExtend as $key=> $value){
  40. if($value['is_enable']==1){
  41. $fileName[$key]=$value['field_mean'];
  42. }
  43. }
  44. }
  45. $data['fileName']=$fileName;
  46. $template=$this->sConn->createCommand("select tpl_data from import_student_template limit 1")->queryRow();
  47. if($template && isset($template['tpl_data'])){
  48. $data['tpl_data']=json_decode($template['tpl_data'],true);
  49. }
  50. $this->render('template',$data);
  51. }
  52. public function actionSave_template(){
  53. $tpl_data=Req::post('data');
  54. $result['status']=0;
  55. if(!$tpl_data) exit(json_encode($result));
  56. $table=$tpl_data[0];
  57. $tpl_data=array_slice($table,5);
  58. foreach ($tpl_data as $key=>$val){
  59. if($val=='-1'){
  60. unset($tpl_data[$key]);
  61. }
  62. }
  63. $template=$this->sConn->createCommand("select id from import_student_template limit 1")->queryRow();
  64. if($template){
  65. $this->sConn->createCommand("update import_student_template set tpl_data='".json_encode($tpl_data)."'")->execute();
  66. }else{
  67. $this->sConn->createCommand("insert into import_student_template set tpl_data='".json_encode($tpl_data)."'")->execute();
  68. }
  69. $result['status']=1;
  70. exit(json_encode($result));
  71. }
  72. public function actionImport(){
  73. ini_set ('memory_limit', '300M');
  74. if(!$_FILES){
  75. Yii::app()->jump->error('文件大小超过范围');
  76. }else {
  77. if (!isset($_FILES['student_file']) || !isset($_FILES['student_file']['size']) || $_FILES['student_file']['size'] > 5242880) {
  78. Yii::app()->jump->error('文件大小超过范围');
  79. }
  80. }
  81. if (Yii::app()->request->getIsPostRequest()) {
  82. //读取模板
  83. $template=$this->schoolManager->getImportStudentTemplate();
  84. if(!$template){
  85. Yii::app()->jump->error('请先设置导入学生模板');
  86. }
  87. @$tpl_data=json_decode($template,true);
  88. if(!$tpl_data){
  89. //Yii::app()->jump->error('模板数据异常,请重新设置模板');
  90. }
  91. $uploader = new Uploader("upload/tmpDir/UploadClassesStudentsList/{$this->schoolId}/");
  92. $uploader->allowTypes = array("xls","xlsx");
  93. $uploader->fieldsMappings = array("exname" => array(0 => $this->schoolId));
  94. $file = $uploader->act();
  95. $file = Arr::current(Arr::current($file));
  96. if (!$file || !isset($file["error"]))
  97. Yii::app()->jump->error('请上传的Excel!');
  98. if ($file["error"] != 0) {
  99. switch ($file["error"]) {
  100. case 2001:
  101. Yii::app()->jump->error('文件类型不符');
  102. break;
  103. case 2002:
  104. Yii::app()->jump->error('文件大小超出允许范围');
  105. break;
  106. default:
  107. Yii::app()->jump->error('上传失败');
  108. break;
  109. }
  110. }
  111. $inputFileName = $file["src"];
  112. if(Yii::app()->params['handle_log_on_off'])
  113. {
  114. writeFileLog(jsonEncode(array(
  115. "exam_group_id" => 0,
  116. "operate_project" => 'zsyas2',
  117. "school_id" => $this->schoolId,
  118. "title" => '导入学生',
  119. "operate_account" => Yii::app()->session['coachInfo']['coach_name'],
  120. "operate_method" => $this->action,
  121. "operate_url" => $this->getRoute(),
  122. "operate_sql" =>'',
  123. "operate_param" =>json_encode(array('post'=>$_POST,'get'=>$_GET,'file'=>$inputFileName)),
  124. "date"=>date('Y-m-d H:i:j')
  125. )));
  126. }
  127. // 导入PHPExcel类
  128. Yii::import('application.extensions.*');
  129. require_once('phpexcel/PHPExcel/IOFactory.php');
  130. try{
  131. $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
  132. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
  133. //FIXME 2019-12-23
  134. @unlink($file["src"]);
  135. }catch (Exception $e){
  136. //FIXME 2019-12-23
  137. @unlink($file["src"]);
  138. Yii::app()->jump->error('Excel格式不正确:'.$e->getMessage());
  139. }
  140. if(!$sheetData)
  141. {
  142. Yii::app()->jump->error('请正确的Excel!');
  143. }
  144. unset($objPHPExcel);
  145. //扩展字段
  146. $studentExtend=$this->schoolManager->getStudentExtend();
  147. if($studentExtend){
  148. foreach ($studentExtend as $key=> $value){
  149. if($value['is_enable']==1){
  150. $this->fileName[$key]=$value['field_mean'];
  151. }
  152. }
  153. }
  154. //检查模板
  155. $tempHeader=array(
  156. '班级','姓名','性别(男/女)','系统准考证号'
  157. );
  158. foreach ($tpl_data as $datum){
  159. $tempHeader[]=$this->fileName[$datum];
  160. }
  161. $sheetHead=$sheetData[1];
  162. if(count($sheetHead)!=count($tempHeader)){
  163. Yii::app()->jump->error('导入学生Excel格式与模板不匹配! ');
  164. }
  165. $sheetHead=array_values($sheetHead);
  166. foreach ($tempHeader as $key => $value){
  167. if(!isset($sheetHead[$key]) || trim($sheetHead[$key])!=$value){
  168. Yii::app()->jump->error('导入学生Excel格式与模板不匹配:'.$value);
  169. }
  170. }
  171. unset($sheetData[1]);
  172. if(!$sheetData)
  173. {
  174. Yii::app()->jump->error('请输入学生数据');
  175. }
  176. //获取当前学期的所有班级
  177. $semesterId = $this->semesterId;
  178. $class_semester_data = ClassModel::model()->findAll('semester_id=:semester_id',array(':semester_id'=>$semesterId));
  179. if($class_semester_data)
  180. {
  181. foreach($class_semester_data as $v)
  182. {
  183. $class_ids[$v->class_id] = $v->class_id;
  184. $c_class_names[$v->class_id] = (string)$v->class_name;
  185. }
  186. }
  187. unset($class_semester_data);
  188. if(!$class_ids)
  189. {
  190. Yii::app()->jump->error('当前学期无班级信息,导入失败');
  191. }
  192. $insertValue="insert into import_student_temp(`student_name`,`class_name`,`sex`,`extend`,`id_number`,`student_card`) values ";
  193. $values=array();
  194. $studentCard=array();
  195. $schoolStudentCard=array();
  196. foreach ($sheetData as $val){
  197. if(!$val['A'] || !$val['B'] || !$val['C']){
  198. //Yii::app()->jump->error('班级,姓名,性别不能为空');
  199. continue;
  200. }
  201. if(count($val)!=count($tpl_data)+4){
  202. Yii::app()->jump->error('导入学生Excel格式与模板不匹配! ');
  203. }
  204. if($val['D']){
  205. $studentCard[$val['D']][]=$val['D'];
  206. }
  207. //扩展信息基本校验
  208. $extendKeyValue=array();
  209. if($tpl_data){
  210. $extend=array_slice($val,4);
  211. $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';
  212. $preg_phone='/^1[34578]\d{9}$/ims';
  213. $extend=array_values($extend);
  214. foreach ($extend as $key=>$v){
  215. if(!$v){
  216. $extendKeyValue[$tpl_data[$key]]='';
  217. continue;
  218. }
  219. switch ($tpl_data[$key]){
  220. case 'userno':
  221. if(!is_numeric($v)){
  222. Yii::app()->jump->error('学号只能是数字! ');
  223. }
  224. break;
  225. case 'school_student_card':
  226. if(!is_numeric($v)){
  227. Yii::app()->jump->error('学校准考证号格式错误! ');
  228. }
  229. $schoolStudentCard[$v][]=$v;
  230. break;
  231. case 'id_number':
  232. if(!preg_match($preg_card,$v)){
  233. Yii::app()->jump->error('身份证格式不正确! ');
  234. }
  235. break;
  236. case 'student_phone':
  237. if(!preg_match($preg_phone,$v)){
  238. Yii::app()->jump->error('手机号格式不正确! ');
  239. }
  240. break;
  241. case 'zhixue_student_card':
  242. if(!is_numeric($v)){
  243. Yii::app()->jump->error('智学网准考证号格式错误! ');
  244. }
  245. break;
  246. case 'is_outer':
  247. $isOuter=0;
  248. if(!isset($v) || !$v || $v=='是') {
  249. $v = '是';
  250. $isOuter = 0;
  251. }elseif($v=='否'){
  252. $isOuter = 1;
  253. }elseif(!in_array($v,array('是','否'))){
  254. Yii::app()->jump->error('在籍生填写错误! ');
  255. }
  256. $v=$isOuter;
  257. break;
  258. }
  259. $extendKeyValue[$tpl_data[$key]]=$v;
  260. }
  261. }
  262. $sex=1;
  263. if($val['C']=='女'){
  264. $sex=2;
  265. }
  266. $id_number=0;
  267. if(isset($extendKeyValue['id_number'])){
  268. $id_number=$extendKeyValue['id_number'];
  269. }
  270. if(!$val['D']){
  271. $val['D']=0;
  272. }
  273. $values[]="('".$val['B']."','".$val['A']."','".$sex."','".jsonEncode($extendKeyValue)."','".$id_number."','".$val['D']."')";
  274. }
  275. //检测系统准考证号是否重复
  276. $errorStudentCard=array();
  277. if($studentCard){
  278. foreach ($studentCard as $item){
  279. if(count($item)>1){
  280. $errorStudentCard[]=$item[0];
  281. }
  282. }
  283. }
  284. if($errorStudentCard){
  285. if(count($errorStudentCard)>3){
  286. Yii::app()->jump->error('系统准考证号重复: '.implode(',',array_slice($errorStudentCard,0,3)).' ...... ');
  287. }else{
  288. Yii::app()->jump->error('系统准考证号重复: '.implode(',',$errorStudentCard));
  289. }
  290. }
  291. $errorSchoolStudentCard=array();
  292. if($schoolStudentCard){
  293. foreach ($schoolStudentCard as $item){
  294. if(count($item)>1){
  295. $errorSchoolStudentCard[]=$item[0];
  296. }
  297. }
  298. }
  299. if($errorSchoolStudentCard){
  300. if(count($errorSchoolStudentCard)>3){
  301. Yii::app()->jump->error('学校准考证号重复: '.implode(',',array_slice($errorSchoolStudentCard,0,3)).' ...... ');
  302. }else{
  303. Yii::app()->jump->error('学校准考证号重复: '.implode(',',$errorSchoolStudentCard));
  304. }
  305. }
  306. if($values){
  307. $sql=$insertValue.implode(',',$values);
  308. $transaction = $this->sConn->beginTransaction();
  309. try{
  310. $this->sConn->createCommand($sql)->execute();
  311. $transaction->commit();
  312. // Yii::app()->jump->error('Excel导入成功!');
  313. //跳转检测页
  314. $this->redirect($this->createUrl('importstudent/check'));
  315. }catch(Exception $e){
  316. $transaction->rollBack();
  317. Yii::app()->jump->error('Excel导入失败!');
  318. }
  319. }else{
  320. Yii::app()->jump->error('Excel导入失败!');
  321. }
  322. }
  323. }
  324. public function actionCheck(){
  325. $this->render('check');
  326. }
  327. //检测姓名
  328. public function actionCheck_student_name(){
  329. ini_set('memory_limit','512M');
  330. set_time_limit(0);
  331. $result['status']=0;
  332. $result['data']=0;
  333. $id_number=array();
  334. $clearIdNumber=array(); //需要清除身份证
  335. $studentCard=array();
  336. $student_all_data = $this->sConn->createCommand("SELECT id,student_name,id_number FROM `import_student_temp` `t` ")->queryAll();
  337. if($student_all_data){
  338. //去除非中文字符
  339. $updateArr=array();
  340. $preg="/[^\x{2E80}-\x{FE4F}0-9^·]+/u";
  341. $count=0;
  342. foreach($student_all_data as $val){
  343. if($val['id_number']){
  344. //判断系统中是否存在
  345. if($this->schoolManager->checkStudentIdNumber($val['id_number'])){
  346. $clearIdNumber[]=$val['id'];
  347. }
  348. $id_number[$val['id_number']][]=$val['id'];
  349. }
  350. if(preg_match($preg,$val['student_name'])){
  351. $ModifyName = preg_replace($preg, "", $val['student_name']);
  352. if($ModifyName){
  353. $updateArr[$val['id']]=$ModifyName;
  354. $count++;
  355. }
  356. }else{
  357. $updateArr[$val['id']]=$val['student_name'];
  358. }
  359. }
  360. //判断身份证是否有重复
  361. if($id_number){
  362. foreach ($id_number as $number=>$vid){
  363. if(count($vid)>1){
  364. $clearIdNumber=array_merge($clearIdNumber,$vid);
  365. }
  366. }
  367. }
  368. //组织更新语句
  369. if($updateArr){
  370. $arrNumber=ceil(count($updateArr)/500);
  371. $Arr=array_chunk($updateArr,$arrNumber,true);
  372. $transcation = $this->sConn->beginTransaction();
  373. try {
  374. if($clearIdNumber){
  375. $this->sConn->createCommand("update import_student_temp set id_number=0 where id in(".implode(',',$clearIdNumber).")")->execute();
  376. }
  377. foreach ($Arr as $item){
  378. $sql='update import_student_temp set `modify_name`= case id ';
  379. foreach ($item as $key=> $val){
  380. $sql.=" WHEN ".$key." THEN '".$val."' ";
  381. }
  382. $sql.=" End ";
  383. $sql.=" where modify_name='' or modify_name is null";
  384. $this->sConn->createCommand($sql)->execute();
  385. }
  386. $transcation->commit();
  387. $result['status']=1;
  388. }catch (Exception $e){
  389. $transcation->rollback();
  390. }
  391. }
  392. $result['count']=$count;
  393. }
  394. exit(json_encode($result));
  395. }
  396. //匹配学生信息
  397. public function actionMatching(){
  398. $classNotFound=array(); //班级名称不一致
  399. $repeat=array(); //重名
  400. $success=array(); //正常
  401. $updateClassValue=array(); //需要更新的班级不存在数据
  402. $updateRepeatValue=array(); //需要更新重复数据
  403. $studentName=array();
  404. $studentCardValue=array(); //准考证号不存在
  405. $updateStudentCard=array();
  406. $idNumber=array();
  407. $updateStudentId=array(); //匹配成功需要更新student_id数据
  408. $updateClassId=array(); //更新班级id
  409. $result['status']=0;
  410. $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();
  411. if(!$student_all_data){
  412. $result['msg']='请先导入excel数据';
  413. exit(json_encode($result));
  414. }
  415. $preSemesterId=$this->schoolManager->getPrevSemesterId();
  416. $nowSemester=$this->semesterId;
  417. foreach ($student_all_data as $key=>$val){
  418. if($val['is_repeat']==1){
  419. $repeat[$val['modify_name']][]=$val;
  420. unset($student_all_data[$key]);
  421. }
  422. if($val['class_not_found']==1){
  423. $classNotFound[$val['class_name']][]=$val;
  424. unset($student_all_data[$key]);
  425. }
  426. if($val['class_not_found']==2){
  427. $studentCardValue[]=$val['id'];
  428. unset($student_all_data[$key]);
  429. }
  430. if($val['is_normal']==1){
  431. $success[]=1;
  432. unset($student_all_data[$key]);
  433. }
  434. }
  435. if(!$student_all_data){
  436. $result['studentCard']=count($studentCardValue);
  437. $result['repeat']=count($repeat);
  438. $result['success']=count($success);
  439. $result['noExistent']=count($classNotFound);
  440. $result['status']=1;
  441. exit(json_encode($result));
  442. }
  443. //判断班级是否存在
  444. foreach($student_all_data as $val){
  445. $class_id=$this->schoolManager->classNameExists($val['class_name'],$nowSemester);
  446. if(!$class_id){
  447. $classNotFound[$val['class_name']][]=$val;
  448. $updateClassValue[]="'".$val['class_name']."'"; //班级不存在
  449. }else{
  450. //判断准考证号
  451. if($val['student_card']){
  452. $sql="select student_id from student where student_card ='".$val['student_card']."' and status=0 and school_id='".$this->schoolId."'";
  453. $b_student=$this->conn->createCommand($sql)->queryRow();
  454. if($b_student){
  455. //判断姓名
  456. $student_card_name=$this->sConn->createCommand("select realname from student_info where student_id='".$b_student['student_id']."'")->queryRow();
  457. if(!$student_card_name){
  458. $studentCardValue[]=$val['id'];
  459. $updateClassId[$val['class_name']]="update import_student_temp set class_id='".$class_id."' where class_name='".$val['class_name']."'";
  460. }elseif($student_card_name['realname']!=$val['student_name']){
  461. $studentCardValue[]=$val['id'];
  462. $updateClassId[$val['class_name']]="update import_student_temp set class_id='".$class_id."' where class_name='".$val['class_name']."'";
  463. }else{
  464. //存在学生
  465. $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']."'";
  466. $success[]=$val['id'];
  467. }
  468. }else{
  469. $updateClassId[$val['class_name']]="update import_student_temp set class_id='".$class_id."' where class_name='".$val['class_name']."'";
  470. $studentCardValue[]=$val['id'];
  471. }
  472. }else{
  473. $updateClassId[$val['class_name']]="update import_student_temp set class_id='".$class_id."' where class_name='".$val['class_name']."'";
  474. $studentName[$val['modify_name']][]=$val['id'];
  475. $idNumber[$val['id']]=$val['id_number'];
  476. }
  477. }
  478. }
  479. //判断姓名是否正常
  480. if($studentName){
  481. foreach ($studentName as $k=>$v){
  482. if(count($v)>1){ //当前excel中学生(去掉姓名中非汉字字符后)存在重名
  483. $repeat[]=$v;
  484. $updateRepeatValue=array_merge($updateRepeatValue,$v);
  485. }else{
  486. //读取系统中数据
  487. $student=$this->schoolManager->getStudentByNameClass($k,$nowSemester,$idNumber[$v[0]]);
  488. if(!$student){
  489. //不存在,正常数据 需要创建
  490. $success[]=$v[0];
  491. // }elseif(count($student)==1){
  492. //存在且唯一,正常数据 需要转班 !!!没有系统准考证号,都需要重新确认
  493. // $success[]=$v[0];
  494. // $updateStudentId[$v[0]]=(string)$student[0]['student_id'];
  495. }else{
  496. //系统中重名
  497. $repeat[]=$v[0];
  498. $updateRepeatValue[]=$v[0];
  499. }
  500. }
  501. }
  502. }
  503. //更新检测成功的数据
  504. if($updateRepeatValue || $updateStudentId || $updateClassValue || $success || $studentCardValue || $updateClassId || $updateStudentCard){
  505. $transcation = $this->sConn->beginTransaction();
  506. try {
  507. if($studentCardValue){
  508. //准考证号不存在
  509. $this->sConn->createCommand("update import_student_temp set class_not_found=2 where id in(".implode(',',$studentCardValue).")")->execute();
  510. }
  511. if($updateStudentId) {
  512. $sql = 'update import_student_temp set `student_id`= case id ';
  513. foreach ($updateStudentId as $key => $val) {
  514. $sql .= " WHEN " . $key . " THEN '" . $val . "' ";
  515. }
  516. $sql .= " End ";
  517. $sql .= " where student_id='' or student_id is null";
  518. $this->sConn->createCommand($sql)->execute();
  519. }
  520. if($updateRepeatValue){
  521. $this->sConn->createCommand("update import_student_temp set is_repeat=1 where id in(".implode(',',$updateRepeatValue).")")->execute();
  522. }
  523. if($updateClassValue){
  524. $updateClassValue=array_values($updateClassValue);
  525. $this->sConn->createCommand("update import_student_temp set class_not_found=1 where class_name in(".implode(',',$updateClassValue).")")->execute();
  526. }
  527. if($updateClassId){
  528. foreach ($updateClassId as $query){
  529. $this->sConn->createCommand($query)->execute();
  530. }
  531. }
  532. if($updateStudentCard){
  533. foreach ($updateStudentCard as $query){
  534. $this->sConn->createCommand($query)->execute();
  535. }
  536. }
  537. if($success){
  538. $success=array_values($success);
  539. $this->sConn->createCommand("update import_student_temp set is_normal=1 where id in(".implode(',',$success).")")->execute();
  540. }
  541. $transcation->commit();
  542. $result['status']=1;
  543. }catch (Exception $e){
  544. $result['status']=0;
  545. $transcation->rollback();
  546. }
  547. }
  548. $result['studentCard']=count($studentCardValue);
  549. $result['repeat']=count($repeat);
  550. $result['success']=count($success);
  551. $result['noExistent']=count($classNotFound);
  552. $result['status']=1;
  553. exit(json_encode($result));
  554. }
  555. //重新上传
  556. public function actionGiveup(){
  557. //$delSql="truncate table import_student_temp;";
  558. $delSql="delete from import_student_temp;";
  559. $this->sConn->createCommand($delSql)->execute();
  560. $result['status']=1;
  561. exit(json_encode($result));
  562. }
  563. //系统准考证不存在
  564. public function actionHandle_existent_card(){
  565. $notFound=$this->sConn->createCommand("select id,student_name,modify_name,class_name,student_card from import_student_temp where class_not_found=2")->queryAll();
  566. if(!$notFound) $this->redirect($this->createUrl('importstudent/check'));
  567. $data['student']=$notFound;
  568. $this->render('handle_existent_card',$data);
  569. }
  570. //系统不存在
  571. public function actionHandle_existent(){
  572. $notFound=$this->sConn->createCommand("select id,class_name from import_student_temp where class_not_found=1")->queryAll();
  573. if(!$notFound) $this->redirect($this->createUrl('importstudent/check'));
  574. $className=array();
  575. foreach ($notFound as $val){
  576. $className[$val['class_name']][]=$val;
  577. }
  578. $data['className']=$className;
  579. $this->render('handle_existent',$data);
  580. }
  581. //修改班级名称
  582. public function actionChange_value(){
  583. $old_class_name=Req::post('data');
  584. $value=Req::post('value');
  585. $result['status']=0;
  586. if(!$old_class_name || !$value){
  587. $result['msg']='参数不正确';
  588. exit(json_encode($result));
  589. }
  590. $valueArr=array();
  591. $sql="select id,modify_name,student_name,sex,extend,student_id,student_card from import_student_temp where class_name='".$old_class_name."' ";
  592. $data=$this->sConn->createCommand($sql)->queryAll();
  593. if(!$data){
  594. $result['msg']='数据异常';
  595. exit(json_encode($result));
  596. }
  597. $nowSemester=$this->semesterId;
  598. $class_id=$this->schoolManager->classNameExists($value,$nowSemester);
  599. $updateClassId=array();
  600. if(!$class_id){
  601. $result['status']=0;
  602. $result['msg']='班级在系统中不存在';
  603. exit(json_encode($result));
  604. }else{
  605. foreach ($data as $key => $val){
  606. //判断准考证号
  607. if($val['student_card']){
  608. $sql="select student_id from student where student_card ='".$val['student_card']."' and status=0 and school_id='".$this->schoolId."'";
  609. $b_student=$this->conn->createCommand($sql)->queryRow();
  610. if($b_student){
  611. //存在学生
  612. $valueArr[]="('".$val['student_name']."','".$value."','".$val['sex']."','".$val['modify_name']."','".$val['extend']."','".$b_student['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
  613. unset($data[$key]);
  614. }else{
  615. $valueArr[]="('".$val['student_name']."','".$value."','".$val['sex']."','".$val['modify_name']."','".$val['extend']."','".$b_student['student_id']."','".$class_id."','".$val['student_card']."',0,2,0)";
  616. unset($data[$key]);
  617. }
  618. }
  619. }
  620. if($data){
  621. //班级存在,判断姓名
  622. //读取系统中数据
  623. $nameArr=array();
  624. foreach ($data as $val){
  625. $nameArr[$val['modify_name']][]=$val;
  626. }
  627. foreach ($nameArr as $Name=>$val){
  628. if(count($val)>1){ //当前excel中学生(去掉姓名中非汉字字符后)存在重名
  629. foreach ($val as $v){
  630. if(!$v['student_id']){
  631. $v['student_id']=0;
  632. }
  633. $valueArr[]="('".$v['student_name']."','".$value."','".$v['sex']."','".$v['modify_name']."','".$v['extend']."','".$v['student_id']."','".$class_id."','".$v['student_card']."',1,0,0)";
  634. }
  635. }else{
  636. $student=$this->schoolManager->getStudentByNameClass($Name,$nowSemester);
  637. if(!$val[0]['student_id']){
  638. $val[0]['student_id']=0;
  639. }
  640. if(!$student){
  641. //不存在,正常数据 需要创建
  642. $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)";
  643. // }elseif(count($student)==1){
  644. //存在且唯一,正常数据 需要转班
  645. // $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)";
  646. }else{
  647. //系统中重名
  648. $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)";
  649. }
  650. }
  651. }
  652. }
  653. $transcation = $this->sConn->beginTransaction();
  654. try {
  655. if($valueArr) {
  656. $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 ";
  657. $this->sConn->createCommand("delete from import_student_temp where class_name='".$old_class_name."'")->execute();
  658. $this->sConn->createCommand($insert.implode(',',$valueArr))->execute();
  659. }
  660. $transcation->commit();
  661. $result['status']=1;
  662. }catch (Exception $e){
  663. $result['status']=0;
  664. $transcation->rollback();
  665. }
  666. exit(json_encode($result));
  667. }
  668. }
  669. //修改身份证号码
  670. public function actionChange_idnumber(){
  671. $id=Req::post('id');
  672. $value=Req::post('value');
  673. $result['status']=0;
  674. if(!$id || !$value){
  675. $result['msg']='参数不正确';
  676. exit(json_encode($result));
  677. }
  678. //判断系统中是否存在
  679. if($this->schoolManager->checkStudentIdNumber($value)){
  680. $result['status']=0;
  681. $result['msg']='系统中已存在相同身份证,请检查后重新确认';
  682. exit(json_encode($result));
  683. }
  684. //判断表格中是否存在
  685. $sql="select id,modify_name from import_student_temp where id_number='".$value."' ";
  686. $data=$this->sConn->createCommand($sql)->queryRow();
  687. if($data){
  688. $result['msg']='导入的数据中中已存在相同身份证,请检查后重新确认';
  689. exit(json_encode($result));
  690. }
  691. if($this->sConn->createCommand("update import_student_temp set id_number='".$value."',is_normal=1,is_repeat=0 where id='".$id."'")->execute()){
  692. $result['status']=1;
  693. }
  694. exit(json_encode($result));
  695. }
  696. //修改准考证号码
  697. public function actionChange_studentcard(){
  698. $id=Req::post('id');
  699. $value=Req::post('value');
  700. $result['status']=0;
  701. if(!$id || !$value){
  702. $result['msg']='参数不正确';
  703. exit(json_encode($result));
  704. }
  705. $excel_data=$this->sConn->createCommand("select student_name from import_student_temp where id='".$id."'")->queryRow();
  706. if(!$excel_data){
  707. $result['msg']='参数不正确';
  708. exit(json_encode($result));
  709. }
  710. //判断表格中是否存在
  711. $sql="select id,student_name from import_student_temp where student_card='".$value."' and id!='".$id."' ";
  712. $data=$this->sConn->createCommand($sql)->queryRow();
  713. if($data){
  714. $result['msg']='导入的数据中中已存在相同准考证'.$data['student_name'].',请检查后重新确认';
  715. exit(json_encode($result));
  716. }
  717. //验证系统中是否存在
  718. $sql="select student_id from student where student_card ='".$value."' and status=0 and school_id='".$this->schoolId."'";
  719. $b_student=$this->conn->createCommand($sql)->queryRow();
  720. if(!$b_student){
  721. $result['msg']='准考证号不存在,请检查后重新确认';
  722. exit(json_encode($result));
  723. }else{
  724. //判断姓名
  725. $student_card_name=$this->sConn->createCommand("select realname from student_info where student_id='".$b_student['student_id']."'")->queryRow();
  726. if(!$student_card_name){
  727. $result['msg']='准考证号不存在,请检查后重新确认';
  728. exit(json_encode($result));
  729. }elseif($student_card_name['realname']!=$excel_data['student_name']){
  730. $result['msg']='新的准考证号与表格姓名不匹配,请检查后重新确认';
  731. exit(json_encode($result));
  732. }else{
  733. 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()){
  734. $result['status']=1;
  735. }
  736. }
  737. }
  738. exit(json_encode($result));
  739. }
  740. //重名学生
  741. public function actionHandle_repeat(){
  742. $page=Req::post('page');
  743. $name_like=Req::post('name');
  744. if(!$page) $page=1;
  745. $pageSize=10;
  746. $offset=($page-1)*$pageSize;
  747. $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 ";
  748. if($name_like){
  749. $sql.=" having check_field_value='".$name_like."'";
  750. }
  751. $sql.="order by count desc";
  752. $all_data=$this->sConn->createCommand($sql)->queryAll();
  753. if(!$all_data){
  754. Yii::app()->jump->error('没有重名的数据! ');
  755. }
  756. $total=count($all_data);
  757. $data['total_page']=ceil($total/$pageSize);
  758. $data['name_group']=array_slice($all_data,$offset,$pageSize);
  759. $data['page']=$page;
  760. if(Yii::app()->request->isAjaxRequest){
  761. $result['status']=1;
  762. $result['data']=$data['name_group'];
  763. $result['total_page']=$data['total_page'];
  764. exit(json_encode($result));
  765. }else{
  766. $this->render('handle_card', $data);
  767. }
  768. }
  769. //根据名称加载重名学生
  770. public function actionShowrepeatstudent(){
  771. $name=Req::post('name');
  772. $fileName=array(
  773. 'userno'=>'学号',
  774. 'school_student_card'=>'学校准考证号',
  775. 'zhixue_student_card'=>'智学网准考证号',
  776. 'id_number'=>'身份证号',
  777. 'student_phone'=>'手机号',
  778. 'is_outer'=>'在籍生'
  779. );
  780. //扩展字段
  781. $studentExtend=$this->schoolManager->getStudentExtend();
  782. if($studentExtend){
  783. foreach ($studentExtend as $key=> $value){
  784. $fileName[$key]=$value['field_mean'];
  785. }
  786. }
  787. $classIds=array();
  788. $sql="select * from import_student_temp where modify_name='".$name."' and is_repeat=1";
  789. $nameArr=array();
  790. $systemStudent=array();
  791. $numberToId=array();
  792. $excel_data = $this->sConn->createCommand($sql)->queryAll();
  793. if($excel_data){
  794. $topic_score=json_decode($excel_data[0]['extend'],true);
  795. $sex[1]='男';
  796. $sex[2]="女";
  797. $excel_html='<table class="table table-striped table-bordered table-hover dataTable no-footer DTTT_selectable">';
  798. $excel_html.='<tr>';
  799. $excel_html.='<th>序号</th>';
  800. $excel_html.='<th>班级</th>';
  801. $excel_html.='<th>姓名</th>';
  802. $excel_html.='<th>身份证号</th>';
  803. $excel_html.='<th>性别</th>';
  804. $i=1;
  805. if(isset($topic_score['id_number'])){
  806. unset($topic_score['id_number']);
  807. }
  808. foreach ($topic_score as $key=>$item){
  809. if(isset($fileName[$key])){
  810. $excel_html.='<th>'.$fileName[$key].'</th>';
  811. }else{
  812. $excel_html.='<th>导入信息'.$i.'</th>';
  813. }
  814. $i++;
  815. }
  816. $excel_html.='</tr>';
  817. $i=1;
  818. foreach($excel_data as $val){
  819. $extend=json_decode($val['extend'],true);
  820. $numberToId[$i]=$val['id'];
  821. $excel_html.='<tr>';
  822. $excel_html.='<td>'.$i.'</td>';
  823. $excel_html.='<td>'.$val['class_name'].'</td>';
  824. $excel_html.='<td>'.$val['modify_name'].'(原'.$val['student_name'].')</td>';
  825. if(isset($extend['id_number'])){
  826. unset($extend['id_number']);
  827. }
  828. $excel_html.='<td><input type="text" maxlength="20" value="'.$val['id_number'].'" data="'.$val['id_number'].'" class="change_card" id="'.$val['id'].'"></td>';
  829. $excel_html.='<td>'.$sex[$val['sex']].'</td>';
  830. foreach($extend as $k => $v){
  831. if($k=='is_outer'){
  832. if($v){
  833. $v='否';
  834. }else{
  835. $v='是';
  836. }
  837. }
  838. $excel_html.='<td>'.$v.'</td>';
  839. }
  840. $excel_html.='</tr>';
  841. $i++;
  842. }
  843. $excel_html.='</table>';
  844. $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 ";
  845. $sys_sql.="LEFT JOIN class c on sc.class_id=c.class_id ";
  846. $sys_sql.="LEFT JOIN semester s on s.semester_id=c.semester_id ";
  847. $sys_sql.=" where `realname` = '".$name."' and s.semester_id ='".$this->semesterId."' ";
  848. $sys_sql.="order by end_time desc ;";
  849. $_allStudent = $this->sConn->createCommand($sys_sql)->queryAll();
  850. $nameArr=array();
  851. $studentData=array();
  852. $businessData=array();
  853. $semester=array();
  854. $studentId=array();
  855. $html='';
  856. if($_allStudent){
  857. foreach ($_allStudent as $val){
  858. $val['serial_number']=0;
  859. $val['userno']=0;
  860. //班级信息
  861. $sql_class_relation="select * from student_class_relation where student_id='".$val['student_id']."' and status=0 ";
  862. $student_class_realtion=$this->sConn->createCommand($sql_class_relation)->queryRow();
  863. if($student_class_realtion){
  864. $val['serial_number']=$student_class_realtion['serial_number'];
  865. $val['userno']=$student_class_realtion['userno'];
  866. }else{
  867. continue; //没有班级信息则过滤
  868. }
  869. //查询副号
  870. $sql_fuhao="select * from student_relation where find_in_set('".$val['student_id']."',relation_student_id)";
  871. $student_fuhao=$this->sConn->createCommand($sql_fuhao)->queryRow();
  872. $val['fuhao']=0;
  873. if($student_fuhao){
  874. if($val['student_id']!=$student_fuhao['master_student_id']){
  875. continue;
  876. }else{
  877. $fuhao_arr=explode(',',$student_fuhao['relation_student_id']);
  878. $val['fuhao']=count($fuhao_arr)-1;
  879. }
  880. }
  881. $studentData[$val['semester_id']][]=$val;
  882. $semester[$val['semester_id']]=$val['semester_name'];
  883. $studentId[]=$val['student_id'];
  884. }
  885. //查询business
  886. $criteria = new CDbCriteria();
  887. $b_student = array();
  888. $criteria->addInCondition('student_id',$studentId);
  889. $b_student_data = BusinessStudent::model()->findAll($criteria);
  890. if($b_student_data){
  891. foreach($b_student_data as $v)
  892. {
  893. $businessData[$v->student_id]=array(
  894. 'username'=>$v->username,
  895. 'student_card'=>$v->student_card,
  896. 'school_student_card'=>$v->school_student_card,
  897. 'zhixue_student_card'=>$v->zhixue_student_card,
  898. );
  899. }
  900. }
  901. $i=1;
  902. $currSemester = $this->schoolManager->getCurrSemester();
  903. $smid=$currSemester['semester_id'];
  904. foreach ($semester as $key=> $val){
  905. $semesterStatus='非当前学期';
  906. if($smid==$key){
  907. $semesterStatus='当前学期';
  908. }
  909. $html.='<div class="handel-term-list">';
  910. $html.='<div class="term-name fw"></div>';
  911. $html.='<div class="relative flex">';
  912. $html.='<img src="/images/prev-arrow.png" alt="" class="prev-arrow arrow-img pointer">';
  913. $html.='<div class="term-card-layout flex-one" >';
  914. $html.='<ul class="term-card-detail">';
  915. if(isset($studentData[$key])){
  916. // debug($studentData[$key]);
  917. foreach ($studentData[$key] as $item){
  918. $isZj='是';
  919. if($item['is_outer']==1){
  920. $isZj='否';
  921. }
  922. $html.='<li class="term-card-list" id="'.$item['student_id'].'" draggable="true" >';
  923. $html.='<div class="padding-layout">';
  924. $html.='<div class="card-lis-data flex">';
  925. $html.='<div class="card_select">';
  926. $html.='<select class="nToId"><option value="0">请选择序号</option>';
  927. foreach ($numberToId as $number=>$Id){
  928. $html.='<option value="'.$Id.'">'.$number.'</option>';
  929. }
  930. $html.='</select>';
  931. $html.='</div>';
  932. $html.='</div>';
  933. $html.='<div class="card-lis-data flex">';
  934. $html.='<span class="card-list-menu gray-font">姓名</span>';
  935. $html.='<span class="card-list-msg flex-one">'.$item['realname'].'</span>';
  936. $html.='</div>';
  937. $html.='<div class="card-lis-data flex">';
  938. $html.='<span class="card-list-menu gray-font">班级</span>';
  939. $html.='<span class="card-list-msg flex-one">'.$item['class_name'].'</span>';
  940. $html.='</div>';
  941. $html.='<div class="hide-list">';
  942. $html.='<div class="card-lis-data flex">';
  943. $html.='<span class="card-list-menu gray-font">学期</span>';
  944. $html.='<span class="card-list-msg flex-one">'.$val.'</span>';
  945. $html.='</div>';
  946. $html.='<div class="card-lis-data flex">';
  947. $html.='<span class="card-list-menu gray-font">学期状态</span>';
  948. $html.='<span class="card-list-msg flex-one">'.$semesterStatus.'</span>';
  949. $html.='</div>';
  950. $html.='<div class="card-lis-data flex">';
  951. $html.='<span class="card-list-menu gray-font">班级序号</span>';
  952. $html.='<span class="card-list-msg flex-one">'.$item['serial_number'].'</span>';
  953. $html.='</div>';
  954. $html.='<div class="card-lis-data flex">';
  955. $html.='<span class="card-list-menu gray-font">学号</span>';
  956. $html.='<span class="card-list-msg flex-one">'.$item['userno'].'</span>';
  957. $html.='</div>';
  958. $html.='<div class="card-lis-data flex">';
  959. $html.='<span class="card-list-menu gray-font">登录账号</span>';
  960. if(isset($businessData[$item['student_id']])){
  961. $html.='<span class="card-list-msg flex-one">'.$businessData[$item['student_id']]['username'].'</span>';
  962. }else{
  963. $html.='<span class="card-list-msg flex-one"></span>';
  964. }
  965. $html.='</div>';
  966. $html.='<div class="card-lis-data flex">';
  967. $html.='<span class="card-list-menu gray-font">系统准考证号</span>';
  968. if(isset($businessData[$item['student_id']])){
  969. $html.='<span class="card-list-msg flex-one">'.$businessData[$item['student_id']]['student_card'].'</span>';
  970. }else{
  971. $html.='<span class="card-list-msg flex-one"></span>';
  972. }
  973. $html.='</div>';
  974. $html.='<div class="card-lis-data flex">';
  975. $html.='<span class="card-list-menu gray-font">学校准考证号</span>';
  976. if(isset($businessData[$item['student_id']])){
  977. $html.='<span class="card-list-msg flex-one">'.$businessData[$item['student_id']]['school_student_card'].'</span>';
  978. }else{
  979. $html.='<span class="card-list-msg flex-one"></span>';
  980. }
  981. $html.='</div>';
  982. $html.='<div class="card-lis-data flex">';
  983. $html.='<span class="card-list-menu gray-font">智学网考号</span>';
  984. if(isset($businessData[$item['student_id']])){
  985. $html.='<span class="card-list-msg flex-one">'.$businessData[$item['student_id']]['zhixue_student_card'].'</span>';
  986. }else{
  987. $html.='<span class="card-list-msg flex-one"></span>';
  988. }
  989. $html.='</div>';
  990. $html.='<div class="card-lis-data flex">';
  991. $html.='<span class="card-list-menu gray-font">身份证号</span>';
  992. $html.='<span class="card-list-msg flex-one">'.$item['id_number'].'</span>';
  993. $html.='</div>';
  994. $html.='<div class="card-lis-data flex">';
  995. $html.='<span class="card-list-menu gray-font">是否为在籍生</span>';
  996. $html.='<span class="card-list-msg flex-one">'.$isZj.'</span>';
  997. $html.='</div>';
  998. $html.='<div class="card-lis-data flex">';
  999. $html.='<span class="card-list-menu gray-font">历史考试成绩</span>';
  1000. $html.='<span rel="'.$item['student_id'].'" class="card-list-msg flex-one browse-history green-font">查看</span>';
  1001. $html.='</div>';
  1002. $html.='<div class="card-lis-data flex">';
  1003. $html.='<span class="card-list-menu gray-font">副号</span>';
  1004. $html.='<span rel="'.$item['student_id'].'" class="card-list-msg flex-one green-font pointer fuhao-lock">'.$item['fuhao'].'</span>';
  1005. $html.='</div>';
  1006. $html.='</div>';
  1007. $html.='<span class="toggle-opt-btn green-font pointer">展开</span>';
  1008. $html.='</div>';
  1009. $html.='</li>';
  1010. $i++;
  1011. }
  1012. }
  1013. $html.='</ul>';
  1014. $html.='</div>';
  1015. $html.='<img src="/images/next-arrow.png" alt="" class="next-arrow arrow-img pointer">';
  1016. $html.='</div>';
  1017. $html.='</div>';
  1018. }
  1019. }
  1020. }
  1021. $result['html']=$html;
  1022. $result['excel_html']=$excel_html;
  1023. $result['status']=1;
  1024. exit(json_encode($result));
  1025. }
  1026. //导入学生执行
  1027. public function actionConfirm_import(){
  1028. $result['status']=0;
  1029. $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();
  1030. if(!$all_student){
  1031. // Yii::app()->jump->error('没有可导入的数据! ');
  1032. $result['msg']='没有可导入的数据!';
  1033. exit(json_encode($result));
  1034. }
  1035. $grade = SGrade::model()->findAll(array('order'=>'id asc'));
  1036. $cardGrade=array();
  1037. $cardLengthGrade=array();
  1038. foreach ($grade as $val){
  1039. $cardGrade[$val->id]=$val->card_status;
  1040. $cardLengthGrade[$val->id]=$val->card_length;
  1041. }
  1042. $updateBusiness = array();
  1043. $updateStudentInfo = array();
  1044. $updateStudentClassRelation = array();
  1045. $insertStudentClassRelation = array();
  1046. $school=$this->schoolManager->getSchoolInfo();
  1047. $semesterId = $this->semesterId;
  1048. $class_semester_data = ClassModel::model()->findAll('semester_id=:semester_id',array(':semester_id'=>$semesterId));
  1049. $classGrade=array();
  1050. if($class_semester_data)
  1051. {
  1052. foreach($class_semester_data as $v)
  1053. {
  1054. $class_ids[$v->class_id] = $v->class_id;
  1055. $classGrade[$v->class_id] = $v->grade;
  1056. }
  1057. }
  1058. unset($class_semester_data);
  1059. if(!$class_ids)
  1060. {
  1061. //Yii::app()->jump->error('无班级信息,导入失败');
  1062. $result['msg']='无班级信息,导入失败!';
  1063. exit(json_encode($result));
  1064. }
  1065. $studentInClass=array();
  1066. $getClassByStudent=array();
  1067. $class_serial_number = array();
  1068. // 获取在线学生
  1069. $criteria = new CDbCriteria();
  1070. $criteria->addInCondition('class_id',$class_ids);
  1071. $criteria->addCondition('status=0');
  1072. $criteria->addCondition('class_type=1');
  1073. $semester_student_class_data = SStudentClassRelation::model()->findAll($criteria);
  1074. if($semester_student_class_data)
  1075. {
  1076. foreach($semester_student_class_data as $v)
  1077. {
  1078. //在线学生ID
  1079. $s_student_ids[(string)$v->student_id] = $v->student_id;
  1080. //获取班级中的最大序号
  1081. if(isset($class_serial_number[(string)$v->class_id]))
  1082. {
  1083. if($class_serial_number[(string)$v->class_id]<$v->serial_number)
  1084. {
  1085. $class_serial_number[(string)$v->class_id] = $v->serial_number;
  1086. }
  1087. }else
  1088. {
  1089. $class_serial_number[(string)$v->class_id] = $v->serial_number;
  1090. }
  1091. $studentInClass[(string)$v->class_id][]=(string)$v->student_id;
  1092. $getClassByStudent[(string)$v->student_id]=(string)$v->class_id;
  1093. }
  1094. }
  1095. $needCreateStudent = array();
  1096. foreach($all_student as $val){
  1097. $extend=array();
  1098. if($val['extend']){
  1099. $extend=json_decode($val['extend'],true);
  1100. //检验扩展信息
  1101. $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';
  1102. $preg_phone='/^1[34578]\d{9}$/ims';
  1103. foreach ($extend as $key=>$item){
  1104. if(!$item && $key!='is_outer'){
  1105. unset($extend[$key]);
  1106. continue;
  1107. }
  1108. if($key=='school_student_card'){
  1109. // if($cardGrade[$classGrade[$val['class_id']]]==0 || $cardLengthGrade[$classGrade[$val['class_id']]]!=mb_strlen($item)){
  1110. // unset($extend[$key]);
  1111. // continue;
  1112. // }
  1113. $students=$this->schoolManager->getStudentIdBySchoolCard($item,$this->schoolId);
  1114. if($students){
  1115. foreach ($students as $sid){
  1116. if(isset($s_student_ids[(string)$sid['student_id']]) && strcmp((string)$sid['student_id'],$val['student_id'])!=0){
  1117. $result['msg']='学校准考证号('.$item.')系统中已存在,导入失败!';
  1118. exit(json_encode($result));
  1119. }
  1120. }
  1121. }
  1122. }elseif($key=='id_number'){
  1123. unset($extend[$key]);
  1124. continue;
  1125. }elseif($key=='student_phone'){
  1126. if(!preg_match($preg_phone,$item)){
  1127. unset($extend[$key]);
  1128. continue;
  1129. }
  1130. }
  1131. }
  1132. }
  1133. $val=array_merge($val,$extend);
  1134. //处理扩展数据
  1135. $school_student_card='';
  1136. $zhixue_student_card='';
  1137. $userno='';
  1138. if(isset($val['school_student_card'])){
  1139. $school_student_card=$val['school_student_card'];
  1140. unset($extend['school_student_card']);
  1141. }
  1142. if(isset($val['zhixue_student_card'])){
  1143. $zhixue_student_card=$val['zhixue_student_card'];
  1144. unset($extend['zhixue_student_card']);
  1145. }
  1146. if(isset($val['userno'])){
  1147. $userno=$val['userno'];
  1148. unset($extend['userno']);
  1149. }
  1150. if($val['student_id']){
  1151. //转班
  1152. $studentInfo = $this->schoolManager->getStudentByStudentIds(array($val['student_id']));
  1153. if(!$studentInfo){
  1154. // Yii::app()->jump->error('数据异常! ');
  1155. $result['msg']='数据异常,导入失败!';
  1156. exit(json_encode($result));
  1157. }
  1158. $studentInfo=$studentInfo[0];
  1159. $oldClass=isset($getClassByStudent[$val['student_id']])?$getClassByStudent[$val['student_id']]:0;
  1160. //读取当前班级
  1161. if(!isset($studentInClass[$val['class_id']]) || !inArray($val['student_id'],$studentInClass[$val['class_id']])){
  1162. //班级不同,转班,补充扩展信息
  1163. if($oldClass){
  1164. $updateStudentClassRelation[$oldClass]['status'][$val['student_id']]=1;
  1165. $updateStudentClassRelation[$oldClass]['operation'][$val['student_id']]=2;
  1166. }
  1167. if(!isset($class_serial_number[$val['class_id']])){
  1168. $class_serial_number[$val['class_id']]=0;
  1169. }
  1170. $insertStudentClassRelation[$val['student_id']]="('".$val['class_id']."','".$val['student_id']."','".($class_serial_number[$val['class_id']]+1)."','".$userno."','".time()."',0)";
  1171. $class_serial_number[$val['class_id']]++;
  1172. $updateStudentInfo['class_id'][$val['student_id']]="'".$val['class_id']."'";
  1173. }else{
  1174. //验证是否已删除
  1175. if(!isset($s_student_ids[(string)$val['student_id']])){
  1176. if(!isset($class_serial_number[$val['class_id']])){
  1177. $class_serial_number[$val['class_id']]=0;
  1178. }
  1179. $insertStudentClassRelation[$val['student_id']]="('".$val['class_id']."','".$val['student_id']."','".($class_serial_number[$val['class_id']]+1)."','".$userno."','".time()."',0)";
  1180. $class_serial_number[$val['class_id']]++;
  1181. $updateStudentInfo['class_id'][$val['student_id']]="'".$val['class_id']."'";
  1182. }
  1183. if($oldClass){
  1184. if($userno){
  1185. $updateStudentClassRelation[$oldClass]['userno'][$val['student_id']]=$userno;
  1186. }
  1187. }
  1188. }
  1189. if(isset($val['is_outer'])){
  1190. $updateStudentInfo['is_outer'][$val['student_id']]="'".$val['is_outer']."'";
  1191. }
  1192. //更新business
  1193. if($school_student_card || $zhixue_student_card){
  1194. if($school_student_card){
  1195. $updateBusiness['school_student_card'][$val['student_id']]=$school_student_card;
  1196. }
  1197. if($zhixue_student_card){
  1198. $updateBusiness['zhixue_student_card'][$val['student_id']]=$zhixue_student_card;
  1199. }
  1200. }
  1201. //更新student_info
  1202. $updateStudentInfoSql=array();
  1203. if($extend){
  1204. foreach ($extend as $field=>$value){
  1205. if($value){
  1206. $updateStudentInfo[$field][$val['student_id']]="'".$value."'";
  1207. }
  1208. }
  1209. }
  1210. if($val['id_number'] && !$this->schoolManager->checkStudentIdNumber($val['id_number'])){
  1211. $updateStudentInfo['id_number'][$val['student_id']]="'".$val['id_number']."'";
  1212. }
  1213. }else{
  1214. //新学生,创建
  1215. $needCreateStudent[]=$val;
  1216. }
  1217. }
  1218. $student_sql=array();
  1219. $student_info_sql=array();
  1220. $class_student_sql=array();
  1221. //创建学生
  1222. if($needCreateStudent){
  1223. $create_card_data = $this->createCard(count($needCreateStudent));
  1224. $studentIdArr=getBatchUuid($this->schoolId,count($needCreateStudent));
  1225. foreach ($needCreateStudent as $key => $val){
  1226. $student_cart = 0;
  1227. foreach ($create_card_data as $v) {
  1228. if ($v['use'] == 0) {
  1229. $student_cart = $v['student_card'];
  1230. $create_card_data[$student_cart]['use'] = 1;
  1231. break;
  1232. }
  1233. }
  1234. //$studentId = getUUID();
  1235. $studentId=current($studentIdArr);
  1236. unset($studentIdArr[$studentId]);
  1237. $student_cart = (int)$student_cart;
  1238. $password = $student_cart;
  1239. $school_student_card='';
  1240. $zhixue_student_card='';
  1241. $userno='';
  1242. $id_number='';
  1243. $student_phone=0;
  1244. $field_1='';
  1245. $field_2='';
  1246. $field_3='';
  1247. $field_4='';
  1248. $field_5='';
  1249. if(isset($val['school_student_card']) && !empty($val['school_student_card'])){
  1250. $school_student_card=$val['school_student_card'];
  1251. }
  1252. if(isset($val['zhixue_student_card']) && !empty($val['zhixue_student_card'])){
  1253. $zhixue_student_card=$val['zhixue_student_card'];
  1254. }
  1255. if(isset($val['userno']) && !empty($val['userno'])){
  1256. $userno=$val['userno'];
  1257. }
  1258. if(isset($val['id_number']) && !empty($val['id_number']) && !$this->schoolManager->checkStudentIdNumber($val['id_number'])){
  1259. $id_number=$val['id_number'];
  1260. }
  1261. if(isset($val['student_phone']) && !empty($val['student_phone'])){
  1262. $student_phone=$val['student_phone'];
  1263. }
  1264. if(isset($val['field_1']) && !empty($val['field_1'])){
  1265. $field_1=$val['field_1'];
  1266. }
  1267. if(isset($val['field_2']) && !empty($val['field_2'])){
  1268. $field_2=$val['field_2'];
  1269. }
  1270. if(isset($val['field_3']) && !empty($val['field_3'])){
  1271. $field_3=$val['field_3'];
  1272. }
  1273. if(isset($val['field_4']) && !empty($val['field_4'])){
  1274. $field_4=$val['field_4'];
  1275. }
  1276. if(isset($val['field_5']) && !empty($val['field_5'])){
  1277. $field_5=$val['field_5'];
  1278. }
  1279. // 将学生登录账号插入主数据
  1280. if(!isset($class_serial_number[$val['class_id']])){
  1281. $class_serial_number[$val['class_id']]=0;
  1282. }
  1283. $is_outer=0;
  1284. if(isset($val['is_outer'])){
  1285. $is_outer=$val['is_outer'];
  1286. }
  1287. $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().")";
  1288. $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."')";
  1289. $class_student_sql[$studentId] = "(".$val['class_id'].",".$studentId.",".($class_serial_number[$val['class_id']] +$key+ 1).",'".$userno."',".time().",0)";
  1290. }
  1291. }
  1292. //执行sql
  1293. $transaction_conn = $this->conn->beginTransaction();
  1294. try {
  1295. if($student_sql){
  1296. $sql="insert into student (`student_id`,`username`,`student_card`,`school_student_card`,`zhixue_student_card`,`password`,`school_id`,`register_time`) values ";
  1297. $sql.=implode(',',$student_sql);
  1298. $this->conn->createCommand($sql)->execute();
  1299. }
  1300. if($updateBusiness){
  1301. $businessUpdateSql="update student set ";
  1302. $businessUpdateSetArr=array();
  1303. $studentId=array();
  1304. foreach ($updateBusiness as $field =>$val){
  1305. $businessUpdateSetStr=" `".$field."`= case student_id ";
  1306. foreach ($val as $id=>$value){
  1307. $businessUpdateSetStr.=" WHEN {$id} THEN '{$value}' ";
  1308. $studentId[]=$id;
  1309. }
  1310. $businessUpdateSetStr.="ELSE `{$field}`";
  1311. $businessUpdateSetStr.=" END ";
  1312. $businessUpdateSetArr[]=$businessUpdateSetStr;
  1313. }
  1314. if($businessUpdateSetArr){
  1315. $businessUpdateSql.=implode(',',$businessUpdateSetArr)." where student_id in (".implode(',',$studentId).") and school_id = '".$this->schoolId."' ";
  1316. $this->conn->createCommand($businessUpdateSql)->execute();
  1317. }
  1318. }
  1319. $businessRs=true;
  1320. }catch (Exception $ec) {
  1321. $transaction_conn->rollback();
  1322. //Yii::app()->jump->error('导入失败');
  1323. exit(json_encode($result));
  1324. }
  1325. if($businessRs){
  1326. $transaction = $this->sConn->beginTransaction();
  1327. try {
  1328. if($updateStudentInfo){
  1329. $studentInfoUpdateSql="update student_info set ";
  1330. $studentInfoUpdateSetArr=array();
  1331. $studentId=array();
  1332. foreach ($updateStudentInfo as $field =>$val){
  1333. $studentInfoUpdateSetStr=" `".$field."`= case student_id ";
  1334. foreach ($val as $id=>$value){
  1335. $studentInfoUpdateSetStr.=" WHEN {$id} THEN {$value} ";
  1336. $studentId[]=$id;
  1337. }
  1338. $studentInfoUpdateSetStr.="ELSE `{$field}`";
  1339. $studentInfoUpdateSetStr.=" END ";
  1340. $studentInfoUpdateSetArr[]=$studentInfoUpdateSetStr;
  1341. }
  1342. if($studentInfoUpdateSetArr){
  1343. $studentInfoUpdateSql.=implode(',',$studentInfoUpdateSetArr)." where student_id in (".implode(',',$studentId).") ";
  1344. $this->sConn->createCommand($studentInfoUpdateSql)->execute();
  1345. }
  1346. }
  1347. if($updateStudentClassRelation){
  1348. foreach ($updateStudentClassRelation as $class_id =>$updateValue){
  1349. $studentClassUpdateSql="update student_class_relation set ";
  1350. $studentClassUpdateSetArr=array();
  1351. foreach($updateValue as $field =>$val){
  1352. $studentClassUpdateSetStr=" `".$field."`= case student_id ";
  1353. foreach ($val as $id=>$value){
  1354. $studentClassUpdateSetStr.=" WHEN {$id} THEN '{$value}' ";
  1355. }
  1356. $studentClassUpdateSetStr.="ELSE `{$field}`";
  1357. $studentClassUpdateSetStr.=" END ";
  1358. $studentClassUpdateSetArr[]=$studentClassUpdateSetStr;
  1359. }
  1360. if($studentClassUpdateSetArr){
  1361. $studentClassUpdateSql.=implode(',',$studentClassUpdateSetArr)." where class_id ='".$class_id."' ";
  1362. $this->sConn->createCommand($studentClassUpdateSql)->execute();
  1363. }
  1364. }
  1365. }
  1366. if($insertStudentClassRelation){
  1367. $class_sql="insert into student_class_relation(`class_id`,`student_id`,`serial_number`,`userno`,`update_time`,`operation`) values";
  1368. $class_sql.=implode(',',$insertStudentClassRelation);
  1369. $this->sConn->createCommand($class_sql)->execute();
  1370. }
  1371. if($student_info_sql){
  1372. $s_sql = 'INSERT INTO `student_info`
  1373. (`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';
  1374. $s_sql.=implode(',',$student_info_sql);
  1375. $this->sConn->createCommand($s_sql)->execute();
  1376. }
  1377. if($class_student_sql){
  1378. $class_sql="insert into student_class_relation(`class_id`,`student_id`,`serial_number`,`userno`,`update_time`,`operation`) values ";
  1379. $class_sql.=implode(',',$class_student_sql);
  1380. $this->sConn->createCommand($class_sql)->execute();
  1381. }
  1382. //$delSql="truncate table import_student_temp;";
  1383. $delSql="delete from import_student_temp;";
  1384. $this->sConn->createCommand($delSql)->execute();
  1385. $transaction_conn->commit();
  1386. $transaction->commit();
  1387. if(Yii::app()->params['handle_log_on_off'])
  1388. {
  1389. writeFileLog(jsonEncode(array(
  1390. "exam_group_id" => 0,
  1391. "operate_project" => 'zsyas2',
  1392. "school_id" => $this->schoolId,
  1393. "title" => '导入学生',
  1394. "operate_account" => Yii::app()->session['coachInfo']['coach_name'],
  1395. "operate_method" => $this->action,
  1396. "operate_url" => $this->getRoute(),
  1397. "operate_sql" =>'',
  1398. "operate_param" =>'',
  1399. "date"=>date('Y-m-d H:i:j')
  1400. )));
  1401. }
  1402. $result['status']=1;
  1403. }catch (Exception $e) {
  1404. $transaction->rollback();
  1405. $transaction_conn->rollback();
  1406. //Yii::app()->jump->error('导入失败');
  1407. exit(json_encode($result));
  1408. }
  1409. }
  1410. unset($transaction);
  1411. unset($transaction_conn);
  1412. // 导入成功
  1413. //$this->redirect(Yii::app()->createUrl('importstudent/index'));
  1414. exit(json_encode($result));
  1415. }
  1416. //预生成系统准考证号
  1417. private function createCard($num)
  1418. {
  1419. $cards = array();
  1420. $student_cart = 0;
  1421. if(!$num)
  1422. {
  1423. return $cards;
  1424. }
  1425. /*
  1426. $b_student_data = BusinessStudent::model()->find(array(
  1427. 'select' => 'student_card',
  1428. 'order' => 'student_card desc',
  1429. 'condition' => 'school_id=:school_id',
  1430. 'limit' => 1,
  1431. 'params' => array(':school_id'=>$this->schoolId)
  1432. ));
  1433. if($b_student_data)
  1434. {
  1435. if(!$b_student_data->student_card)
  1436. {
  1437. if($this->schoolId<100)
  1438. {
  1439. $student_cart = '9'.$this->schoolId.'00100';
  1440. }else
  1441. {
  1442. $student_cart = $this->schoolId.'00100';
  1443. }
  1444. }else
  1445. {
  1446. $student_cart = $b_student_data->student_card;
  1447. }
  1448. }else
  1449. {
  1450. if($this->schoolId<100)
  1451. {
  1452. $student_cart = '9'.$this->schoolId.'00100';
  1453. }else
  1454. {
  1455. $student_cart = $this->schoolId.'00100';
  1456. }
  1457. }
  1458. */
  1459. $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();
  1460. if($b_student_data)
  1461. {
  1462. if(!$b_student_data['student_card'])
  1463. {
  1464. if($this->schoolId<100)
  1465. {
  1466. $student_cart = '9'.$this->schoolId.'00100';
  1467. }else
  1468. {
  1469. $student_cart = $this->schoolId.'00100';
  1470. }
  1471. }else
  1472. {
  1473. $student_cart = $b_student_data['student_card'];
  1474. }
  1475. }else
  1476. {
  1477. if($this->schoolId<100)
  1478. {
  1479. $student_cart = '9'.$this->schoolId.'00100';
  1480. }else
  1481. {
  1482. $student_cart = $this->schoolId.'00100';
  1483. }
  1484. }
  1485. unset($b_student_data);
  1486. for($i=1;$i<=$num;$i++)
  1487. {
  1488. $cards[$student_cart+$i] = array(
  1489. 'student_card'=>$student_cart+$i,
  1490. 'use'=>0,
  1491. 'order'=>$i
  1492. );
  1493. }
  1494. return $cards;
  1495. }
  1496. //重名处理后检查
  1497. public function actionRepeat_check(){
  1498. $str=Req::post('str');
  1499. $name=Req::post('name');
  1500. $result['status']=0;
  1501. $studentIds=array();
  1502. $tempIds=array();
  1503. if($str){
  1504. foreach($str as $val){
  1505. @$nToId=explode('///',$val);
  1506. if(isset($nToId[0])){
  1507. if(in_array((string)$nToId[0],$studentIds,true)){
  1508. $result['msg']='不能学生重复选择';
  1509. exit(json_encode($result));
  1510. }else{
  1511. $studentIds[]=(string)$nToId[0];
  1512. }
  1513. }
  1514. if(isset($nToId[1])){
  1515. if(in_array((string)$nToId[1],$tempIds,true)){
  1516. $result['msg']='不能学生重复选择';
  1517. exit(json_encode($result));
  1518. }else{
  1519. $tempIds[]=(string)$nToId[1];
  1520. }
  1521. }
  1522. $nToId=null;
  1523. }
  1524. $updateSql=array();
  1525. foreach($str as $val){
  1526. @$nToId=explode('///',$val);
  1527. if(isset($nToId[0]) && isset($nToId[1])){
  1528. //判断学生id是否已关联过
  1529. $related=$this->sConn->createCommand("select modify_name from import_student_temp where student_id='".$nToId[0]."' limit 1")->queryRow();
  1530. if($related){
  1531. $result['msg']='学生已经被关联,请重新检查:'.$related['modify_name'];
  1532. exit(json_encode($result));
  1533. }
  1534. $updateSql[]="update import_student_temp set student_id='".$nToId[0]."',is_repeat=0,is_normal=1 where id='".$nToId[1]."'";
  1535. }
  1536. }
  1537. if($updateSql){
  1538. $transaction = $this->sConn->beginTransaction();
  1539. try {
  1540. foreach ($updateSql as $query) {
  1541. $this->sConn->createCommand($query)->execute();
  1542. }
  1543. $transaction->commit();
  1544. }catch (Exception $e) {
  1545. $transaction->rollback();
  1546. $result['msg']='关联失败';
  1547. exit(json_encode($result));
  1548. }
  1549. }
  1550. }
  1551. unset($transaction);
  1552. $updateIdNumberSql=array();
  1553. $sql="select id,id_number from import_student_temp where modify_name='".$name."' and is_repeat=1";
  1554. $excel_data = $this->sConn->createCommand($sql)->queryAll();
  1555. if($excel_data){
  1556. foreach ($excel_data as $val){
  1557. if($val['id_number']){
  1558. $updateIdNumberSql[]="update import_student_temp set is_repeat=0,is_normal=1 where id='".$val['id']."'";
  1559. }
  1560. }
  1561. }
  1562. if($updateIdNumberSql){
  1563. $transaction = $this->sConn->beginTransaction();
  1564. try {
  1565. foreach($updateIdNumberSql as $query){
  1566. $this->sConn->createCommand($query)->execute();
  1567. }
  1568. $transaction->commit();
  1569. }catch (Exception $e) {
  1570. $transaction->rollback();
  1571. $result['msg']='关联失败';
  1572. exit(json_encode($result));
  1573. }
  1574. }
  1575. $result['status']=1;
  1576. exit(json_encode($result));
  1577. }
  1578. //正常数据列表
  1579. public function actionNormal(){
  1580. $condition = array();
  1581. $class_id = safe_replace(Yii::app()->request->getQuery('classId'));
  1582. $realname = safe_replace(Yii::app()->request->getQuery('realname'));
  1583. $grade = safe_replace(Yii::app()->request->getQuery('grade'));
  1584. if($class_id){
  1585. $condition[]=" class_id='".$class_id."'";
  1586. }
  1587. if($realname){
  1588. $condition[]=" modify_name like '%".$realname."%'";
  1589. }
  1590. $condition[]="is_normal=1";
  1591. $rs=$this->schoolManager->getNormalStudent($condition,20);
  1592. $fileName=array(
  1593. 'userno'=>'学号',
  1594. 'school_student_card'=>'学校准考证号',
  1595. 'zhixue_student_card'=>'智学网准考证号',
  1596. 'id_number'=>'身份证号',
  1597. 'student_phone'=>'手机号',
  1598. 'is_outer'=>'在籍生'
  1599. );
  1600. //扩展字段
  1601. $studentExtend=$this->schoolManager->getStudentExtend();
  1602. if($studentExtend){
  1603. foreach ($studentExtend as $key=> $value){
  1604. $fileName[$key]=$value['field_mean'];
  1605. }
  1606. }
  1607. $data['pages'] = $rs['pager'];
  1608. $data['page_total'] = $rs['pager']->rowsCount;
  1609. $data['list']=$rs['rs'];
  1610. $data['class_id']=$class_id;
  1611. $data['grade']=$grade;
  1612. $data['realname']=$realname;
  1613. $extendTh='';
  1614. if($data['list']){
  1615. foreach ($data['list'] as $key=>$val){
  1616. $extendTd='';
  1617. $json=json_decode($val['extend'],true);
  1618. unset($json['id_number']);
  1619. if(!$extendTh){
  1620. foreach ($json as $k=>$v){
  1621. $extendTh.='<th>'.$fileName[$k].'</th>';
  1622. }
  1623. }
  1624. foreach ($json as $k=>$v){
  1625. if($k=='is_outer'){
  1626. if($v){
  1627. $v='否';
  1628. }else{
  1629. $v='是';
  1630. }
  1631. }
  1632. $extendTd.='<td>'.$v.'</td>';
  1633. }
  1634. $data['list'][$key]['ext']=$extendTd;
  1635. }
  1636. }
  1637. $data['extTh']=$extendTh;
  1638. $this->render('normal',$data);
  1639. }
  1640. //根据班级读取学生
  1641. public function actionLoadStudentByClassName(){
  1642. $realname = safe_replace(Req::post('classname'));
  1643. $result['status']=0;
  1644. if(!$realname){
  1645. exit(json_encode($result));
  1646. }
  1647. $fileName=array(
  1648. 'userno'=>'学号',
  1649. 'school_student_card'=>'学校准考证号',
  1650. 'zhixue_student_card'=>'智学网准考证号',
  1651. 'id_number'=>'身份证号',
  1652. 'student_phone'=>'手机号',
  1653. 'is_outer'=>'在籍生'
  1654. );
  1655. //扩展字段
  1656. $studentExtend=$this->schoolManager->getStudentExtend();
  1657. if($studentExtend){
  1658. foreach ($studentExtend as $key=> $value){
  1659. $fileName[$key]=$value['field_mean'];
  1660. }
  1661. }
  1662. $sex[1]='男';
  1663. $sex[2]='女';
  1664. $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();
  1665. $extendTh='';
  1666. $html='<tr>';
  1667. $html.="<td>姓名</td>";
  1668. $html.="<td>性别</td>";
  1669. $html.="<td>身份证</td>";
  1670. if($all_student){
  1671. if($all_student){
  1672. foreach ($all_student as $key=>$val){
  1673. $extendTd='<tr>';
  1674. $extendTd.="<td>{$val['modify_name']}(原{$val['student_name']})</td>";
  1675. $extendTd.="<td>{$sex[$val['sex']]}</td>";
  1676. $extendTd.="<td>{$val['id_number']}</td>";
  1677. $json=json_decode($val['extend'],true);
  1678. unset($json['id_number']);
  1679. if(!$extendTh){
  1680. foreach ($json as $k=>$v){
  1681. $extendTh.='<td>'.$fileName[$k].'</td>';
  1682. }
  1683. $html.=$extendTh.'</tr>';
  1684. }
  1685. foreach ($json as $k=>$v){
  1686. $extendTd.='<td>'.$v.'</td>';
  1687. }
  1688. $extendTd.="</tr>";
  1689. $html.=$extendTd;
  1690. }
  1691. $html.="";
  1692. }
  1693. }
  1694. $result['html']=$html;
  1695. $result['status']=1;
  1696. exit(json_encode($result));
  1697. }
  1698. private function getInClassStudents($class_ids)
  1699. {
  1700. $data = array();
  1701. $student_ids = array();
  1702. $student_names = array();
  1703. $student_cards = array();
  1704. $school_student_cards = array();
  1705. $userno = array();
  1706. $serial_number = array();
  1707. $student_class_ids = array();
  1708. $student_info = array();
  1709. $class_student = array();
  1710. $criteria = new CDbCriteria();
  1711. $criteria->addInCondition('class_id',$class_ids);
  1712. $criteria->addCondition('status=:status');
  1713. $criteria->params[':status'] = 0;
  1714. $criteria->order = 'serial_number desc,class_id desc';
  1715. $student_class_data = SStudentClassRelation::model()->findAll($criteria);
  1716. if($student_class_data)
  1717. {
  1718. foreach($student_class_data as $key=>$v)
  1719. {
  1720. if(isset($serial_number[$v->class_id]))
  1721. {
  1722. if($serial_number[$v->class_id]<$v->serial_number)
  1723. {
  1724. $serial_number[$v->class_id] = $v->serial_number;
  1725. }
  1726. }else
  1727. {
  1728. $serial_number[$v->class_id] = $v->serial_number;
  1729. }
  1730. $student_ids[$v->student_id] = $v->student_id;
  1731. $userno[$v->student_id] = $v->userno;
  1732. $class_student[$v->student_id] = $v;
  1733. $student_class_ids[$v->student_id] = $v->class_id;
  1734. }
  1735. $criteria = new CDbCriteria();
  1736. $criteria->addInCondition('student_id',$student_ids);
  1737. $student_data = SStudentInfo::model()->findAll($criteria);
  1738. if($student_data)
  1739. {
  1740. foreach($student_data as $v)
  1741. {
  1742. $student_names[$v->student_id] = $v->realname;
  1743. $student_info[$v->student_id] = $v;
  1744. }
  1745. }
  1746. unset($student_data);
  1747. unset($criteria);
  1748. $criteria = new CDbCriteria();
  1749. $criteria->addInCondition('student_id',$student_ids);
  1750. $b_student_data = BusinessStudent::model()->findAll($criteria);
  1751. if($b_student_data)
  1752. {
  1753. foreach($b_student_data as $v)
  1754. {
  1755. $student_cards[$v->student_id] = $v->student_card;
  1756. if($v->school_student_card)
  1757. {
  1758. $school_student_cards[$v->student_id] = $v->school_student_card;
  1759. }
  1760. }
  1761. }
  1762. unset($b_student_data);
  1763. $data['student_names'] = $student_names;
  1764. $data['student_cards'] = $student_cards;
  1765. $data['student_ids'] = $student_ids;
  1766. $data['school_student_cards'] = $school_student_cards;
  1767. $data['student_userno'] = $userno;
  1768. $data['student_info'] = $student_info;
  1769. $data['class_student'] = $class_student;
  1770. $data['student_class_ids'] = $student_class_ids;
  1771. $data['serial_number'] = $serial_number;
  1772. }
  1773. return $data;
  1774. }
  1775. //教学班导入学生
  1776. public function actionImportStudentClassified(){
  1777. $this->sConn->createCommand("DELETE FROM `import_student_temp` where import_type=1 ")->execute();
  1778. $student_all_data = $this->sConn->createCommand("SELECT id FROM `import_student_temp` limit 1 ")->queryRow();
  1779. if($student_all_data){
  1780. $this->render('check_classified');
  1781. }else{
  1782. //读取科目设置
  1783. $subjects=array();
  1784. $rs=$this->schoolManager->getSubjectByGrade(0,0,$this->semesterId);
  1785. if($rs){
  1786. foreach ($rs as $val){
  1787. $subjects[$val['subject_id']]=Yii::app()->params['subjectId'][$val['subject_id']];
  1788. }
  1789. }
  1790. $data['subjects']=$subjects;
  1791. $this->render('import_classified',$data);
  1792. }
  1793. }
  1794. public function actionImportClassified(){
  1795. ini_set ('memory_limit', '300M');
  1796. if(!$_FILES){
  1797. Yii::app()->jump->error('文件大小超过范围');
  1798. }else {
  1799. if (!isset($_FILES['student_file']) || !isset($_FILES['student_file']['size']) || $_FILES['student_file']['size'] > 5242880) {
  1800. Yii::app()->jump->error('文件大小超过范围');
  1801. }
  1802. }
  1803. $subjectId=Req::post('subject');
  1804. if (Yii::app()->request->getIsPostRequest()) {
  1805. $uploader = new Uploader("upload/tmpDir/UploadClassesStudentsList/{$this->schoolId}/");
  1806. $uploader->allowTypes = array("xls","xlsx");
  1807. $uploader->fieldsMappings = array("exname" => array(0 => $this->schoolId));
  1808. $file = $uploader->act();
  1809. $file = Arr::current(Arr::current($file));
  1810. if (!$file || !isset($file["error"]))
  1811. Yii::app()->jump->error('请上传的Excel!');
  1812. if ($file["error"] != 0) {
  1813. switch ($file["error"]) {
  1814. case 2001:
  1815. Yii::app()->jump->error('文件类型不符');
  1816. break;
  1817. case 2002:
  1818. Yii::app()->jump->error('文件大小超出允许范围');
  1819. break;
  1820. default:
  1821. Yii::app()->jump->error('上传失败');
  1822. break;
  1823. }
  1824. }
  1825. $inputFileName = $file["src"];
  1826. if(Yii::app()->params['handle_log_on_off'])
  1827. {
  1828. writeFileLog(jsonEncode(array(
  1829. "exam_group_id" => 0,
  1830. "operate_project" => 'zsyas2',
  1831. "school_id" => $this->schoolId,
  1832. "title" => '导入教学班学生excel',
  1833. "operate_account" => Yii::app()->session['coachInfo']['coach_name'],
  1834. "operate_method" => $this->action,
  1835. "operate_url" => $this->getRoute(),
  1836. "operate_sql" =>'',
  1837. "operate_param" =>json_encode(array('post'=>$_POST,'get'=>$_GET,'file'=>$inputFileName)),
  1838. "date"=>date('Y-m-d H:i:j')
  1839. )));
  1840. }
  1841. // 导入PHPExcel类
  1842. Yii::import('application.extensions.*');
  1843. require_once('phpexcel/PHPExcel/IOFactory.php');
  1844. try{
  1845. $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
  1846. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
  1847. //FIXME 2019-12-23
  1848. @unlink($file["src"]);
  1849. }catch (Exception $e){
  1850. //FIXME 2019-12-23
  1851. @unlink($file["src"]);
  1852. Yii::app()->jump->error('Excel格式不正确:'.$e->getMessage());
  1853. }
  1854. if(!$sheetData)
  1855. {
  1856. Yii::app()->jump->error('请正确的Excel!');
  1857. }
  1858. unset($objPHPExcel);
  1859. unset($sheetData[1]);
  1860. if(!$sheetData)
  1861. {
  1862. Yii::app()->jump->error('请输入学生数据');
  1863. }
  1864. //获取当前学期的所有班级
  1865. $semesterId = $this->semesterId;
  1866. $class_semester_data = ClassModel::model()->findAll('semester_id=:semester_id and class_type=2',array(':semester_id'=>$semesterId));
  1867. if($class_semester_data)
  1868. {
  1869. foreach($class_semester_data as $v)
  1870. {
  1871. $class_ids[$v->class_id] = $v->class_id;
  1872. $c_class_names[$v->class_id] = (string)$v->class_name;
  1873. }
  1874. }
  1875. unset($class_semester_data);
  1876. if(!$class_ids)
  1877. {
  1878. Yii::app()->jump->error('当前学期无班级信息,导入失败');
  1879. }
  1880. $insertValue="insert into import_student_temp(`student_name`,`class_name`,`student_card`,`import_type`,`subject_id`) values ";
  1881. $values=array();
  1882. $studentCard=array();
  1883. foreach ($sheetData as $val){
  1884. if(!$val['A'] || !$val['B'] ){
  1885. //Yii::app()->jump->error('班级,姓名,性别不能为空');
  1886. continue;
  1887. }
  1888. if($val['C']){
  1889. $studentCard[$val['C']][]=$val['C'];
  1890. }
  1891. $values[]="('".$val['B']."','".$val['A']."','".$val['C']."',2,'".$subjectId."')";
  1892. }
  1893. //检测系统准考证号是否重复
  1894. $errorStudentCard=array();
  1895. if($studentCard){
  1896. foreach ($studentCard as $item){
  1897. if(count($item)>1){
  1898. $errorStudentCard[]=$item[0];
  1899. }
  1900. }
  1901. }
  1902. if($errorStudentCard){
  1903. if(count($errorStudentCard)>3){
  1904. Yii::app()->jump->error('系统准考证号重复: '.implode(',',array_slice($errorStudentCard,0,3)).' ...... ');
  1905. }else{
  1906. Yii::app()->jump->error('系统准考证号重复: '.implode(',',$errorStudentCard));
  1907. }
  1908. }
  1909. if($values){
  1910. $sql=$insertValue.implode(',',$values);
  1911. $transaction = $this->sConn->beginTransaction();
  1912. try{
  1913. $this->sConn->createCommand($sql)->execute();
  1914. $transaction->commit();
  1915. // Yii::app()->jump->error('Excel导入成功!');
  1916. //跳转检测页
  1917. $this->redirect($this->createUrl('importstudent/checkClassified'));
  1918. }catch(Exception $e){
  1919. $transaction->rollBack();
  1920. Yii::app()->jump->error('Excel导入失败!');
  1921. }
  1922. }else{
  1923. Yii::app()->jump->error('Excel导入失败!');
  1924. }
  1925. }
  1926. }
  1927. public function actionCheckClassified(){
  1928. $this->render('check_classified');
  1929. }
  1930. //检测姓名
  1931. public function actionCheck_student_name_classified(){
  1932. ini_set('memory_limit','512M');
  1933. set_time_limit(0);
  1934. $result['status']=0;
  1935. $result['data']=0;
  1936. $id_number=array();
  1937. $clearIdNumber=array(); //需要清除身份证
  1938. $studentCard=array();
  1939. $student_all_data = $this->sConn->createCommand("SELECT id,student_name,id_number FROM `import_student_temp` `t` ")->queryAll();
  1940. if($student_all_data){
  1941. //去除非中文字符
  1942. $updateArr=array();
  1943. $preg="/[^\x{4E00}-\x{9FFF}^·]+/u";
  1944. $count=0;
  1945. foreach($student_all_data as $val){
  1946. if(preg_match($preg,$val['student_name'])){
  1947. $ModifyName = preg_replace($preg, "", $val['student_name']);
  1948. if($ModifyName){
  1949. $updateArr[$val['id']]=$ModifyName;
  1950. $count++;
  1951. }
  1952. }else{
  1953. $updateArr[$val['id']]=$val['student_name'];
  1954. }
  1955. }
  1956. //组织更新语句
  1957. if($updateArr){
  1958. $arrNumber=ceil(count($updateArr)/500);
  1959. $Arr=array_chunk($updateArr,$arrNumber,true);
  1960. $transcation = $this->sConn->beginTransaction();
  1961. try {
  1962. foreach ($Arr as $item){
  1963. $sql='update import_student_temp set `modify_name`= case id ';
  1964. foreach ($item as $key=> $val){
  1965. $sql.=" WHEN ".$key." THEN '".$val."' ";
  1966. }
  1967. $sql.=" End ";
  1968. $sql.=" where modify_name='' or modify_name is null";
  1969. $this->sConn->createCommand($sql)->execute();
  1970. }
  1971. $transcation->commit();
  1972. $result['status']=1;
  1973. }catch (Exception $e){
  1974. $transcation->rollback();
  1975. }
  1976. }
  1977. $result['count']=$count;
  1978. }
  1979. exit(json_encode($result));
  1980. }
  1981. //匹配学生信息
  1982. public function actionMatchingClassified(){
  1983. $classNotFound=array(); //班级名称不一致
  1984. $repeat=array(); //重名
  1985. $success=array(); //正常
  1986. $updateClassValue=array(); //需要更新的班级不存在数据
  1987. $updateRepeatValue=array(); //需要更新重复数据
  1988. $studentName=array();
  1989. $studentCardValue=array(); //准考证号不存在
  1990. $updateStudentCard=array();
  1991. $idNumber=array();
  1992. $updateStudentId=array(); //匹配成功需要更新student_id数据
  1993. $updateClassId=array(); //更新班级id
  1994. $result['status']=0;
  1995. $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();
  1996. if(!$student_all_data){
  1997. $result['msg']='请先导入excel数据';
  1998. exit(json_encode($result));
  1999. }
  2000. $nowSemester=$this->semesterId;
  2001. foreach ($student_all_data as $key=>$val){
  2002. if($val['is_repeat']==1){
  2003. $repeat[]=$val['id'];
  2004. unset($student_all_data[$key]);
  2005. }
  2006. if($val['class_not_found']==1){
  2007. $classNotFound[$val['class_name']][]=$val;
  2008. unset($student_all_data[$key]);
  2009. }
  2010. if($val['class_not_found']==2){
  2011. $studentCardValue[]=$val['id'];
  2012. unset($student_all_data[$key]);
  2013. }
  2014. if($val['is_normal']==1){
  2015. $success[]=1;
  2016. unset($student_all_data[$key]);
  2017. }
  2018. }
  2019. if(!$student_all_data){
  2020. $result['studentCard']=count($studentCardValue);
  2021. $result['repeat']=count($repeat);
  2022. $result['success']=count($success);
  2023. $result['noExistent']=count($classNotFound);
  2024. $result['status']=1;
  2025. exit(json_encode($result));
  2026. }
  2027. //判断班级是否存在
  2028. foreach($student_all_data as $val){
  2029. $class_id=$this->schoolManager->classNameExists($val['class_name'],$nowSemester,2);
  2030. if(!$class_id){
  2031. $classNotFound[$val['class_name']][]=$val;
  2032. $updateClassValue[]="'".$val['class_name']."'"; //班级不存在
  2033. }else{
  2034. //校验科目是否匹配
  2035. $targetSubjects=$this->schoolManager->getSubjectByClass($class_id,'Arr');
  2036. if(!in_array($val['subject_id'],$targetSubjects)){
  2037. $classNotFound[$val['class_name']][]=$val;
  2038. $updateClassValue[]="'".$val['class_name']."'"; //班级不存在
  2039. continue;
  2040. }
  2041. $isFoundCard=false;
  2042. //判断准考证号
  2043. if($val['student_card']){
  2044. $sql="select student_id from student where student_card ='".$val['student_card']."' and status=0 and school_id='".$this->schoolId."'";
  2045. $b_student=$this->conn->createCommand($sql)->queryRow();
  2046. if($b_student){
  2047. //验证是否有行政班数据
  2048. $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();
  2049. if($checkXZB){
  2050. //验证是否有教学班数据,有则转班
  2051. $classifiedSql="SELECT c.class_id,c.class_name,subject_id,scr.student_id FROM `class_subject_relation` csr ";
  2052. $classifiedSql.="join class c on csr.class_id=c.class_id ";
  2053. $classifiedSql.="join student_class_relation scr on scr.class_id=c.class_id ";
  2054. $classifiedSql.="where subject_id=".$val['subject_id']." and c.class_type=2 and student_id='".$b_student['student_id']."';";
  2055. $studentClassified=$this->sConn->createCommand($classifiedSql)->queryRow();
  2056. if($studentClassified){
  2057. $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']."'";
  2058. }else{
  2059. $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']."'";
  2060. }
  2061. $success[]=$val['id'];
  2062. $isFoundCard=true;
  2063. }
  2064. }
  2065. }
  2066. if(!$isFoundCard){
  2067. //用学生姓名匹配
  2068. $getAllStudentName = $this->schoolManager->getStudentByNameClass($val['modify_name'],$nowSemester);
  2069. if(count($getAllStudentName)!=1){
  2070. $repeat[]=$val['id'];
  2071. }else{
  2072. //判断教学班
  2073. $subjectRepeat=false;
  2074. $classifiedStudent=$this->schoolManager->getStudentByNameClassified($val['modify_name'],$nowSemester);
  2075. if($classifiedStudent){
  2076. //判断学科
  2077. foreach ($classifiedStudent as $item){
  2078. $subjects=$this->schoolManager->getSubjectByClass($item['class_id'],'Arr');
  2079. if($subjects && in_array($val['subject_id'],$subjects)){ //已有教学班科目
  2080. $subjectRepeat=true;
  2081. $updateStudentCard[]="update import_student_temp set class_id='".$class_id."',student_id='".$item['student_id']."',is_normal=2 where modify_name='".$val['modify_name']."'";
  2082. break;
  2083. }
  2084. }
  2085. }
  2086. if(!$subjectRepeat){
  2087. $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']."'";
  2088. }
  2089. $success[]=$val['id'];
  2090. }
  2091. }
  2092. }
  2093. }
  2094. //更新检测成功的数据
  2095. if( $updateClassValue || $success || $updateStudentCard || $repeat){
  2096. $transcation = $this->sConn->beginTransaction();
  2097. try {
  2098. if($updateClassValue){
  2099. $updateClassValue=array_values($updateClassValue);
  2100. $this->sConn->createCommand("update import_student_temp set class_not_found=1 where class_name in(".implode(',',$updateClassValue).")")->execute();
  2101. }
  2102. if($updateStudentCard){
  2103. foreach ($updateStudentCard as $query){
  2104. $this->sConn->createCommand($query)->execute();
  2105. }
  2106. }
  2107. if($repeat){
  2108. $this->sConn->createCommand("update import_student_temp set is_repeat=1 where id in(".implode(',',$repeat).")")->execute();
  2109. }
  2110. if($success){
  2111. $success=array_values($success);
  2112. $this->sConn->createCommand("update import_student_temp set is_normal=1 where id in(".implode(',',$success).")")->execute();
  2113. }
  2114. $transcation->commit();
  2115. $result['status']=1;
  2116. }catch (Exception $e){
  2117. $result['status']=0;
  2118. $transcation->rollback();
  2119. }
  2120. }
  2121. $result['studentCard']=count($studentCardValue);
  2122. $result['repeat']=count($repeat);
  2123. $result['success']=count($success);
  2124. $result['noExistent']=count($classNotFound);
  2125. $result['status']=1;
  2126. exit(json_encode($result));
  2127. }
  2128. //班级名称不匹配
  2129. public function actionHandle_classified_existent(){
  2130. $notFound=$this->sConn->createCommand("select id,class_name from import_student_temp where class_not_found=1")->queryAll();
  2131. if(!$notFound) $this->redirect($this->createUrl('importstudent/checkClassified'));
  2132. $className=array();
  2133. foreach ($notFound as $val){
  2134. $className[$val['class_name']][]=$val;
  2135. }
  2136. $data['className']=$className;
  2137. $this->render('handle_classified_existent',$data);
  2138. }
  2139. //修改教学班级名称
  2140. public function actionChange_classified_value(){
  2141. $old_class_name=Req::post('data');
  2142. $value=Req::post('value');
  2143. $result['status']=0;
  2144. if(!$old_class_name || !$value){
  2145. $result['msg']='参数不正确';
  2146. exit(json_encode($result));
  2147. }
  2148. $valueArr=array();
  2149. $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."' ";
  2150. $data=$this->sConn->createCommand($sql)->queryAll();
  2151. if(!$data){
  2152. $result['msg']='数据异常';
  2153. exit(json_encode($result));
  2154. }
  2155. $nowSemester=$this->semesterId;
  2156. $class_id=$this->schoolManager->classNameExists($value,$nowSemester,2);
  2157. if(!$class_id){
  2158. $result['status']=0;
  2159. $result['msg']='班级在系统中不存在';
  2160. exit(json_encode($result));
  2161. }else{
  2162. //校验科目是否匹配
  2163. $targetSubjects=$this->schoolManager->getSubjectByClass($class_id,'Arr');
  2164. foreach ($data as $key => $val){
  2165. if(!in_array($val['subject_id'],$targetSubjects)){
  2166. $result['status']=0;
  2167. $result['msg']='班级和选择的学科不匹配';
  2168. exit(json_encode($result));
  2169. }
  2170. $isFoundCard=false;
  2171. //判断准考证号
  2172. if($val['student_card']){
  2173. $sql="select student_id from student where student_card ='".$val['student_card']."' and status=0 and school_id='".$this->schoolId."'";
  2174. $b_student=$this->conn->createCommand($sql)->queryRow();
  2175. if($b_student){
  2176. //验证是否有教学班数据,有则转班
  2177. $classifiedSql="SELECT c.class_id,c.class_name,subject_id,scr.student_id FROM `class_subject_relation` csr ";
  2178. $classifiedSql.="join class c on csr.class_id=c.class_id ";
  2179. $classifiedSql.="join student_class_relation scr on scr.class_id=c.class_id ";
  2180. $classifiedSql.="where subject_id=".$val['subject_id']." and c.class_type=2 and student_id='".$b_student['student_id']."';";
  2181. $studentClassified=$this->sConn->createCommand($classifiedSql)->queryRow();
  2182. if($studentClassified){
  2183. $valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','".$b_student['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
  2184. }else{
  2185. $valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','".$b_student['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
  2186. }
  2187. $isFoundCard=true;
  2188. }
  2189. }
  2190. if(!$isFoundCard){
  2191. //用学生姓名匹配
  2192. $getAllStudentName = $this->schoolManager->getStudentByNameClass($val['modify_name'],$nowSemester);
  2193. if(count($getAllStudentName)!=1){
  2194. $valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','','".$class_id."','".$val['student_card']."',1,0,0)";
  2195. }else{
  2196. //判断教学班
  2197. $subjectRepeat=false;
  2198. $classifiedStudent=$this->schoolManager->getStudentByNameClassified($val['modify_name'],$nowSemester);
  2199. if($classifiedStudent){
  2200. //判断学科
  2201. foreach ($classifiedStudent as $item){
  2202. $subjects=$this->schoolManager->getSubjectByClass($item['class_id']);
  2203. if($subjects && in_array($val['subject_id'],$subjects)){ //已有教学班科目
  2204. $subjectRepeat=true;
  2205. $valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','".$item['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
  2206. break;
  2207. }
  2208. }
  2209. }
  2210. if(!$subjectRepeat){
  2211. $valueArr[]="('".$val['student_name']."','".$value."','1','".$val['modify_name']."','".$val['extend']."','".$getAllStudentName[0]['student_id']."','".$class_id."','".$val['student_card']."',0,0,1)";
  2212. }
  2213. }
  2214. }
  2215. }
  2216. $transcation = $this->sConn->beginTransaction();
  2217. try {
  2218. if($valueArr) {
  2219. $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 ";
  2220. $this->sConn->createCommand("delete from import_student_temp where class_name='".$old_class_name."'")->execute();
  2221. $this->sConn->createCommand($insert.implode(',',$valueArr))->execute();
  2222. }
  2223. $transcation->commit();
  2224. $result['status']=1;
  2225. }catch (Exception $e){
  2226. $result['status']=0;
  2227. $transcation->rollback();
  2228. }
  2229. exit(json_encode($result));
  2230. }
  2231. }
  2232. //修改准考证号码
  2233. public function actionChangeClassifiedStudentCard(){
  2234. $id=Req::post('id');
  2235. $value=Req::post('value');
  2236. $result['status']=0;
  2237. if(!$id || !$value){
  2238. $result['msg']='参数不正确';
  2239. exit(json_encode($result));
  2240. }
  2241. $excel_data=$this->sConn->createCommand("select student_name,subject_id from import_student_temp where id='".$id."'")->queryRow();
  2242. if(!$excel_data){
  2243. $result['msg']='参数不正确';
  2244. exit(json_encode($result));
  2245. }
  2246. //判断表格中是否存在
  2247. $sql="select id,student_name from import_student_temp where student_card='".$value."' and id!='".$id."' ";
  2248. $data=$this->sConn->createCommand($sql)->queryRow();
  2249. if($data){
  2250. $result['msg']='导入的数据中中已存在相同准考证'.$data['student_name'].',请检查后重新确认';
  2251. exit(json_encode($result));
  2252. }
  2253. //验证系统中是否存在
  2254. $sql="select student_id from student where student_card ='".$value."' and status=0 and school_id='".$this->schoolId."'";
  2255. $b_student=$this->conn->createCommand($sql)->queryRow();
  2256. if(!$b_student){
  2257. $result['msg']='准考证号不存在,请检查后重新确认';
  2258. exit(json_encode($result));
  2259. }else{
  2260. //判断姓名
  2261. $student_card_name=$this->sConn->createCommand("select realname from student_info where student_id='".$b_student['student_id']."'")->queryRow();
  2262. if(!$student_card_name){
  2263. $result['msg']='准考证号不存在,请检查后重新确认';
  2264. exit(json_encode($result));
  2265. }elseif($student_card_name['realname']!=$excel_data['student_name']){
  2266. $result['msg']='新的准考证号与表格姓名不匹配,请检查后重新确认';
  2267. exit(json_encode($result));
  2268. }else{
  2269. //验证是否有教学班数据,有则转班
  2270. $classifiedSql="SELECT c.class_id,c.class_name,subject_id,scr.student_id FROM `class_subject_relation` csr ";
  2271. $classifiedSql.="join class c on csr.class_id=c.class_id ";
  2272. $classifiedSql.="join student_class_relation scr on scr.class_id=c.class_id ";
  2273. $classifiedSql.="where subject_id=".$excel_data['subject_id']." and c.class_type=2 and student_id='".$b_student['student_id']."';";
  2274. $studentClassified=$this->sConn->createCommand($classifiedSql)->queryRow();
  2275. if($studentClassified){
  2276. 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()){
  2277. $result['status']=1;
  2278. }
  2279. }else{
  2280. 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()){
  2281. $result['status']=1;
  2282. }
  2283. }
  2284. }
  2285. }
  2286. exit(json_encode($result));
  2287. }
  2288. //正常数据列表
  2289. public function actionNormalClassified(){
  2290. $condition = array();
  2291. $class_id = safe_replace(Yii::app()->request->getQuery('classId'));
  2292. $realname = safe_replace(Yii::app()->request->getQuery('realname'));
  2293. $grade = safe_replace(Yii::app()->request->getQuery('grade'));
  2294. if($class_id){
  2295. $condition[]=" class_id='".$class_id."'";
  2296. }
  2297. if($realname){
  2298. $condition[]=" modify_name like '%".$realname."%'";
  2299. }
  2300. $condition[]="is_normal!=0";
  2301. $rs=$this->schoolManager->getNormalStudent($condition,20);
  2302. $data['pages'] = $rs['pager'];
  2303. $data['page_total'] = $rs['pager']->rowsCount;
  2304. $data['list']=$rs['rs'];
  2305. $data['class_id']=$class_id;
  2306. $data['grade']=$grade;
  2307. $data['realname']=$realname;
  2308. $this->render('normal_classified',$data);
  2309. }
  2310. //根据年级读取教学班
  2311. public function actionGetClassifiedByGrade(){
  2312. $grade=Req::post('grade');
  2313. $semester=$this->semesterId;
  2314. $class=ClassModel::model()->getClassBySemesternianji($semester,$grade,2);
  2315. $result=array(
  2316. 'status'=>1
  2317. );
  2318. $data=array();
  2319. if($class){
  2320. foreach($class as $val){
  2321. $data[]=array(
  2322. 'class_id'=>$val->class_id,
  2323. 'class_name'=>$val->class_name,
  2324. );
  2325. }
  2326. }
  2327. $result['data']=$data;
  2328. exit(json_encode($result));
  2329. }
  2330. //匹配失败
  2331. public function actionHandleClassifiedCard(){
  2332. $notFound=$this->sConn->createCommand("select id,student_name,modify_name,class_name,student_card from import_student_temp where is_repeat=1")->queryAll();
  2333. if(!$notFound) $this->redirect($this->createUrl('importstudent/checkClassified'));
  2334. $data['student']=$notFound;
  2335. $this->render('handle_classified_card',$data);
  2336. }
  2337. //导入学生执行
  2338. public function actionConfirm_classified_import(){
  2339. $result['status']=0;
  2340. $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();
  2341. if(!$all_student){
  2342. $result['msg']='没有可导入的数据!';
  2343. exit(json_encode($result));
  2344. }
  2345. $updateStudentClassRelation = array();
  2346. $insertStudentClassRelation = array();
  2347. $semesterId = $this->semesterId;
  2348. $class_semester_data = ClassModel::model()->findAll('semester_id=:semester_id and class_type=2',array(':semester_id'=>$semesterId));
  2349. $classGrade=array();
  2350. if($class_semester_data)
  2351. {
  2352. foreach($class_semester_data as $v)
  2353. {
  2354. $class_ids[$v->class_id] = $v->class_id;
  2355. $classGrade[$v->class_id] = $v->grade;
  2356. }
  2357. }
  2358. unset($class_semester_data);
  2359. if(!$class_ids)
  2360. {
  2361. //Yii::app()->jump->error('无班级信息,导入失败');
  2362. $result['msg']='无班级信息,导入失败!';
  2363. exit(json_encode($result));
  2364. }
  2365. $class_serial_number = array();
  2366. // 获取在线学生
  2367. $criteria = new CDbCriteria();
  2368. $criteria->addInCondition('class_id',$class_ids);
  2369. $criteria->addCondition('status=0');
  2370. $semester_student_class_data = SStudentClassRelation::model()->findAll($criteria);
  2371. if($semester_student_class_data)
  2372. {
  2373. foreach($semester_student_class_data as $v)
  2374. {
  2375. //获取班级中的最大序号
  2376. if(isset($class_serial_number[(string)$v->class_id]))
  2377. {
  2378. if($class_serial_number[(string)$v->class_id]<$v->serial_number)
  2379. {
  2380. $class_serial_number[(string)$v->class_id] = $v->serial_number;
  2381. }
  2382. }else
  2383. {
  2384. $class_serial_number[(string)$v->class_id] = $v->serial_number;
  2385. }
  2386. }
  2387. }
  2388. foreach($all_student as $val){
  2389. if($val['student_id']){
  2390. //转班
  2391. $studentInfo = $this->schoolManager->getStudentByStudentIds(array($val['student_id']));
  2392. if(!$studentInfo){
  2393. $result['msg']='数据异常,导入失败!';
  2394. exit(json_encode($result));
  2395. }
  2396. //查询目标班是否已存在
  2397. $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();
  2398. if($checkRepeat){
  2399. if($checkRepeat['status']==0){
  2400. continue;
  2401. }
  2402. }
  2403. if(!isset($class_serial_number[$val['class_id']])){
  2404. $class_serial_number[$val['class_id']]=0;
  2405. }
  2406. $insertStudentClassRelation[$val['student_id']]="('".$val['class_id']."','".$val['student_id']."','".($class_serial_number[$val['class_id']]+1)."','0','".time()."',0,2)";
  2407. //删除相同科目其它教学班数据
  2408. $classifiedSql="SELECT c.class_id,c.class_name,subject_id,scr.student_id FROM `class_subject_relation` csr ";
  2409. $classifiedSql.="join class c on csr.class_id=c.class_id ";
  2410. $classifiedSql.="join student_class_relation scr on scr.class_id=c.class_id ";
  2411. $classifiedSql.="where subject_id=".$val['subject_id']." and c.class_type=2 and student_id='".$val['student_id']."';";
  2412. $studentClassified=$this->sConn->createCommand($classifiedSql)->queryRow();
  2413. if($studentClassified){
  2414. $updateStudentClassRelation[$studentClassified['class_id']]['status'][$val['student_id']]=1;
  2415. $updateStudentClassRelation[$studentClassified['class_id']]['operation'][$val['student_id']]=2;
  2416. }
  2417. $class_serial_number[$val['class_id']]++;
  2418. }
  2419. }
  2420. //执行sql
  2421. $transaction = $this->sConn->beginTransaction();
  2422. try {
  2423. if($updateStudentClassRelation){
  2424. foreach ($updateStudentClassRelation as $class_id =>$updateValue){
  2425. $studentClassUpdateSql="update student_class_relation set ";
  2426. $studentClassUpdateSetArr=array();
  2427. foreach($updateValue as $field =>$val){
  2428. $studentClassUpdateSetStr=" `".$field."`= case student_id ";
  2429. foreach ($val as $id=>$value){
  2430. $studentClassUpdateSetStr.=" WHEN {$id} THEN '{$value}' ";
  2431. }
  2432. $studentClassUpdateSetStr.="ELSE `{$field}`";
  2433. $studentClassUpdateSetStr.=" END ";
  2434. $studentClassUpdateSetArr[]=$studentClassUpdateSetStr;
  2435. }
  2436. if($studentClassUpdateSetArr){
  2437. $studentClassUpdateSql.=implode(',',$studentClassUpdateSetArr)." where class_id ='".$class_id."' ";
  2438. $this->sConn->createCommand($studentClassUpdateSql)->execute();
  2439. }
  2440. }
  2441. }
  2442. if($insertStudentClassRelation){
  2443. $class_sql="insert into student_class_relation(`class_id`,`student_id`,`serial_number`,`userno`,`update_time`,`operation`,`class_type`) values";
  2444. $class_sql.=implode(',',$insertStudentClassRelation);
  2445. $this->sConn->createCommand($class_sql)->execute();
  2446. }
  2447. //$delSql="truncate table import_student_temp;";
  2448. $delSql="delete from import_student_temp;";
  2449. $this->sConn->createCommand($delSql)->execute();
  2450. $transaction->commit();
  2451. if(Yii::app()->params['handle_log_on_off'])
  2452. {
  2453. writeFileLog(jsonEncode(array(
  2454. "exam_group_id" => 0,
  2455. "operate_project" => 'zsyas2',
  2456. "school_id" => $this->schoolId,
  2457. "title" => '导入教学班学生',
  2458. "operate_account" => Yii::app()->session['coachInfo']['coach_name'],
  2459. "operate_method" => $this->action,
  2460. "operate_url" => $this->getRoute(),
  2461. "operate_sql" =>'',
  2462. "operate_param" =>'',
  2463. "date"=>date('Y-m-d H:i:s')
  2464. )));
  2465. }
  2466. $result['status']=1;
  2467. }catch (Exception $e) {
  2468. $transaction->rollback();
  2469. exit(json_encode($result));
  2470. }
  2471. unset($transaction);
  2472. exit(json_encode($result));
  2473. }
  2474. //根据教学班级读取学生
  2475. public function actionLoadStudentByClassified(){
  2476. $realname = safe_replace(Req::post('classname'));
  2477. $result['status']=0;
  2478. if(!$realname){
  2479. exit(json_encode($result));
  2480. }
  2481. $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();
  2482. $extendTh='';
  2483. $html='<tr>';
  2484. $html.="<td>教学班名称</td>";
  2485. $html.="<td>姓名</td>";
  2486. $html.="<td>准考证号</td>";
  2487. if($all_student){
  2488. if($all_student){
  2489. foreach ($all_student as $key=>$val){
  2490. $extendTd='<tr>';
  2491. $extendTd.="<td>{$val['class_name']}</td>";
  2492. $extendTd.="<td>{$val['modify_name']}(原{$val['student_name']})</td>";
  2493. $extendTd.="<td>{$val['student_card']}</td>";
  2494. $extendTd.="</tr>";
  2495. $html.=$extendTd;
  2496. }
  2497. $html.="";
  2498. }
  2499. }
  2500. $result['html']=$html;
  2501. $result['status']=1;
  2502. exit(json_encode($result));
  2503. }
  2504. }