You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
6.0 KiB
184 lines
6.0 KiB
<?php |
|
|
|
namespace App\Imports; |
|
use App\Models\AdmissionNewStudents; |
|
use App\Models\Config; |
|
use App\Models\SecondaryCollege; |
|
use App\Models\Speciality; |
|
use Illuminate\Support\Collection; |
|
use Illuminate\Database\Eloquent\Model; |
|
use Maatwebsite\Excel\Concerns\ToModel; |
|
use Maatwebsite\Excel\Concerns\ToCollection; |
|
use Maatwebsite\Excel\Concerns\WithHeadingRow; |
|
use Maatwebsite\Excel\Concerns\WithBatchInserts; |
|
use Maatwebsite\Excel\Concerns\WithChunkReading; |
|
use Maatwebsite\Excel\Imports\HeadingRowFormatter; |
|
use Illuminate\Support\Facades\DB; |
|
use Throwable; |
|
HeadingRowFormatter:: |
|
default('none'); |
|
|
|
class FirstSheetImport implements ToCollection, WithBatchInserts, WithChunkReading, WithHeadingRow, ToModel |
|
{ |
|
private $round; |
|
|
|
public function __construct(int $round) |
|
{ |
|
$this->round = $round; |
|
} |
|
|
|
|
|
/** |
|
* @param array $row |
|
* |
|
* @return Model|Model[]|null |
|
*/ |
|
public function model(array $row) |
|
{ |
|
|
|
//写导入的逻辑关系 |
|
// $user = Orderuser::where('phone', '=', $row['电话'])->where('name', '=',$row['姓名'])->first(); |
|
|
|
// dd($row); |
|
// // 数据库对应的字段 |
|
return null; |
|
} |
|
|
|
public function collection(Collection $rows) |
|
{ |
|
//导入的表格所有数据都在此处显示 |
|
$list = $rows->toArray(); |
|
if(empty($list)){ |
|
throw new \Exception("请勿上传空文件"); |
|
} |
|
|
|
//取出当前年份 |
|
$config = Config::query()->where([ |
|
"unique_identification" => "annual_session" |
|
])->first(); |
|
if(empty($config)){ |
|
throw new \Exception("相关配置信息不能为空"); |
|
} |
|
|
|
//入库 |
|
$insertData = []; |
|
|
|
foreach($list as $key => $item){ |
|
$arr = [ |
|
"mobile" => trim($item["手机号"]), |
|
"name" => trim($item["姓名"]), |
|
"idCard" => trim($item["身份证"]), |
|
"admission_college" => trim($item["录取学院"]), |
|
"admitted_major" => trim($item["录取专业"]), |
|
"admission_code" => trim($item["学号"]), |
|
"nj" => trim($item["年级"]), |
|
"xydm" => trim($item["学院代码"]), |
|
"zydm" => trim($item["专业代码"]), |
|
"bjdm" => trim($item["班级代码"]), |
|
"xz" => trim($item["学制代码"]), |
|
"xslbdm" => trim($item["学生类别代码"]), |
|
"xjztdm" => trim($item["学籍状态代码"]), |
|
"sfzx" => trim($item["是否在校代码"]), |
|
"is_five_year" => trim($item["是否五年一贯制"]), |
|
"is_equivalent" => trim($item["考生类型"]), |
|
"is_four_type" => trim($item["考生分类"]), |
|
"province" => trim($item["省份"]), |
|
"create_time" => time(), |
|
"annual_session" => $config->data |
|
]; |
|
//检测性别 |
|
if(trim($item["性别"]) == "男"){ |
|
$arr["sex"] = 1; |
|
}else if(trim($item["性别"]) == "女"){ |
|
$arr["sex"] = 2; |
|
} |
|
|
|
//走读生 |
|
if(trim($item["走读生"]) == "是"){ |
|
$arr["day_student"] = AdmissionNewStudents::DAY_STUDENT_YES; |
|
} |
|
|
|
//零元入学 |
|
if(trim($item["零元入学"]) == "是"){ |
|
$arr["zero_enrol"] = AdmissionNewStudents::ZERO_ENROL_YES; |
|
} |
|
|
|
//检测专业 |
|
//取出所有二级学院 |
|
$secondaryCollegeList = SecondaryCollege::query()->where([ |
|
"status" => SecondaryCollege::STATUS_YES |
|
])->get()->toArray(); |
|
if(empty($secondaryCollegeList)){ |
|
throw new \Exception("识别失败,二级学院信息为空"); |
|
} |
|
|
|
//以学院名作为键 |
|
$secondaryCollegeList = array_column($secondaryCollegeList, null, "name"); |
|
|
|
//初识化专业id |
|
$speciality_id = 0; |
|
|
|
//取出指定学院下面的专业 |
|
$specialityInfo = Speciality::query()->where([ |
|
"secondary_college_id" => $secondaryCollegeList[trim($item["录取学院"])]["id"], |
|
"speciality_name" => trim($item["录取专业"]), |
|
"status" => Speciality::STATUS_YES |
|
])->first(); |
|
if(!empty($specialityInfo)){ |
|
$speciality_id = $specialityInfo->id; |
|
} |
|
|
|
//专业ID为空时提示 |
|
if(empty($speciality_id)){ |
|
throw new \Exception("识别失败,请检查二级学院或专业名称是否和后台录入的一致"); |
|
} |
|
|
|
$arr["speciality_id"] = $speciality_id; |
|
|
|
array_push($insertData, $arr); |
|
} |
|
|
|
DB::beginTransaction(); |
|
try { |
|
|
|
$chunk_list = array_chunk($insertData, 1000); |
|
|
|
foreach ($chunk_list as $new_list) { |
|
$add = AdmissionNewStudents::query()->insert($new_list); |
|
if($add != count($new_list)){ |
|
throw new \Exception("导入数据失败,请重试"); |
|
} |
|
} |
|
|
|
DB::commit(); |
|
}catch (\PDOException $e){ |
|
DB::rollBack(); |
|
if ($e->getCode() === '23000') { |
|
// 唯一性约束错误处理逻辑 |
|
$errorMessage = "导入表格中存在已入库学生信息,请勿重复导入"; |
|
// 可以根据需要进行相关处理 |
|
throw new \Exception($errorMessage); |
|
} else { |
|
// 其他类型的错误处理逻辑 |
|
throw new \Exception("导入数据失败,请重试"); |
|
} |
|
}catch (\Exception $e){ |
|
DB::rollBack(); |
|
throw new \Exception("导入数据失败,请重试"); |
|
} |
|
|
|
} |
|
|
|
|
|
//批量导入1000条 |
|
public function batchSize(): int |
|
{ |
|
return 1000; |
|
} |
|
|
|
//以1000条数据基准切割数据 |
|
public function chunkSize(): int |
|
{ |
|
return 1000; |
|
} |
|
}
|
|
|