...
|
...
|
@@ -397,9 +397,17 @@ class User extends Backend |
|
|
$fieldArr['密码'] = 'password';
|
|
|
$fieldArr['性别'] = 'sex';
|
|
|
$fieldArr['工作单位'] = 'work_address';
|
|
|
$fieldArr['报考科目'] = 'examname';
|
|
|
$fieldArr['报考科目到期时间'] = 'periodtime';
|
|
|
|
|
|
//加载文件
|
|
|
$insert_origin = [];
|
|
|
$insert = [];
|
|
|
$third_study_data = [];
|
|
|
$study_class_data = [];
|
|
|
$createtime = time();
|
|
|
$user_study_exist = [];
|
|
|
$user_exist = [];
|
|
|
try {
|
|
|
if (!$PHPExcel = $reader->load($filePath)) {
|
|
|
$this->error(__('Unknown data format'));
|
...
|
...
|
@@ -440,32 +448,68 @@ class User extends Backend |
|
|
$row['sex'] = 2;
|
|
|
break;
|
|
|
}
|
|
|
// $row['expirationtime'] = intval(($row['expirationtime'] - 25569) * 3600 * 24) - 28800; //转换成1970年以来的秒数
|
|
|
$insert[] = $row;
|
|
|
if(empty($row['username'])){
|
|
|
throw new Exception('第'.$currentRow.'行请填写用户名');
|
|
|
}
|
|
|
if(empty($row['password'])){
|
|
|
throw new Exception('第'.$currentRow.'行请填写密码');
|
|
|
}
|
|
|
if(!empty($row['examname']) && empty($row['periodtime'])){
|
|
|
throw new Exception('第'.$currentRow.'行请填写报考科目到期时间');
|
|
|
}
|
|
|
if(empty($row['examname']) && !empty($row['periodtime'])){
|
|
|
throw new Exception('第'.$currentRow.'行请填写报考科目');
|
|
|
}
|
|
|
$row['examname'] = !empty($row['examname']) ? $row['examname'] : '';
|
|
|
if(!isset($user_study_exist[$row['username'].'-'.$row['examname']])){
|
|
|
if ($user = UserModel::where('username',$row['username'])->field('id')->find()) {
|
|
|
// 添加报考科目信息
|
|
|
$study = Db::name('study')->where('examname',$row['examname'])->field('id')->find();
|
|
|
$third_study = Db::name('third_study')->where('study_id',$study['id'])->where('third_id',$user['id'])->find();
|
|
|
if($study && !$third_study){
|
|
|
$third_study_data[] = [
|
|
|
'third_id' => $user['id'],
|
|
|
'study_id' => $study['id'],
|
|
|
'periodtime' => intval(($row['periodtime'] - 25569) * 3600 * 24) - 28800, //转换成1970年以来的秒数
|
|
|
];
|
|
|
$classes_ids = Db::name('classes')->where('study_id',$study['id'])->where('is_major',1)->column('id');
|
|
|
foreach ($classes_ids as $class_id){
|
|
|
$study_class_data[] = [
|
|
|
'third_id' => $user['id'],
|
|
|
'study_id' => $study['id'],
|
|
|
'class_id' => $class_id,
|
|
|
'playtime' => 0,
|
|
|
'createtime' => $createtime,
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
$user_study_exist[$row['username'].'-'.$row['examname']] = 1;
|
|
|
}else{
|
|
|
// 原始数据
|
|
|
$insert_origin[] = $row;
|
|
|
$user_study_exist[$row['username'].'-'.$row['examname']] = 1;
|
|
|
|
|
|
if(!isset($user_exist[$row['username']])){
|
|
|
// 调整后的数据
|
|
|
unset($row['examname'],$row['periodtime']);
|
|
|
$insert[] = $row;
|
|
|
$user_exist[$row['username']] = 1;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception $exception) {
|
|
|
$this->error($exception->getMessage());
|
|
|
}
|
|
|
if (!$insert) {
|
|
|
// if (!$insert) {
|
|
|
// $this->error(__('No rows were updated'));
|
|
|
// }
|
|
|
if (!$insert && !$third_study_data && !$study_class_data) {
|
|
|
$this->error(__('No rows were updated'));
|
|
|
}
|
|
|
|
|
|
foreach ($insert as $k => $v){
|
|
|
if(empty($v['username'])){
|
|
|
$this->error('第'.($k+2).'行请填写用户名');
|
|
|
}
|
|
|
if(empty($v['password'])){
|
|
|
$this->error('第'.($k+2).'行请填写密码');
|
|
|
}
|
|
|
if (UserModel::getByUsername($v['username'])) {
|
|
|
$this->error('第'.($k+2).'行用户名已存在');
|
|
|
}
|
|
|
if (!empty($v['mobile']) && UserModel::getByMobile($v['mobile'])) {
|
|
|
$this->error('第'.($k+2).'行手机号已存在');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
//是否包含admin_id字段
|
|
|
$has_admin_id = false;
|
...
|
...
|
@@ -483,14 +527,49 @@ class User extends Backend |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
$this->model->saveAll($insert);
|
|
|
if($insert){
|
|
|
$this->model->saveAll($insert);
|
|
|
}
|
|
|
// 添加报考科目
|
|
|
foreach ($insert_origin as $v){
|
|
|
$third_id = UserModel::where('username',$v['username'])->value('id');
|
|
|
// 添加报考科目信息
|
|
|
$study = Db::name('study')->where('examname',$v['examname'])->field('id')->find();
|
|
|
$third_study = Db::name('third_study')->where('study_id',$study['id'])->where('third_id',$third_id)->find();
|
|
|
if($study && !$third_study){
|
|
|
$third_study_data[] = [
|
|
|
'third_id' => $third_id,
|
|
|
'study_id' => $study['id'],
|
|
|
'periodtime' => intval(($v['periodtime'] - 25569) * 3600 * 24) - 28800, //转换成1970年以来的秒数
|
|
|
];
|
|
|
$classes_ids = Db::name('classes')->where('study_id',$study['id'])->where('is_major',1)->column('id');
|
|
|
foreach ($classes_ids as $class_id){
|
|
|
$study_class_data[] = [
|
|
|
'third_id' => $third_id,
|
|
|
'study_id' => $study['id'],
|
|
|
'class_id' => $class_id,
|
|
|
'playtime' => 0,
|
|
|
'createtime' => $createtime,
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if(!empty($third_study_data)){
|
|
|
Db::name('third_study')->insertAll($third_study_data);
|
|
|
}
|
|
|
if(!empty($study_class_data)){
|
|
|
Db::name('study_class')->insertAll($study_class_data);
|
|
|
}
|
|
|
Db::commit();
|
|
|
} catch (PDOException $exception) {
|
|
|
Db::rollback();
|
|
|
$msg = $exception->getMessage();
|
|
|
if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
|
|
|
$msg = "导入失败,包含【{$matches[1]}】的记录已存在";
|
|
|
};
|
|
|
$this->error($msg);
|
|
|
} catch (Exception $e) {
|
|
|
Db::rollback();
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
|
...
|
...
|
|