all(); DB::beginTransaction(); try { // 验证必填字段 if (empty($all['key'])) { return $this->fail([ResponseCode::ERROR_PARAMETER, '统计项标识不能为空']); } if (empty($all['name'])) { return $this->fail([ResponseCode::ERROR_PARAMETER, '统计项名称不能为空']); } if (isset($all['id'])) { $model = $this->model->find($all['id']); if (empty($model)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']); } // 更新时检查 key 是否重复(排除自己) $exists = StatisticsMetadata::where('key', $all['key']) ->where('id', '!=', $all['id']) ->first(); if ($exists) { return $this->fail([ResponseCode::ERROR_BUSINESS, '统计项标识已存在']); } } else { $model = $this->model; // 新增时检查 key 是否重复 $exists = StatisticsMetadata::where('key', $all['key'])->first(); if ($exists) { return $this->fail([ResponseCode::ERROR_BUSINESS, '统计项标识已存在']); } } $original = $model->getOriginal(); $model->fill($all); $model->save(); DB::commit(); // 记录日志 $this->saveLogs($original, $model); return $this->success($model); } catch (\Exception $exception) { DB::rollBack(); return $this->fail([$exception->getCode(), $exception->getMessage()]); } } /** * @OA\Get( * path="/api/admin/statistics-metadata/destroy", * tags={"统计指标管理"}, * summary="删除", * description="", * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( * response="200", * description="暂无" * ) * ) */ public function destroy() { return parent::destroy(); } }