停车场管理系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

132 lines
3.9 KiB

<?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\AdminTranslationService;
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 = 0;
protected int $user_id;
protected array $error = [];
public function __construct($admin_user_id)
{
$this->user_id = $admin_user_id;
}
public function model(array $row)
{
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(
'name',
$floor
)->where(
'building_floor',
$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;
}
$create = $where = [
'number' => $number,
'floor_id' => $floor_id,
'region_id' => $region_id
];
if (ParkingSpace::query()->where($where)->exists()) {
$this->error[] = imports_error($this->index, 'import28');
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; // 设置每次处理的行数,有助于避免内存问题并可能改善表头解析
}
public function errorArr(): array
{
return $this->error;
}
}