Browse Source

白名单车牌导入,车牌管理导入返回报错

master
wanghongjun 5 days ago
parent
commit
21246cacc9
  1. 12
      app/Http/Controllers/Admin/ParkingLicensePlateController.php
  2. 12
      app/Http/Controllers/Admin/VipListController.php
  3. 21
      app/Imports/ParkingLicensePlateImport.php
  4. 27
      app/Imports/ParkingVipListImport.php
  5. 3
      resources/lang/en/imports.php
  6. 3
      resources/lang/zh-CN/imports.php
  7. 3
      resources/lang/zh-TW/imports.php

12
app/Http/Controllers/Admin/ParkingLicensePlateController.php

@ -226,6 +226,7 @@ class ParkingLicensePlateController extends BaseController
* @param Request $request * @param Request $request
* @return JsonResponse * @return JsonResponse
* @throws ValidationException * @throws ValidationException
* @throws CustomException
*/ */
public function import(Request $request): JsonResponse public function import(Request $request): JsonResponse
{ {
@ -272,18 +273,25 @@ class ParkingLicensePlateController extends BaseController
// 4. 执行导入(使用存储后的绝对路径) // 4. 执行导入(使用存储后的绝对路径)
// storage_path('app') 获取 storage/app 的绝对路径 // storage_path('app') 获取 storage/app 的绝对路径
$model = new ParkingLicensePlateImport($parking_space_type);
Excel::import( Excel::import(
new ParkingLicensePlateImport($parking_space_type), $model,
storage_path('app/' . $path) storage_path('app/' . $path)
); );
// 5. (可选)导入完成后删除临时文件 // 5. (可选)导入完成后删除临时文件
Storage::delete($path); Storage::delete($path);
// 6. 返回错误行
$error_arr = $model->errorArr();
if ($error_arr) {
throw new CustomException(implode("<br>", $error_arr));
}
return $this->responseService->success( return $this->responseService->success(
__('controller.import.success') __('controller.import.success')
); );
} catch (ValidationException $e) { } catch (ValidationException|CustomException $e) {
throw $e; throw $e;
} catch (Exception $e) { } catch (Exception $e) {
return $this->responseService->systemError( return $this->responseService->systemError(

12
app/Http/Controllers/Admin/VipListController.php

@ -169,6 +169,7 @@ class VipListController extends BaseController
* @param Request $request * @param Request $request
* @return JsonResponse * @return JsonResponse
* @throws ValidationException * @throws ValidationException
* @throws CustomException
*/ */
public function import(Request $request): JsonResponse public function import(Request $request): JsonResponse
{ {
@ -194,18 +195,25 @@ class VipListController extends BaseController
// 4. 执行导入(使用存储后的绝对路径) // 4. 执行导入(使用存储后的绝对路径)
// storage_path('app') 获取 storage/app 的绝对路径 // storage_path('app') 获取 storage/app 的绝对路径
$model = new ParkingVipListImport();
Excel::import( Excel::import(
new ParkingVipListImport(), $model,
storage_path('app/' . $path) storage_path('app/' . $path)
); );
// 5. (可选)导入完成后删除临时文件 // 5. (可选)导入完成后删除临时文件
Storage::delete($path); Storage::delete($path);
// 6. 返回错误行
$error_arr = $model->errorArr();
if ($error_arr) {
throw new CustomException(implode("<br>", $error_arr));
}
return $this->responseService->success( return $this->responseService->success(
__('controller.import.success') __('controller.import.success')
); );
} catch (ValidationException $e) { } catch (ValidationException|CustomException $e) {
throw $e; throw $e;
} catch (Exception $e) { } catch (Exception $e) {
return $this->responseService->systemError( return $this->responseService->systemError(

21
app/Imports/ParkingLicensePlateImport.php

@ -9,7 +9,8 @@ use Maatwebsite\Excel\Concerns\ToModel;
class ParkingLicensePlateImport implements ToModel class ParkingLicensePlateImport implements ToModel
{ {
protected string $parkingSpaceType; protected string $parkingSpaceType;
protected int $index = 1; protected int $index = 0;
protected array $error = [];
/** /**
* @var OperationLogService * @var OperationLogService
@ -32,15 +33,22 @@ class ParkingLicensePlateImport implements ToModel
$this->index += 1; $this->index += 1;
return; return;
} }
if (isset($row[1]) && $row[1]) { $this->index += 1;
$number = $row[1]; $number = $row[1] ?? '';
if (empty($number)) {
$this->error[] = imports_error($this->index, 'import33');
return;
}
$where = [ $where = [
'number' => $number, 'number' => $number,
'space_type_id' => $this->parkingSpaceType 'space_type_id' => $this->parkingSpaceType
]; ];
if (!ParkingLicensePlate::query()->where($where) if (ParkingLicensePlate::query()->where($where)
->exists() ->exists()
) { ) {
$this->error[] = imports_error($this->index, 'import37');
return;
}
$model = ParkingLicensePlate::query()->create([ $model = ParkingLicensePlate::query()->create([
'number' => $number, 'number' => $number,
'space_type_id' => $this->parkingSpaceType, 'space_type_id' => $this->parkingSpaceType,
@ -48,6 +56,9 @@ class ParkingLicensePlateImport implements ToModel
]); ]);
$this->logService->logCreated($model, 'license_plate.create'); $this->logService->logCreated($model, 'license_plate.create');
} }
}
public function errorArr(): array
{
return $this->error;
} }
} }

27
app/Imports/ParkingVipListImport.php

@ -11,11 +11,12 @@ use Maatwebsite\Excel\Concerns\ToModel;
class ParkingVipListImport implements ToModel class ParkingVipListImport implements ToModel
{ {
protected int $index = 0;
protected array $error = [];
/** /**
* @var OperationLogService * @var OperationLogService
*/ */
private OperationLogService $logService; private OperationLogService $logService;
protected int $index = 1;
public function __construct() public function __construct()
{ {
@ -28,16 +29,25 @@ class ParkingVipListImport implements ToModel
*/ */
public function model(array $row) public function model(array $row)
{ {
if ($this->index == 1) { if (!$this->index) {
$this->index += 1; $this->index += 1;
return; return;
} }
if (isset($row[1]) && $row[1]) { $this->index += 1;
$license = $row[1]; $license = $row[1] ?? '';
$license_id = (new ParkingLicensePlateService($this->logService))->createLicenseId($license); if (empty($license)) {
if (!ParkingVipList::query()->where('license_id', $license_id) $this->error[] = imports_error($this->index, 'import33');
return;
}
$license_id = (new ParkingLicensePlateService(
$this->logService
))->createLicenseId($license);
if (ParkingVipList::query()->where('license_id', $license_id)
->exists() ->exists()
) { ) {
$this->error[] = imports_error($this->index, 'import37');
return;
}
$model = ParkingVipList::query()->create([ $model = ParkingVipList::query()->create([
'license_id' => $license_id, 'license_id' => $license_id,
'user_id' => Auth::guard('sanctum')->user()['id'], 'user_id' => Auth::guard('sanctum')->user()['id'],
@ -45,6 +55,9 @@ class ParkingVipListImport implements ToModel
]); ]);
$this->logService->logCreated($model, 'vip_list.create'); $this->logService->logCreated($model, 'vip_list.create');
} }
}
public function errorArr(): array
{
return $this->error;
} }
} }

3
resources/lang/en/imports.php

@ -36,5 +36,6 @@ return [
'import33' => 'License plate number cannot be empty', 'import33' => 'License plate number cannot be empty',
'import34' => 'The lane cannot be empty.', 'import34' => 'The lane cannot be empty.',
'import35' => 'Lane does not exist', 'import35' => 'Lane does not exist',
'import36' => 'The reason for passage cannot be empty.' 'import36' => 'The reason for passage cannot be empty.',
'import37' => 'The license plate number already exists.'
]; ];

3
resources/lang/zh-CN/imports.php

@ -36,5 +36,6 @@ return [
'import33' => '车牌号码不能为空', 'import33' => '车牌号码不能为空',
'import34' => '车道不能为空', 'import34' => '车道不能为空',
'import35' => '车道不存在', 'import35' => '车道不存在',
'import36' => '通行原因不能为空' 'import36' => '通行原因不能为空',
'import37' => '车牌号码已存在'
]; ];

3
resources/lang/zh-TW/imports.php

@ -36,5 +36,6 @@ return [
'import33' => '車牌號碼不能為空', 'import33' => '車牌號碼不能為空',
'import34' => '車道不能為空', 'import34' => '車道不能為空',
'import35' => '車道不存在', 'import35' => '車道不存在',
'import36' => '通行原因不能為空' 'import36' => '通行原因不能為空',
'import37' => '車牌號碼已存在'
]; ];

Loading…
Cancel
Save