From bf396ea154b2700999f8b5f2488027a5a0a58491 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Thu, 18 Jun 2026 15:16:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=B4=E4=BF=AE=E8=BD=A6=E4=BD=8D=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/ParkingSpaceRepairController.php | 11 ++++- app/Imports/ParkingSpaceImport.php | 10 ++--- app/Imports/ParkingSpaceRepairImport.php | 40 ++++++++++++++----- resources/lang/en/imports.php | 6 ++- resources/lang/zh-CN/imports.php | 6 ++- resources/lang/zh-TW/imports.php | 14 ++++--- 6 files changed, 64 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/Admin/ParkingSpaceRepairController.php b/app/Http/Controllers/Admin/ParkingSpaceRepairController.php index 30d3b32..ce3dcc1 100644 --- a/app/Http/Controllers/Admin/ParkingSpaceRepairController.php +++ b/app/Http/Controllers/Admin/ParkingSpaceRepairController.php @@ -387,18 +387,25 @@ class ParkingSpaceRepairController extends BaseController // 4. 执行导入(使用存储后的绝对路径) // storage_path('app') 获取 storage/app 的绝对路径 + $model = new ParkingSpaceRepairImport($this->adminUserId); Excel::import( - new ParkingSpaceRepairImport($this->adminUserId), + $model, storage_path('app/' . $path) ); // 5. (可选)导入完成后删除临时文件 Storage::delete($path); + // 6. 返回错误行 + $error_arr = $model->errorArr(); + if ($error_arr) { + throw new CustomException(implode("
", $error_arr)); + } + return $this->responseService->success( __('controller.import.success') ); - } catch (ValidationException $e) { + } catch (ValidationException|CustomException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( diff --git a/app/Imports/ParkingSpaceImport.php b/app/Imports/ParkingSpaceImport.php index 137eaaa..20ee0bc 100644 --- a/app/Imports/ParkingSpaceImport.php +++ b/app/Imports/ParkingSpaceImport.php @@ -32,11 +32,11 @@ class ParkingSpaceImport implements ToModel, WithChunkReading return; } $this->index += 1; - $parking = $row[1]; - $floor = $row[2]; - $region = $row[3]; - $attr = $row[4]; - $number = $row[5]; + $parking = $row[1] ?? ''; + $floor = $row[2] ?? ''; + $region = $row[3] ?? ''; + $attr = $row[4] ?? ''; + $number = $row[5] ?? ''; if (empty($parking)) { $this->error[] = imports_error($this->index, 'import20'); return; diff --git a/app/Imports/ParkingSpaceRepairImport.php b/app/Imports/ParkingSpaceRepairImport.php index ffd704c..d09ae4a 100644 --- a/app/Imports/ParkingSpaceRepairImport.php +++ b/app/Imports/ParkingSpaceRepairImport.php @@ -11,8 +11,9 @@ use Maatwebsite\Excel\Concerns\WithChunkReading; class ParkingSpaceRepairImport implements ToModel, WithChunkReading { - protected int $index = 1; + protected int $index = 0; protected int $user_id; + protected array $error = []; public function __construct($admin_user_id) { @@ -21,37 +22,53 @@ class ParkingSpaceRepairImport implements ToModel, WithChunkReading public function model(array $row) { - if ($this->index == 1) { + if (!$this->index) { $this->index += 1; return; } - $space_number = $row[1]; + $this->index += 1; + $space_number = $row[1] ?? ''; if (empty($space_number)) { + $this->error[] = imports_error($this->index, 'import15'); return; } - $floor_name = $row[2]; + $floor_name = $row[2] ?? ''; if (empty($floor_name)) { + $this->error[] = imports_error($this->index, 'import21'); return; } - $parking_name = $row[3]; + $parking_name = $row[3] ?? ''; if (empty($parking_name)) { + $this->error[] = imports_error($this->index, 'import20'); + return; + } + $start_at = $row[4] ?? ''; + if (empty($start_at)) { + $this->error[] = imports_error($this->index, 'import29'); return; } - $start_at = $row[4]; $start_times = strtotime($start_at); - if (empty($start_at) || !$start_times) { + if (!$start_times) { + $this->error[] = imports_error($this->index, 'import30'); return; } - $end_at = $row[5]; + $end_at = $row[5] ?? ''; $end_times = strtotime($end_at); - if (empty($end_at) || !$end_times) { + if (empty($end_at)) { + $this->error[] = imports_error($this->index, 'import31'); + return; + } + if (empty(!$end_times)) { + $this->error[] = imports_error($this->index, 'import32'); return; } if ($start_times >= $end_times) { + $this->error[] = imports_error($this->index, 'import13'); return; } $space_id = ParkingSpace::getValueId($space_number); if (empty($space_id)) { + $this->error[] = imports_error($this->index, 'import17'); return; } @@ -68,4 +85,9 @@ class ParkingSpaceRepairImport implements ToModel, WithChunkReading { return 1000; // 设置每次处理的行数,有助于避免内存问题并可能改善表头解析 } + + public function errorArr(): array + { + return $this->error; + } } diff --git a/resources/lang/en/imports.php b/resources/lang/en/imports.php index a7601cc..13ddabb 100644 --- a/resources/lang/en/imports.php +++ b/resources/lang/en/imports.php @@ -28,5 +28,9 @@ return [ 'import25' => 'The floor does not exist.', 'import26' => 'The region does not exist.', 'import27' => 'Parking space attributes do not exist', - 'import28' => 'The parking space number for the assigned floor already exists.' + 'import28' => 'The parking space number for the assigned floor already exists.', + 'import29' => 'The repair start time cannot be empty.', + 'import30' => 'Repair start time data error', + 'import31' => 'The repair end time cannot be empty.', + 'import32' => 'Repair end time data error' ]; diff --git a/resources/lang/zh-CN/imports.php b/resources/lang/zh-CN/imports.php index a9fd64f..f6af52e 100644 --- a/resources/lang/zh-CN/imports.php +++ b/resources/lang/zh-CN/imports.php @@ -28,5 +28,9 @@ return [ 'import25' => '所属楼层不存在', 'import26' => '所属区域不存在', 'import27' => '车位属性不存在', - 'import28' => '所属楼层车位号已存在' + 'import28' => '所属楼层车位号已存在', + 'import29' => '维修开始时间不能为空', + 'import30' => '维修开始时间数据错误', + 'import31' => '维修结束时间不能为空', + 'import32' => '维修结束时间数据错误' ]; diff --git a/resources/lang/zh-TW/imports.php b/resources/lang/zh-TW/imports.php index cb56e29..c4791f8 100644 --- a/resources/lang/zh-TW/imports.php +++ b/resources/lang/zh-TW/imports.php @@ -6,13 +6,13 @@ return [ 'import3' => '模式名稱不能為空', 'import4' => '模式名稱不存在', 'import5' => '開始日期不能為空', - 'import6' => '開始日期資料錯誤', + 'import6' => '開始日期數據錯誤', 'import7' => '開始時間不能為空', - 'import8' => '開始時間資料錯誤', + 'import8' => '開始時間數據錯誤', 'import9' => '結束日期不能為空', - 'import10' => '結束日期資料錯誤', + 'import10' => '結束日期數據錯誤', 'import11' => '結束時間不能為空', - 'import12' => '結束時間資料錯誤', + 'import12' => '結束時間數據錯誤', 'import13' => '開始時間不能大於結束時間', 'import14' => '目前時段已有活動,不可上傳', 'import15' => '車位號碼不能為空', @@ -28,5 +28,9 @@ return [ 'import25' => '所屬樓層不存在', 'import26' => '所屬區域不存在', 'import27' => '車位屬性不存在', - 'import28' => '所屬樓層車位號碼已存在' + 'import28' => '所屬樓層車位號碼已存在', + 'import29' => '維修開始時間不能為空', + 'import30' => '維修開始時間數據錯誤', + 'import31' => '維修結束時間不能為空', + 'import32' => '維修結束時間數據錯誤' ];