停车场管理系统
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.
 
 

223 lines
6.1 KiB

<?php
namespace App\Services;
use App\Exceptions\CustomException;
use App\Models\ParkingCamera;
use App\Models\ParkingLicensePlate;
use App\Models\ParkingSpace;
use App\Models\ParkingSpaceCamera;
use App\Models\ParkingVipAccessRecord;
use App\Models\ParkingVipList;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class ParkingVipListService
{
/**
* @var OperationLogService
*/
private OperationLogService $logService;
/**
* 构造函数
* @param OperationLogService $logService
*/
public function __construct(OperationLogService $logService)
{
$this->logService = $logService;
$this->logService->menuTitle = 'vip_list';
}
/**
* @param array $data
* @throws CustomException
*/
public function createModel(array $data)
{
try {
DB::beginTransaction();
$ParkingLicensePlateService = new ParkingLicensePlateService($this->logService);
$license_id = $ParkingLicensePlateService->createLicenseId($data['license']);
if (ParkingVipList::query()->where('license_id', $license_id)
->exists()
) {
throw new CustomException(
__('service.admin_vip_list.license_exists')
);
}
$model = ParkingVipList::query()->create([
'license_id' => $license_id,
'user_id' => Auth::guard()->user()['id'],
'created_at' => get_datetime()
]);
$this->logService->logCreated($model, 'vip_list.create');
DB::commit();
return $model;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* @param array $data
* @param int $id
* @throws Exception
*/
public function updateModel(array $data, int $id)
{
try {
DB::beginTransaction();
$license = $data['license'];
$ParkingLicensePlateService = new ParkingLicensePlateService($this->logService);
$license_id = $ParkingLicensePlateService->createLicenseId($license);
// 验证
$existsWhere = [
['license_id', '=', $license_id],
['id', '<>', $id]
];
if (ParkingVipList::query()->where($existsWhere)->exists()) {
throw new Exception(__('service.admin_vip_list.license_exists'));
}
// 更新
$model = ParkingVipList::query()->findOrFail($id);
$oldValues = $model->toArray();
$model->update([
'license_id' => $license_id,
'user_id' => Auth::guard()->user()['id'],
'updated_at' => get_datetime()
]);
$this->logService->logUpdated($model, $oldValues, 'vip_list.update');
DB::commit();
return $model;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* @param $id
* @return bool
* @throws Exception
*/
public function deleteModel($id): bool
{
try {
DB::beginTransaction();
$model = ParkingVipList::query()->findOrFail($id);
$this->logService->logDeleted($model, 'vip_list.delete');
$model->delete();
DB::commit();
return true;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
// vip 车辆进入
public static function enter(
$space_id,
$space_type_id,
$access_record_id,
$enter_time,
$license_id,
$floor_id
) {
$vip_list_id = ParkingVipList::query()->where('license_id', $license_id)
->value('id');
if ($vip_list_id) {
if (!$space_type_id && $space_id) {
$space_type_id = ParkingSpace::query()->where('id', $space_id)
->value('space_type_id');
}
ParkingVipAccessRecord::enterModel([
'space_id' => $space_id,
'space_type_id' => $space_type_id,
'vip_list_id' => $vip_list_id,
'enter_time' => $enter_time,
'access_record_id' => $access_record_id,
]);
self::notice($space_id, $floor_id, $license_id, 'addDeviceVipNotice');
}
// 判断是否违停
self::isIllegal($license_id, $space_type_id, $space_id, $floor_id);
}
// vip 车辆离场
public static function leave($access_record_id, $leave_time)
{
$id = ParkingVipAccessRecord::query()->where(
'access_record_id',
$access_record_id
)->whereNull('leave_time')->value('id');
if ($id) {
ParkingVipAccessRecord::leaveModel(['leave_time' => $leave_time],
$id);
}
}
// 判断是否违停
protected static function isIllegal(
$license_id,
$space_type_id,
$space_id,
$floor_id
): void {
if ($space_type_id && $license_id) {
$exists = ParkingLicensePlate::query()->where([
'id' => $license_id,
'space_type_id' => $space_type_id
])->exists();
if ($exists) {
return;
}
self::notice($space_id, $floor_id, $license_id);
}
}
// 违泊、VIP 进入提醒
protected static function notice(
$space_id,
$floor_id,
$license_id,
$str = 'addDeviceIllegalNotice'
): void {
$camera_id = ParkingSpaceCamera::query()->where(
'space_id',
$space_id
)->value('camera_id');
$camera_ip = ParkingCamera::query()->where('id', $camera_id)
->value('camera_ip');
AdminNoticeService::$str(
$camera_id ?? 0,
$camera_ip ?? '',
$space_id,
$floor_id,
1,
$license_id
);
}
}