getGodId(); $userId = $this->user_id; // 参数验证 if ($godId <= 0) { throw new LogicException("神像ID无效"); } // 检查神像ID是否有效 if (!in_array($godId, [ BUFF_TYPE::HARVEST_GOD->value, BUFF_TYPE::RAIN_GOD->value, BUFF_TYPE::WEED_KILLER_GOD->value, BUFF_TYPE::PEST_CLEANER_GOD->value ])) { throw new LogicException("无效的神像类型"); } // 检查用户是否已有该神像的有效加持 $existingBuff = BuffService::getActiveUserBuff($userId, $godId); if ($existingBuff) { throw new LogicException("该神像已激活,有效期至:" . $existingBuff->expire_time); } // 查找用户背包中的神像物品 $godItemId = 3000 + $godId; // 假设神像物品ID为3001-3004,对应神像类型1-4 $userItems = ItemService::getUserItems($userId, ['item_id' => $godItemId]); if ($userItems->isEmpty()) { throw new LogicException("您没有该神像物品"); } // 开始事务 DB::beginTransaction(); // 消耗神像物品 ItemService::consumeItem($userId, $godItemId, null, 1, [ 'source_type' => 'god_activate', 'source_id' => $godId, 'details' => [ 'god_id' => $godId, 'god_name' => BUFF_TYPE::getName($godId) ] ]); // 激活神像加持(默认24小时) $durationHours = 24; $buff = BuffService::activateBuff($userId, $godId, $durationHours); if (!$buff) { throw new LogicException("神像激活失败"); } // 提交事务 DB::commit(); // 创建LastData对象,用于返回神像信息 $lastData = new LastData(); $godList = []; // 创建神像数据 $dataGod = new DataGod(); $dataGod->setId($godId); $dataGod->setStatus(true); $dataGod->setVaidTime($buff->expire_time->timestamp); $godList[] = $dataGod; // 设置神像列表到LastData $lastData->setGods($godList); // 设置LastData到响应 $this->response->setLastData($lastData); // 设置响应状态 $this->response->setCode(0); $this->response->setMsg('神像激活成功'); // 记录日志 Log::info('用户激活神像成功', [ 'user_id' => $userId, 'god_id' => $godId, 'expire_time' => $buff->expire_time->toDateTimeString() ]); } catch (LogicException $e) { // 回滚事务 if (DB::transactionLevel() > 0) { DB::rollBack(); } // 设置错误响应 $this->response->setCode(400); $this->response->setMsg($e->getMessage()); Log::warning('用户激活神像失败', [ 'user_id' => $this->user_id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } catch (\Exception $e) { // 回滚事务 if (DB::transactionLevel() > 0) { DB::rollBack(); } // 设置错误响应 $this->response->setCode(500); $this->response->setMsg('系统错误,请稍后再试'); Log::error('激活神像操作异常', [ 'user_id' => $this->user_id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); } return $response; } }