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.
177 lines
5.4 KiB
177 lines
5.4 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AdminFloorRegion;
|
|
use App\Models\ParkingInformation;
|
|
use App\Models\ParkingLicensePlate;
|
|
use App\Models\ParkingSpace;
|
|
use App\Models\ParkingSpaceType;
|
|
use Exception;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ParkingInformationService extends BaseService
|
|
{
|
|
|
|
public function __construct(OperationLogService $logService)
|
|
{
|
|
parent::__construct($logService);
|
|
$logService->menuTitle = 'parking_information';
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $data
|
|
* @return Builder|Model
|
|
* @throws Exception
|
|
*/
|
|
public function createModel($data): Builder|Model
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$license_plate = $data['license_plate'];
|
|
$license_plate = ParkingLicensePlate::query()->where(
|
|
'number',
|
|
$license_plate
|
|
)->first(['id', 'space_type_id']);
|
|
$license_plate_id = 0;
|
|
if ($license_plate) {
|
|
$license_plate_id = $license_plate['id'];
|
|
$space_type_id = $license_plate['space_type_id'];
|
|
} else {
|
|
$space_type_data = ParkingSpaceType::getDefaultData();
|
|
$space_type_id = $space_type_data['id'] ?? 0;
|
|
}
|
|
|
|
$space_id = 0;
|
|
$floor_region_id = 0;
|
|
// 通过车位类型查询楼层 区域 车位
|
|
if ($space_type_id) {
|
|
$parkingSpaceWhere = [
|
|
'space_type_id' => $space_type_id,
|
|
'status' => 0,
|
|
'license_plate_id' => 0
|
|
];
|
|
$res = ParkingSpace::query()
|
|
->where($parkingSpaceWhere)
|
|
->first(['id', 'floor_id']);
|
|
if ($res) {
|
|
$space_id = $res['id'];
|
|
if ($res['floor_id']) {
|
|
$floor_region_id = AdminFloorRegion::query()->where(
|
|
'floor_id',
|
|
$res['floor_id']
|
|
)->value('id');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 通过车位查询数据
|
|
if ($space_id) {
|
|
$space = ParkingSpace::query()->where('status', 0)
|
|
->where('license_plate_id', 0)->first(
|
|
['id', 'floor_id', 'space_type_id']
|
|
);
|
|
if ($space) {
|
|
$space_id = $space['id'];
|
|
if (!$space_type_id) {
|
|
$space_type_id = $space['space_type_id'];
|
|
}
|
|
if (!$floor_region_id && $space['floor_id']) {
|
|
$floor_region_id = AdminFloorRegion::query()->where(
|
|
'floor_id',
|
|
$space['floor_id']
|
|
)->value('id');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 通过车位类型 创建车牌
|
|
if ($space_type_id) {
|
|
$createData = [
|
|
'number' => $license_plate,
|
|
'space_type_id' => $space_type_id
|
|
];
|
|
(new ParkingLicensePlateService(
|
|
$this->logService
|
|
))->createModel($createData);
|
|
$license_plate_id = ParkingLicensePlate::query()->where(
|
|
$createData
|
|
)->value('id');
|
|
}
|
|
if (!$license_plate_id) {
|
|
throw new Exception(
|
|
__('service.parking_information.not_default')
|
|
);
|
|
}
|
|
|
|
if (ParkingInformation::query()->where('license_plate_id', $license_plate_id)
|
|
->exists()
|
|
) {
|
|
throw new Exception(__('service.license_plate.number_exists'));
|
|
}
|
|
|
|
$model = ParkingInformation::query()->create([
|
|
'floor_region_id' => $floor_region_id,
|
|
'space_id' => $space_id,
|
|
'license_plate_id' => $license_plate_id,
|
|
'entry_time' => get_datetime(),
|
|
'space_type_id' => $space_type_id,
|
|
'created_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logCreated($model, 'parking_information.create');
|
|
|
|
DB::commit();
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function deleteModel($id): bool
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$model = ParkingInformation::query()->findOrFail($id);
|
|
|
|
$this->logService->logDeleted($model, 'parking_information.delete');
|
|
|
|
$model->delete();
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function clearModel(): bool
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$data = ParkingInformation::query()->get()->toArray();
|
|
|
|
$this->logService->logDeletedData(
|
|
(new ParkingInformation()),
|
|
'parking_information.clear',
|
|
$data
|
|
);
|
|
|
|
ParkingInformation::query()->delete();
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|
|
|