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