8 changed files with 206 additions and 1 deletions
@ -0,0 +1,31 @@ |
|||
<?php |
|||
|
|||
namespace App\Exports; |
|||
|
|||
use Maatwebsite\Excel\Concerns\FromArray; |
|||
|
|||
class ParkingSpaceTemplateExport implements FromArray |
|||
{ |
|||
public function array(): array |
|||
{ |
|||
return [ |
|||
[ |
|||
__exports('global.index'), |
|||
__exports('whitelist.parking'), |
|||
__exports('parking_space.floor'), |
|||
__exports('parking_space.region'), |
|||
__exports('parking_space.space_attr'), |
|||
__exports('parking_space.number') |
|||
], |
|||
[ |
|||
'1', |
|||
'MSCP', |
|||
'L1', |
|||
'A', |
|||
'YUC', |
|||
'L1-0001' |
|||
] |
|||
]; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,97 @@ |
|||
<?php |
|||
|
|||
namespace App\Imports; |
|||
|
|||
use App\Models\AdminFloor; |
|||
use App\Models\AdminFloorRegion; |
|||
use App\Models\Parking; |
|||
use App\Models\ParkingSpace; |
|||
use App\Models\ParkingSpaceAttributes; |
|||
use App\Models\ParkingSpaceType; |
|||
use App\Services\OperationLogService; |
|||
use App\Services\ParkingSpaceService; |
|||
use Maatwebsite\Excel\Concerns\ToModel; |
|||
use Maatwebsite\Excel\Concerns\WithChunkReading; |
|||
|
|||
class ParkingSpaceImport implements ToModel, WithChunkReading |
|||
{ |
|||
protected int $index = 1; |
|||
protected int $user_id; |
|||
|
|||
public function __construct($admin_user_id) |
|||
{ |
|||
$this->user_id = $admin_user_id; |
|||
} |
|||
|
|||
public function model(array $row) |
|||
{ |
|||
if ($this->index == 1) { |
|||
$this->index += 1; |
|||
return; |
|||
} |
|||
$parking = $row[1]; |
|||
$floor = $row[2]; |
|||
$region = $row[3]; |
|||
$attr = $row[4]; |
|||
$number = $row[5]; |
|||
if (empty($parking)) { |
|||
return; |
|||
} |
|||
if (empty($floor)) { |
|||
return; |
|||
} |
|||
if (empty($region)) { |
|||
return; |
|||
} |
|||
if (empty($attr)) { |
|||
return; |
|||
} |
|||
if (empty($number)) { |
|||
return; |
|||
} |
|||
$parking_id = Parking::getValueId($parking); |
|||
if (!$parking_id) { |
|||
return; |
|||
} |
|||
$floor_id = AdminFloor::query()->where('status', 1)->where( |
|||
'building_floor', |
|||
$parking_id |
|||
)->value('id'); |
|||
if (!$floor_id) { |
|||
return; |
|||
} |
|||
$region_id = AdminFloorRegion::query()->where('status', 1)->where( |
|||
'floor_id', |
|||
$floor_id |
|||
)->value('id'); |
|||
$attr_id = ParkingSpaceAttributes::query()->where('attributes', $attr) |
|||
->value('id'); |
|||
if (!$attr_id) { |
|||
return; |
|||
} |
|||
|
|||
$create = $where = [ |
|||
'number' => $number, |
|||
'floor_id' => $floor_id, |
|||
'region_id' => $region_id |
|||
]; |
|||
if (ParkingSpace::query()->where($where)->exists()) { |
|||
return; |
|||
} |
|||
$create['space_type_id'] = 0; |
|||
$res = ParkingSpaceType::getDefaultData(); |
|||
if ($res) { |
|||
$create['space_type_id'] = $res['id']; |
|||
} |
|||
$create['space_attr_id'] = $attr_id; |
|||
|
|||
$service = new ParkingSpaceService(new OperationLogService()); |
|||
$service->createData($create); |
|||
} |
|||
|
|||
|
|||
public function chunkSize(): int |
|||
{ |
|||
return 1000; // 设置每次处理的行数,有助于避免内存问题并可能改善表头解析 |
|||
} |
|||
} |
|||
Loading…
Reference in new issue