Browse Source

车位导入字段错误返回,车位状态显示优化

master
wanghongjun 1 day ago
parent
commit
90679ed0ee
  1. 14
      app/Http/Controllers/Admin/ParkingSpaceController.php
  2. 38
      app/Imports/ParkingSpaceImport.php
  3. 3
      app/Services/ParkingSpaceService.php
  4. 11
      resources/lang/en/imports.php
  5. 11
      resources/lang/zh-CN/imports.php
  6. 11
      resources/lang/zh-TW/imports.php

14
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("<br>", $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(

38
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;
}
}

3
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']

11
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.'
];

11
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' => '所属楼层车位号已存在'
];

11
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' => '所屬樓層車位號碼已存在'
];

Loading…
Cancel
Save