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; } } }