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.
77 lines
2.1 KiB
77 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Models\Parking;
|
|
use App\Models\ParkingChannel;
|
|
use App\Models\ParkingSpaceType;
|
|
use App\Services\OperationLogService;
|
|
use App\Services\ParkingWhitelistService;
|
|
use Maatwebsite\Excel\Concerns\ToModel;
|
|
use Maatwebsite\Excel\Concerns\WithChunkReading;
|
|
class ParkingWhitelistImport 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;
|
|
}
|
|
$license_plate = $row[1];
|
|
if (empty($license_plate)) {
|
|
return;
|
|
}
|
|
$parking_name = $row[2];
|
|
if (empty($parking_name)) {
|
|
return;
|
|
}
|
|
$parking_id = Parking::getValueId($parking_name);
|
|
if (empty($parking_id)) {
|
|
return;
|
|
}
|
|
$channel_name = $row[3];
|
|
if (empty($channel_name)) {
|
|
return;
|
|
}
|
|
$channel_arr = explode(',', $channel_name);
|
|
$channelIds = ParkingChannel::query()->whereIn('name', $channel_arr)->pluck('id')->toArray();
|
|
if (empty($channelIds)) {
|
|
return;
|
|
}
|
|
$reason = $row[4];
|
|
if (empty($reason)) {
|
|
return;
|
|
}
|
|
$type_str = $row[5];
|
|
$space_type_id = 0;
|
|
if (!empty($type_str)) {
|
|
$space_type_id = ParkingSpaceType::getValueId($type_str);
|
|
}
|
|
|
|
$data = [
|
|
'license_plate' => $license_plate,
|
|
'parking_id' => $parking_id,
|
|
'reason' => $reason,
|
|
'channel_ids' => $channelIds,
|
|
'admin_user_id' => $this->user_id,
|
|
'member_type' => $space_type_id
|
|
];
|
|
|
|
$service = new ParkingWhitelistService(new OperationLogService());
|
|
$service->createModel($data);
|
|
$this->index += 1;
|
|
}
|
|
|
|
public function chunkSize(): int
|
|
{
|
|
return 1000; // 设置每次处理的行数,有助于避免内存问题并可能改善表头解析
|
|
}
|
|
}
|
|
|