diff --git a/app/Http/Controllers/Admin/ParkingPatternController.php b/app/Http/Controllers/Admin/ParkingPatternController.php
index db76c71..a5b5f15 100644
--- a/app/Http/Controllers/Admin/ParkingPatternController.php
+++ b/app/Http/Controllers/Admin/ParkingPatternController.php
@@ -248,6 +248,7 @@ class ParkingPatternController extends BaseController
* @param Request $request
* @return JsonResponse
* @throws ValidationException
+ * @throws CustomException
*/
public function batchImport(Request $request): JsonResponse
{
@@ -273,18 +274,25 @@ class ParkingPatternController extends BaseController
// 4. 执行导入(使用存储后的绝对路径)
// storage_path('app') 获取 storage/app 的绝对路径
+ $model = new ParkingPatternSpaceImport($this->adminUserId);
Excel::import(
- new ParkingPatternSpaceImport($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(
@@ -299,6 +307,7 @@ class ParkingPatternController extends BaseController
* @param Request $request
* @return JsonResponse
* @throws ValidationException
+ * @throws CustomException
*/
public function import(Request $request): JsonResponse
{
@@ -338,18 +347,25 @@ class ParkingPatternController extends BaseController
// 4. 执行导入(使用存储后的绝对路径)
// storage_path('app') 获取 storage/app 的绝对路径
+ $model = new ParkingPatternImport($model_name, $this->adminUserId, $en_name, $tw_name);
Excel::import(
- new ParkingPatternImport($model_name, $this->adminUserId, $en_name, $tw_name),
+ $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/ParkingPatternImport.php b/app/Imports/ParkingPatternImport.php
index 0fd713d..df55391 100644
--- a/app/Imports/ParkingPatternImport.php
+++ b/app/Imports/ParkingPatternImport.php
@@ -12,7 +12,8 @@ class ParkingPatternImport implements ToModel
protected string $en_name;
protected string $tw_name;
protected string $user_id;
- protected int $index = 1;
+ protected int $index = 0;
+ protected array $error = [];
public function __construct(
string $model_name,
@@ -31,23 +32,36 @@ class ParkingPatternImport implements ToModel
*/
public function model(array $row)
{
- if ($this->index == 1) {
+ if (!$this->index) {
$this->index += 1;
return;
}
- if (!empty($row[1])
- && !empty($row[2])
- ) {
- $data = [
- 'model_name' => $this->model_name,
- 'admin_user_id' => $this->user_id,
- 'parking_space_number' => $row[1],
- 'parking_space_type' => $row[2],
- 'en_name' => $this->en_name,
- 'tw_name' => $this->tw_name
- ];
- $service = new ParkingPatternService(new OperationLogService());
- $service->saveModel($data);
+ $this->index += 1;
+ if (empty($row[1])) {
+ $this->error[] = imports_error($this->index, 'import15');
+ return;
+ }
+ if (empty($row[2])) {
+ $this->error[] = imports_error($this->index, 'import16');
+ return;
+ }
+ $data = [
+ 'model_name' => $this->model_name,
+ 'admin_user_id' => $this->user_id,
+ 'parking_space_number' => $row[1],
+ 'parking_space_type' => $row[2],
+ 'en_name' => $this->en_name,
+ 'tw_name' => $this->tw_name
+ ];
+ $service = new ParkingPatternService(new OperationLogService());
+ $error_str = $service->saveModel($data);
+ if ($error_str) {
+ $this->error[] = imports_error($this->index, $error_str);
}
}
+
+ public function errorArr(): array
+ {
+ return $this->error;
+ }
}
diff --git a/app/Imports/ParkingPatternSpaceImport.php b/app/Imports/ParkingPatternSpaceImport.php
index 0ffbfd1..fb856b7 100644
--- a/app/Imports/ParkingPatternSpaceImport.php
+++ b/app/Imports/ParkingPatternSpaceImport.php
@@ -9,7 +9,8 @@ use Maatwebsite\Excel\Concerns\ToModel;
class ParkingPatternSpaceImport implements ToModel
{
protected string $user_id;
- protected int $index = 1;
+ protected int $index = 0;
+ protected array $error = [];
public function __construct(string $user_id)
{
@@ -21,19 +22,38 @@ class ParkingPatternSpaceImport implements ToModel
*/
public function model(array $row)
{
- if ($this->index == 1) {
+ if (!$this->index) {
$this->index += 1;
return;
}
- if (!empty($row[1]) && !empty($row[2]) && !empty($row[3])) {
- $data = [
- 'model_name' => $row[1],
- 'admin_user_id' => $this->user_id,
- 'parking_space_number' => $row[2],
- 'parking_space_type' => $row[3]
- ];
- $service = new ParkingPatternService(new OperationLogService());
- $service->saveModel($data);
+ $this->index += 1;
+ if (empty($row[1])) {
+ $this->error[] = imports_error($this->index, 'import19');
+ return;
+ }
+ if (empty($row[2])) {
+ $this->error[] = imports_error($this->index, 'import15');
+ return;
+ }
+ if (empty($row[3])) {
+ $this->error[] = imports_error($this->index, 'import16');
+ return;
}
+ $data = [
+ 'model_name' => $row[1],
+ 'admin_user_id' => $this->user_id,
+ 'parking_space_number' => $row[2],
+ 'parking_space_type' => $row[3]
+ ];
+ $service = new ParkingPatternService(new OperationLogService());
+ $error_str = $service->saveModel($data);
+ if ($error_str) {
+ $this->error[] = imports_error($this->index, $error_str);
+ }
+ }
+
+ public function errorArr(): array
+ {
+ return $this->error;
}
}
diff --git a/app/Services/ParkingPatternService.php b/app/Services/ParkingPatternService.php
index 298b2f6..bf85577 100644
--- a/app/Services/ParkingPatternService.php
+++ b/app/Services/ParkingPatternService.php
@@ -154,9 +154,9 @@ class ParkingPatternService extends BaseService
/**
* 保存
* @param $data
- * @return bool
+ * @return string
*/
- public function saveModel($data): bool
+ public function saveModel($data): string
{
try {
DB::beginTransaction();
@@ -188,7 +188,7 @@ class ParkingPatternService extends BaseService
// 验证
$parking_space_id = ParkingSpace::getValueId($parking_space_number);
if (empty($parking_space_id)) {
- throw new Exception('');
+ return 'import17';
}
$parking_space_type_id = ParkingSpaceType::getValueId(
$parking_space_type
@@ -196,7 +196,7 @@ class ParkingPatternService extends BaseService
if (empty($parking_space_type_id)) {
$parking_space_type_id = AdminTranslationService::getTypeId(1, $parking_space_type);
if (!$parking_space_type_id) {
- throw new Exception('');
+ return 'import18';
}
}
@@ -226,10 +226,10 @@ class ParkingPatternService extends BaseService
}
DB::commit();
- return true;
+ return '';
} catch (Exception $e) {
DB::rollBack();
- return false;
+ return '';
}
}
diff --git a/resources/lang/en/imports.php b/resources/lang/en/imports.php
index 3662386..5f16e71 100644
--- a/resources/lang/en/imports.php
+++ b/resources/lang/en/imports.php
@@ -14,5 +14,10 @@ return [
'import11' => 'The end time cannot be empty.',
'import12' => 'End time data error',
'import13' => 'The start time cannot be greater than the end time.',
- 'import14' => 'There is already an activity running in the current time period, so uploading is not allowed.'
+ 'import14' => 'There is already an activity running in the current time period, so uploading is not allowed.',
+ 'import15' => 'Parking space number cannot be empty',
+ '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.'
];
diff --git a/resources/lang/zh-CN/imports.php b/resources/lang/zh-CN/imports.php
index 078ccd6..07b62c7 100644
--- a/resources/lang/zh-CN/imports.php
+++ b/resources/lang/zh-CN/imports.php
@@ -14,5 +14,10 @@ return [
'import11' => '结束时间不能为空',
'import12' => '结束时间数据错误',
'import13' => '开始时间不能大于结束时间',
- 'import14' => '当前时间段已有活动,不可上传'
+ 'import14' => '当前时间段已有活动,不可上传',
+ 'import15' => '车位号码不能为空',
+ 'import16' => '车位类型不能为空',
+ 'import17' => '车位号码不存在',
+ 'import18' => '车位类型不存在',
+ 'import19' => '模式名称不能为空'
];
diff --git a/resources/lang/zh-TW/imports.php b/resources/lang/zh-TW/imports.php
index 977e2f6..faa81eb 100644
--- a/resources/lang/zh-TW/imports.php
+++ b/resources/lang/zh-TW/imports.php
@@ -14,5 +14,10 @@ return [
'import11' => '結束時間不能為空',
'import12' => '結束時間資料錯誤',
'import13' => '開始時間不能大於結束時間',
- 'import14' => '目前時段已有活動,不可上傳'
+ 'import14' => '目前時段已有活動,不可上傳',
+ 'import15' => '车位号码不能为空',
+ 'import16' => '车位类型不能为空',
+ 'import17' => '车位号码不存在',
+ 'import18' => '车位类型不存在',
+ 'import19' => '模式名称不能为空'
];