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

126 lines
3.0 KiB

<?php
namespace App\Services;
use App\Models\AdminTranslation;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class AdminTranslationService
{
/**
* @var OperationLogService
*/
private OperationLogService $logService;
/**
* @param OperationLogService $logService
*/
public function __construct(OperationLogService $logService)
{
$this->logService = $logService;
}
/**
* @param array $data
* @return Model|Builder
* @throws Exception
*/
public function createModel(array $data): Model|Builder
{
try {
DB::beginTransaction();
DB::commit();
$save_data = [
'en' => $data['en'],
'zh_cn' => $data['zh_cn'],
'zh_tw' => $data['zh_tw']
];
if (AdminTranslation::query()->where($save_data)->exists()) {
throw new Exception(
__('service.admin_translation.data_exists')
);
}
$save_data['created_at'] = get_datetime();
$model = AdminTranslation::query()->create($save_data);
$this->logService->logCreated($model, '创建翻译');
return $model;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* @param array $data
* @param int $id
* @return Model|Builder
* @throws Exception
*/
public function updateModel(array $data, int $id): Model|Builder
{
try {
DB::beginTransaction();
DB::commit();
$update_data = [
'en' => $data['en'],
'zh_cn' => $data['zh_cn'],
'zh_tw' => $data['zh_tw']
];
if (AdminTranslation::query()->where($update_data)->where(
'id',
'<>',
$id
)->exists()
) {
throw new Exception(
__('service.admin_translation.data_exists')
);
}
$model = AdminTranslation::query()->findOrFail($id);
$oldValues = $model->toArray();
$update_data['updated_at'] = get_datetime();
$model->update($update_data);
$this->logService->logUpdated($model, $oldValues, '更新翻译');
return $model;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* @param int $id
* @return bool
* @throws Exception
*/
public function deleteModel(int $id): bool
{
try {
DB::beginTransaction();
$model = AdminTranslation::query()->findOrFail($id);
$this->logService->logDeleted($model, '删除翻译');
$model->delete();
DB::commit();
return true;
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
}