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.
108 lines
3.4 KiB
108 lines
3.4 KiB
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Models\Parking;
|
|
use App\Models\ParkingChannel;
|
|
use App\Models\ParkingSpaceType;
|
|
use App\Services\AdminTranslationService;
|
|
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 = 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;
|
|
if (!$row[1] && !$row[2] && !$row[3] && !$row[4]) {
|
|
return;
|
|
}
|
|
$license_plate = $row[1] ?? '';
|
|
if (empty($license_plate)) {
|
|
$this->error[] = imports_error($this->index, 'import33');
|
|
return;
|
|
}
|
|
$parking_name = $row[2] ?? '';
|
|
if (empty($parking_name)) {
|
|
$this->error[] = imports_error($this->index, 'import20');
|
|
return;
|
|
}
|
|
$parking_id = Parking::getValueId($parking_name);
|
|
if (empty($parking_id)) {
|
|
$parking_id = AdminTranslationService::getTypeId(3, $parking_name);
|
|
if (!$parking_id) {
|
|
$this->error[] = imports_error($this->index, 'import24');
|
|
return;
|
|
}
|
|
}
|
|
$channel_name = $row[3] ?? '';
|
|
if (empty($channel_name)) {
|
|
$this->error[] = imports_error($this->index, 'import34');
|
|
return;
|
|
}
|
|
$channel_arr = explode(',', $channel_name);
|
|
$channelIds = ParkingChannel::query()->whereIn('name', $channel_arr)->pluck('id')->toArray();
|
|
if (empty($channelIds)) {
|
|
$channelIds = [];
|
|
foreach ($channel_arr as $channel) {
|
|
$channel_id = AdminTranslationService::getTypeId(5, $channel);
|
|
if ($channel_id) {
|
|
$channelIds[] = $channel_id;
|
|
}
|
|
}
|
|
if (!$channelIds) {
|
|
$this->error[] = imports_error($this->index, 'import35');
|
|
return;
|
|
}
|
|
}
|
|
$reason = $row[4] ?? '';
|
|
if (empty($reason)) {
|
|
$this->error[] = imports_error($this->index, 'import36');
|
|
return;
|
|
}
|
|
$type_str = $row[5] ?? '';
|
|
$space_type_id = 0;
|
|
if (!empty($type_str)) {
|
|
$space_type_id = ParkingSpaceType::getValueId($type_str) ?? 0;
|
|
if (!$space_type_id) {
|
|
$space_type_id = AdminTranslationService::getTypeId(1, $type_str) ?? 0;
|
|
}
|
|
}
|
|
|
|
$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);
|
|
}
|
|
|
|
public function chunkSize(): int
|
|
{
|
|
return 1000; // 设置每次处理的行数,有助于避免内存问题并可能改善表头解析
|
|
}
|
|
|
|
public function errorArr(): array
|
|
{
|
|
return $this->error;
|
|
}
|
|
}
|
|
|