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.
118 lines
3.3 KiB
118 lines
3.3 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ParkingPatternSpace;
|
|
use App\Models\ParkingSpace;
|
|
use App\Models\ParkingSpaceType;
|
|
use Exception;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ParkingPatternSpaceService extends BaseService
|
|
{
|
|
public function __construct(OperationLogService $logService)
|
|
{
|
|
parent::__construct($logService);
|
|
$this->logService->menuTitle = 'model_manage';
|
|
}
|
|
|
|
/**
|
|
* @param $data
|
|
* @return Builder|Model
|
|
* @throws Exception
|
|
*/
|
|
public function createModel($data): Builder|Model
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
if (ParkingPatternSpace::query()->where('space_id', $data['parking_space_id'])
|
|
->exists()
|
|
) {
|
|
throw new Exception(__('service.parking_pattern.space_exists'));
|
|
}
|
|
if (!ParkingSpace::getNumber($data['parking_space_id'])) {
|
|
throw new Exception(__('service.parking_pattern.space_not_exists'));
|
|
}
|
|
if (!ParkingSpaceType::getName($data['parking_space_type'])) {
|
|
throw new Exception(__('service.parking_pattern.type_not_exists'));
|
|
}
|
|
$model = $this->createData($data);
|
|
DB::commit();
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $data
|
|
* @return Builder|Model
|
|
*/
|
|
public function createData($data): Builder|Model
|
|
{
|
|
$save = [
|
|
'pattern_id' => $data['pattern_id'],
|
|
'space_id' => $data['parking_space_id'],
|
|
'space_type_id' => $data['parking_space_type'],
|
|
'created_at' => get_datetime(),
|
|
'updated_at' => get_datetime()
|
|
];
|
|
$ParkingPatternSpace = ParkingPatternSpace::query()->create($save);
|
|
$this->logService->logCreated(
|
|
$ParkingPatternSpace,
|
|
'parking_pattern_space.create'
|
|
);
|
|
return $ParkingPatternSpace;
|
|
}
|
|
|
|
/**
|
|
* @param $data
|
|
* @param $id
|
|
* @return Builder|Builder[]|\Illuminate\Database\Eloquent\Collection|Model|null
|
|
* @throws Exception
|
|
*/
|
|
public function updateModel($data, $id)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
$update = [
|
|
'space_type_id' => $data['parking_space_type']
|
|
];
|
|
$model = ParkingPatternSpace::query()->findOrFail($id);
|
|
$oldValues = $model->toArray();
|
|
$model->update($update);
|
|
$this->logService->logUpdated(
|
|
$model,
|
|
$oldValues,
|
|
'parking_pattern_space.update'
|
|
);
|
|
DB::commit();
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function deleteModel(int $id): bool
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$model = ParkingPatternSpace::query()->findOrFail($id);
|
|
|
|
$this->logService->logDeleted($model, 'parking_pattern_space.delete');
|
|
|
|
$model->delete();
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|
|
|