diff --git a/app/Imports/ParkingSpaceRepairImport.php b/app/Imports/ParkingSpaceRepairImport.php index 4eeeeb3..7cbbd75 100644 --- a/app/Imports/ParkingSpaceRepairImport.php +++ b/app/Imports/ParkingSpaceRepairImport.php @@ -76,12 +76,15 @@ class ParkingSpaceRepairImport implements ToModel, WithChunkReading } $service = new ParkingSpaceRepairService(new OperationLogService()); - $service->createModel([ + $errorRes = $service->createModel([ 'admin_user_id' => $this->user_id, 'parking_space_name' => $space_number, 'start_at' => date("Y-m-d H:i:s", $start_times), 'end_at' => date("Y-m-d H:i:s", $end_times) - ]); + ], true); + if ($errorRes) { + $this->error[] = imports_error($this->index, $errorRes, true); + } } public function chunkSize(): int diff --git a/app/Services/ParkingSpaceRepairService.php b/app/Services/ParkingSpaceRepairService.php index fcfd8d2..2953d53 100644 --- a/app/Services/ParkingSpaceRepairService.php +++ b/app/Services/ParkingSpaceRepairService.php @@ -54,9 +54,11 @@ class ParkingSpaceRepairService extends BaseService /** * @param array $data - * @throws Exception + * @param bool $is_import + * @return string + * @throws CustomException */ - public function createModel(array $data) + public function createModel(array $data, bool $is_import = false): string { try { DB::beginTransaction(); @@ -66,6 +68,11 @@ class ParkingSpaceRepairService extends BaseService $space = ParkingSpace::query()->where('number', $parking_space_name)->first(); $str = $count > 1 ? ':' . $parking_space_name : ''; if (!$space) { + if ($is_import) { + DB::rollBack(); + return __service($this->menuTitle . '.space_not_exists') + . $str; + } throw new CustomException( __service($this->menuTitle . '.space_not_exists') . $str @@ -75,12 +82,16 @@ class ParkingSpaceRepairService extends BaseService $space = $space->toArray(); $space_id = $space['id']; if ($space['status'] == 2) { + if ($is_import) { + DB::rollBack(); + return __service($this->menuTitle . '.space_exists') + . $str; + } throw new CustomException( - __service($this->menuTitle . '.space_exists') - . $str + __service($this->menuTitle . '.space_exists') . $str ); } - $model = $this->createData( + $this->createData( $space_id, $data['admin_user_id'], $data['start_at'], @@ -89,7 +100,7 @@ class ParkingSpaceRepairService extends BaseService } DB::commit(); - return $model; + return ''; } catch (Exception $e) { DB::rollBack(); throw $e; diff --git a/app/common.php b/app/common.php index 152c510..5d817fb 100644 --- a/app/common.php +++ b/app/common.php @@ -255,11 +255,15 @@ if (!function_exists('__imports')) { } if (!function_exists('imports_error')) { - function imports_error($index, $str): string + function imports_error($index, $str, $is = false): string { $str1 = __imports('import1'); $str2 = __imports('import2'); - $str3 = __imports($str); + if ($is){ + $str3 = $str; + } else { + $str3 = __imports($str); + } return $str1 . $index . $str2 . ' : ' . $str3; } }