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("相关配置信息不能为空"); } //取出所有二级学院 $secondaryCollegeList = SecondaryCollege::query()->where([ "status" => SecondaryCollege::STATUS_YES ])->get()->toArray(); if(empty($secondaryCollegeList)){ throw new \Exception("识别失败,二级学院信息为空"); } //以学院名作为键 $secondaryCollegeList = array_column($secondaryCollegeList, null, "name"); //宿舍类型 $dormitoryTypeList = DormitoryType::query()->where([ "status" => DormitoryType::STATUS_YES ])->get()->toArray(); if(empty($dormitoryTypeList)){ throw new \Exception("识别失败,宿舍类型信息为空"); } $dormitoryTypeList = array_column($dormitoryTypeList, null, "dormitory"); //生活区 $livingAreaList = LivingArea::query()->where([ "status" => LivingArea::STATUS_YES ])->get()->toArray(); if(empty($livingAreaList)){ throw new \Exception("识别失败,生活区信息为空"); } $livingAreaList = array_column($livingAreaList, null, "title"); //楼栋 $buildingList = Building::query()->where([ "status" => Building::STATUS_YES ])->get()->toArray(); if(empty($buildingList)){ throw new \Exception("识别失败,楼栋信息为空"); } $buildingList = array_column($buildingList, null, "building_title"); //楼层 $floorList = Floor::query()->where([ "status" => Floor::STATUS_YES ])->get()->toArray(); if(empty($floorList)){ throw new \Exception("识别失败,楼层信息为空"); } $floorList = array_column($floorList, null, "floor_title"); //宿舍号 $dormitoryNumberList = DormitoryNumber::query()->where([ "status" => DormitoryNumber::STATUS_YES ])->get()->toArray(); if(empty($dormitoryNumberList)){ throw new \Exception("识别失败,宿舍号信息为空"); } $dormitoryNumberList = array_column($dormitoryNumberList, null, "dormitory_number"); //床位 $bedList = Bed::query()->where([ "status" => Bed::STATUS_YES ])->get()->toArray(); if(empty($bedList)){ throw new \Exception("识别失败,床位信息为空"); } $bedList = array_column($bedList, null, "bed_number"); //入库 $insertData = []; foreach($list as $key => $item){ $arr = [ "create_time" => time(), "annual_session" => $config->data ]; //检测性别 if(trim($item["男女生宿舍"]) == "男"){ $arr["sex"] = 1; }else if(trim($item["男女生宿舍"]) == "女"){ $arr["sex"] = 2; } //初识化专业id $speciality_id = 0; if(!array_key_exists(trim($item["二级学院"]), $secondaryCollegeList)){ throw new \Exception("识别失败,找不到【二级学院】--".trim($item["二级学院"])."--第".($key+2)."行"); } //取出指定学院下面的专业 $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("识别失败,请检查二级学院或专业名称是否和后台录入的一致,请检查第".($key+1)."行的学院及专业:".$item["二级学院"]."--".$item["专业名称"]); } //专业id $arr["speciality_id"] = $speciality_id; //宿舍类型id $arr["dormitory_type"] = $dormitoryTypeList[trim($item["宿舍类型"])]["id"]; //多人间 $multipleWorlds = MultipleWorld::query()->where([ "dormitory_type_id" => $dormitoryTypeList[trim($item["宿舍类型"])]["id"], "people" => trim($item["多人间"]), "status" => MultipleWorld::STATUS_YES ])->first(); if(empty($multipleWorlds)){ throw new \Exception("识别失败,多人间信息为空"); } //多人间id $arr["multiple_worlds"] = $multipleWorlds->id; //生活区id if(array_key_exists(trim($item["生活区"]), $livingAreaList)){ $arr["living_area"] = $livingAreaList[trim($item["生活区"])]["id"]; }else{ throw new \Exception("识别失败,找不到【生活区】--".trim($item["生活区"])."--第".($key+2)."行"); } //楼栋id if(array_key_exists(trim($item["楼栋"]), $buildingList)){ $arr["building_id"] = $buildingList[trim($item["楼栋"])]["id"]; }else{ throw new \Exception("识别失败,找不到【楼栋】--".trim($item["楼栋"])."--第".($key+2)."行"); } //楼层id if(array_key_exists(trim($item["楼层"]), $floorList)){ $arr["floor_id"] = $floorList[trim($item["楼层"])]["id"]; }else{ throw new \Exception("识别失败,找不到【楼层】--".trim($item["楼层"])."--第".($key+2)."行"); } //宿舍号id if(array_key_exists(trim($item["宿舍号"]), $dormitoryNumberList)){ $arr["dormitory_number"] = $dormitoryNumberList[trim($item["宿舍号"])]["id"]; }else{ throw new \Exception("识别失败,找不到【宿舍号】--".trim($item["宿舍号"])."--第".($key+2)."行"); } //床位id if(array_key_exists(trim($item["床位号"]), $bedList)){ $arr["bed_id"] = $bedList[trim($item["床位号"])]["id"]; }else{ throw new \Exception("识别失败,找不到【床位号】--".trim($item["床位号"])."--第".($key+2)."行"); } //走读生 if(trim($item["走读生"]) == "是"){ $arr["day_student"] = AllocationDormitoryBed::DAY_STUDENT_YES; } //零元入学 if(trim($item["零元入学"]) == "是"){ $arr["zero_enrol"] = AllocationDormitoryBed::ZERO_ENROL_YES; } array_push($insertData, $arr); } DB::beginTransaction(); try { $chunk_list = array_chunk($insertData, 1000); foreach ($chunk_list as $new_list) { $add = AllocationDormitoryBed::query()->insert($new_list); if($add != count($insertData)){ throw new \Exception("导入数据失败1,请重试"); } } DB::commit(); }catch (\PDOException $e){ DB::rollBack(); // 先回滚事务 if ($e->getCode() === '23000') { // 唯一性约束错误处理逻辑 $errorMessage = "导入表格中存在已入库床位信息,请勿重复导入"; // 可以根据需要进行相关处理 throw new \Exception($errorMessage); } else { // 其他类型的错误处理逻辑 throw new \Exception("导入数据失败2,请重试"); } }catch (\Exception $e){ DB::rollBack(); throw new \Exception("导入数据失败3,请重试"); } } //批量导入1000条 public function batchSize(): int { return 1000; } //以1000条数据基准切割数据 public function chunkSize(): int { return 1000; } }