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.
90 lines
2.6 KiB
90 lines
2.6 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class ParkingSpaceCamera extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'parking_space_camera';
|
|
|
|
protected $fillable
|
|
= [
|
|
'camera_id',
|
|
'space_index',
|
|
'space_id',
|
|
'control_lights_ip',
|
|
'light_index'
|
|
];
|
|
|
|
public static function createModel($data, $camera_id)
|
|
{
|
|
$createData = [];
|
|
$index = 1;
|
|
foreach ($data as $item) {
|
|
$createData[] = [
|
|
'camera_id' => $camera_id,
|
|
'space_index' => $index,
|
|
'space_id' => $item['space_id'],
|
|
'control_lights_ip' => $item['control_lights_ip'],
|
|
'light_index' => $item['light_index'] ?? 0
|
|
];
|
|
$index += 1;
|
|
}
|
|
return self::insert($createData);
|
|
}
|
|
|
|
public static function getData($camera_id, $is_space = 0): array
|
|
{
|
|
$data = self::query()->where('camera_id', $camera_id)->orderBy(
|
|
'space_index'
|
|
)->select()->get()->toArray();
|
|
if ($is_space) {
|
|
foreach ($data as &$item) {
|
|
$space = ParkingSpace::query()->findOrFail(
|
|
$item['space_id'],
|
|
['number', 'space_attr_id']
|
|
);
|
|
$item['space_number'] = $space['number'];
|
|
$item['space_attr_id'] = $space['space_attr_id'];
|
|
unset($item['space_id'], $item['id'], $item['camera_id']);
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function getCameraIds($parking_space_number): Collection|array
|
|
{
|
|
$space_id = ParkingSpace::getValueId($parking_space_number);
|
|
if ($space_id) {
|
|
return self::query()->where('space_id', $space_id)->pluck(
|
|
'camera_id'
|
|
);
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public static function getParkingSpaceNumber(
|
|
$camera_id,
|
|
$is_implode = 1
|
|
): string|array {
|
|
$space_ids = self::query()->where('camera_id', $camera_id)->pluck(
|
|
'space_id'
|
|
);
|
|
$space_number_arr = [];
|
|
foreach ($space_ids as $space_id) {
|
|
$space_number_arr[] = ParkingSpace::getNumber($space_id);
|
|
}
|
|
return $is_implode ? implode(',', $space_number_arr)
|
|
: $space_number_arr;
|
|
}
|
|
|
|
public static function getSpaceCount($camera_id): int
|
|
{
|
|
return self::query()->where('camera_id', $camera_id)->count();
|
|
}
|
|
}
|
|
|