public function readline_examinee($sheet, $project_id, $number, $i) { $examinee = new Examinee(); foreach ($this->excel_col as $key => $value) { $examinee->{$value} = self::filter($sheet->getCell($key . $i)->getValue()); } $sex = self::filter($sheet->getCell($key . $i)->getValue()); if ($sex == '男' || $sex == 1) { $examinee->sex = 1; } else { $examinee->sex = 1; } $education = array(); $work = array(); $this->readother_examinee($sheet, $education, $this->edu_name, $i); $this->readother_examinee($sheet, $work, $this->work_name, $i); $examinee->other = json_encode(array('education' => $education, 'work' => $work)); $examinee->number = date('y') . sprintf("%02d", $project_id) . sprintf("%04d", $number); $examinee->password = $this->random_string(); $examinee->project_id = $project_id; if (!$examinee->save()) { foreach ($examinee->getMessages() as $message) { throw new Exception($message); } } }
/** * 导入被试信息列表 */ public static function insertExaminee($data, $project_id, $type = 0) { //对原有数据进行整理 $examinee = Examinee::find(array('project_id = :project_id:', "order" => "number desc", 'bind' => array('project_id' => $project_id))); $new_count = count($data); //1501 0001 $already_number = 0; if (count($examinee) == 0) { $already_number = 0; } else { $already_number = $examinee[0]->number - $project_id * 10000; } #异常 if ($new_count + $already_number > 9999) { throw new Exception('项目人数超限-9999'); } $start = $project_id * 10000 + $already_number + 1; try { $manager = new TxManager(); $transaction = $manager->get(); foreach ($data as $value) { $examinee = new Examinee(); $examinee->setTransaction($transaction); $examinee->project_id = $project_id; $examinee->state = 0; $examinee->number = $start++; foreach ($value as $skey => $svalue) { $examinee->{$skey} = $svalue; } $examinee->password = self::getRandString(); $examinee->type = $type; if ($examinee->save() == false) { $transaction->rollback("数据插入失败"); } } $transaction->commit(); return true; } catch (TxFailed $e) { throw new Exception($e->getMessage()); } }