From 90679ed0ee066d5ba055721b9bf92f4db547729a Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Thu, 18 Jun 2026 14:43:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=A6=E4=BD=8D=E5=AF=BC=E5=85=A5=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E9=94=99=E8=AF=AF=E8=BF=94=E5=9B=9E=EF=BC=8C=E8=BD=A6?= =?UTF-8?q?=E4=BD=8D=E7=8A=B6=E6=80=81=E6=98=BE=E7=A4=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/ParkingSpaceController.php | 14 +++++-- app/Imports/ParkingSpaceImport.php | 38 +++++++++++++++++-- app/Services/ParkingSpaceService.php | 3 +- resources/lang/en/imports.php | 11 +++++- resources/lang/zh-CN/imports.php | 11 +++++- resources/lang/zh-TW/imports.php | 11 +++++- 6 files changed, 78 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Admin/ParkingSpaceController.php b/app/Http/Controllers/Admin/ParkingSpaceController.php index 4eda715..89a9f1e 100644 --- a/app/Http/Controllers/Admin/ParkingSpaceController.php +++ b/app/Http/Controllers/Admin/ParkingSpaceController.php @@ -309,7 +309,7 @@ class ParkingSpaceController extends BaseController $data['parking_space_number'] = $item['number']; $data['license_plate'] = ParkingLicensePlate::getNumber($item['license_plate_id']); - $data['status'] = $this->service->getParkingSpaceStatus($item['status']); + $data['status'] = $this->service->getParkingSpaceStatus($item['status'], $item['license_plate_id']); $data['parking_space_type'] = ParkingSpaceType::getName($item['space_type_id']); $data['berthing_time_str'] = ''; if ($item['berthing_time']) { @@ -602,6 +602,7 @@ class ParkingSpaceController extends BaseController * @param Request $request * @return JsonResponse * @throws ValidationException + * @throws CustomException */ public function import(Request $request): JsonResponse { @@ -627,18 +628,25 @@ class ParkingSpaceController extends BaseController // 4. 执行导入(使用存储后的绝对路径) // storage_path('app') 获取 storage/app 的绝对路径 + $model = new ParkingSpaceImport($this->adminUserId); Excel::import( - new ParkingSpaceImport($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 682d453..137eaaa 100644 --- a/app/Imports/ParkingSpaceImport.php +++ b/app/Imports/ParkingSpaceImport.php @@ -8,6 +8,7 @@ use App\Models\Parking; use App\Models\ParkingSpace; use App\Models\ParkingSpaceAttributes; use App\Models\ParkingSpaceType; +use App\Services\AdminTranslationService; use App\Services\OperationLogService; use App\Services\ParkingSpaceService; use Maatwebsite\Excel\Concerns\ToModel; @@ -15,8 +16,9 @@ use Maatwebsite\Excel\Concerns\WithChunkReading; class ParkingSpaceImport implements ToModel, WithChunkReading { - protected int $index = 1; + protected int $index = 0; protected int $user_id; + protected array $error = []; public function __construct($admin_user_id) { @@ -25,32 +27,42 @@ class ParkingSpaceImport implements ToModel, WithChunkReading public function model(array $row) { - if ($this->index == 1) { + if (!$this->index) { $this->index += 1; return; } + $this->index += 1; $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; } if (empty($floor)) { + $this->error[] = imports_error($this->index, 'import21'); return; } if (empty($region)) { + $this->error[] = imports_error($this->index, 'import22'); return; } if (empty($attr)) { + $this->error[] = imports_error($this->index, 'import23'); return; } if (empty($number)) { + $this->error[] = imports_error($this->index, 'import15'); return; } $parking_id = Parking::getValueId($parking); if (!$parking_id) { + $parking_id = AdminTranslationService::getTypeId(3, $parking); + if (!$parking_id) { + $this->error[] = imports_error($this->index, 'import24'); + } return; } $floor_id = AdminFloor::query()->where('status', 1)->where( @@ -61,15 +73,30 @@ class ParkingSpaceImport implements ToModel, WithChunkReading $parking_id )->value('id'); if (!$floor_id) { + $floor_id = AdminTranslationService::getTypeId(4, $floor); + if (!$floor_id) { + $this->error[] = imports_error($this->index, 'import25'); + } return; } $region_id = AdminFloorRegion::query()->where('status', 1)->where( 'floor_id', $floor_id )->where('name', $region)->value('id'); + if (!$region_id) { + $region_id = AdminTranslationService::getTypeId(5, $region); + if (!$region_id) { + $this->error[] = imports_error($this->index, 'import26'); + } + return; + } $attr_id = ParkingSpaceAttributes::query()->where('attributes', $attr) ->value('id'); if (!$attr_id) { + $attr_id = AdminTranslationService::getTypeId(2, $attr); + if (!$attr_id) { + $this->error[] = imports_error($this->index, 'import27'); + } return; } @@ -79,6 +106,7 @@ class ParkingSpaceImport implements ToModel, WithChunkReading 'region_id' => $region_id ]; if (ParkingSpace::query()->where($where)->exists()) { + $this->error[] = imports_error($this->index, 'import28'); return; } $create['space_type_id'] = 0; @@ -92,9 +120,13 @@ class ParkingSpaceImport implements ToModel, WithChunkReading $service->createData($create); } - public function chunkSize(): int { return 1000; // 设置每次处理的行数,有助于避免内存问题并可能改善表头解析 } + + public function errorArr(): array + { + return $this->error; + } } diff --git a/app/Services/ParkingSpaceService.php b/app/Services/ParkingSpaceService.php index 98892df..afc3e0b 100644 --- a/app/Services/ParkingSpaceService.php +++ b/app/Services/ParkingSpaceService.php @@ -143,7 +143,8 @@ class ParkingSpaceService extends BaseService $item['recognition'] ); $item['status'] = $this->getParkingSpaceStatus( - $item['status'] + $item['status'], + $item['license_plate_id'] ); $item['operation_type'] = $this->getOperationType( $item['operation_type'] diff --git a/resources/lang/en/imports.php b/resources/lang/en/imports.php index 5f16e71..a7601cc 100644 --- a/resources/lang/en/imports.php +++ b/resources/lang/en/imports.php @@ -19,5 +19,14 @@ return [ 'import16' => 'Parking space type cannot be empty', 'import17' => 'Parking space number does not exist', 'import18' => 'Parking space type does not exist', - 'import19' => 'The mode name cannot be empty.' + 'import19' => 'The mode name cannot be empty.', + 'import20' => 'The parking lot cannot be empty.', + 'import21' => 'The floor number cannot be empty.', + 'import22' => 'The region cannot be empty.', + 'import23' => 'Parking space attributes cannot be empty.', + 'import24' => 'Parking lot does not exist', + '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.' ]; diff --git a/resources/lang/zh-CN/imports.php b/resources/lang/zh-CN/imports.php index 07b62c7..a9fd64f 100644 --- a/resources/lang/zh-CN/imports.php +++ b/resources/lang/zh-CN/imports.php @@ -19,5 +19,14 @@ return [ 'import16' => '车位类型不能为空', 'import17' => '车位号码不存在', 'import18' => '车位类型不存在', - 'import19' => '模式名称不能为空' + 'import19' => '模式名称不能为空', + 'import20' => '停车场不能为空', + 'import21' => '所属楼层不能为空', + 'import22' => '所属区域不能为空', + 'import23' => '车位属性不能为空', + 'import24' => '停车场不存在', + 'import25' => '所属楼层不存在', + 'import26' => '所属区域不存在', + 'import27' => '车位属性不存在', + 'import28' => '所属楼层车位号已存在' ]; diff --git a/resources/lang/zh-TW/imports.php b/resources/lang/zh-TW/imports.php index 90162ad..cb56e29 100644 --- a/resources/lang/zh-TW/imports.php +++ b/resources/lang/zh-TW/imports.php @@ -19,5 +19,14 @@ return [ 'import16' => '車位類型不能為空', 'import17' => '車位號碼不存在', 'import18' => '車位類型不存在', - 'import19' => '模式名稱不能為空' + 'import19' => '模式名稱不能為空', + 'import20' => '停車場不能為空', + 'import21' => '所屬樓層不能為空', + 'import22' => '所屬區域不能為空', + 'import23' => '車位屬性不能為空', + 'import24' => '停車場不存在', + 'import25' => '所屬樓層不存在', + 'import26' => '所屬區域不存在', + 'import27' => '車位屬性不存在', + 'import28' => '所屬樓層車位號碼已存在' ];