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.
136 lines
3.4 KiB
136 lines
3.4 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ParkingGuardBooth;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ParkingGuardBoothService extends BaseService
|
|
{
|
|
protected string $menuTitle = 'guard_booth_management';
|
|
|
|
/**
|
|
* @param array $data
|
|
* @throws Exception
|
|
*/
|
|
public function createModel(array $data)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$existsWhere = [
|
|
['name', '=', $data['name']]
|
|
];
|
|
if (ParkingGuardBooth::query()->where($existsWhere)->exists()) {
|
|
throw new Exception(
|
|
__('service.guard_booth_management.name_exists')
|
|
);
|
|
}
|
|
|
|
$model = ParkingGuardBooth::query()->create([
|
|
'name' => $data['name'],
|
|
'remark' => $data['remark'] ?? '',
|
|
'created_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logCreated(
|
|
$model,
|
|
'guard_booth_management.create'
|
|
);
|
|
|
|
AdminTranslationService::saveTranslation(
|
|
$data['name'],
|
|
$data['en_name'] ?? '',
|
|
$data['tw_name'] ?? '',
|
|
$model->id,
|
|
7
|
|
);
|
|
|
|
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();
|
|
|
|
// 验证
|
|
$existsWhere = [
|
|
['name', '=', $data['name']],
|
|
['id', '<>', $id]
|
|
];
|
|
if (ParkingGuardBooth::query()->where($existsWhere)->exists()) {
|
|
throw new Exception(
|
|
__('service.guard_booth_management.name_exists')
|
|
);
|
|
}
|
|
|
|
// 更新
|
|
$model = ParkingGuardBooth::query()->findOrFail($id);
|
|
$oldValues = $model->toArray();
|
|
|
|
$model->update([
|
|
'name' => $data['name'],
|
|
'remark' => $data['remark'] ?? '',
|
|
'updated_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logUpdated(
|
|
$model,
|
|
$oldValues,
|
|
'guard_booth_management.update'
|
|
);
|
|
|
|
AdminTranslationService::saveTranslation(
|
|
$data['name'],
|
|
$data['en_name'] ?? '',
|
|
$data['tw_name'] ?? '',
|
|
$id,
|
|
7
|
|
);
|
|
|
|
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 = ParkingGuardBooth::query()->findOrFail($id);
|
|
|
|
$this->logService->logDeleted($model, 'guard_booth_management.delete');
|
|
|
|
$model->delete();
|
|
|
|
AdminTranslationService::syncDelete($id, 7);
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|
|
|