$value) { $statusArr[$key] = __('admin.' . $value); } return $statusArr; } /** * @param array $data * @throws Exception */ public function createModel(array $data) { try { DB::beginTransaction(); $existsWhere = [ ['member_type', '=', $data['member_type']] ]; if (AdminChannelRole::query()->where($existsWhere)->exists() ) { throw new Exception( __('service.' . $this->menuTitle . '.type_exists') ); } $channel_ids = implode(',', $data['channel_ids']); $model = AdminChannelRole::query()->create([ 'member_type' => $data['member_type'], 'status' => $data['status'] ?? 1, 'describe' => $data['describe'] ?? '', 'channel_ids' => $channel_ids ?? '', 'created_at' => get_datetime() ]); $this->logService->logCreated( $model, $this->menuTitle . '.create' ); 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 = [ ['member_type', '=', $data['member_type']], ['id', '<>', $id] ]; if (AdminChannelRole::query()->where($existsWhere)->exists() ) { throw new Exception( __('service.' . $this->menuTitle . '.reason_exists') ); } // 更新 $model = AdminChannelRole::query()->findOrFail($id); $oldValues = $model->toArray(); $channel_ids = implode(',', $data['channel_ids']); $model->update([ 'member_type' => $data['member_type'], 'status' => $data['status'] ?? 1, 'describe' => $data['describe'] ?? '', 'channel_ids' => $channel_ids ?? '', 'updated_at' => get_datetime() ]); $this->logService->logUpdated( $model, $oldValues, $this->menuTitle . '.update' ); 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 = AdminChannelRole::query()->findOrFail($id); $this->logService->logDeleted($model, $this->menuTitle . '.delete'); $model->delete(); DB::commit(); return true; } catch (Exception $e) { DB::rollBack(); throw $e; } } }