orderBy('buff_type') ->get(); } catch (\Exception $e) { Log::error('获取用户神灵加持失败', [ 'user_id' => $userId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return collect(); } } /** * 获取用户的有效神灵加持 * * @param int $userId * @return Collection */ public function getActiveBuffs(int $userId): Collection { try { return FarmGodBuff::where('user_id', $userId) ->where('expire_time', '>', now()) ->orderBy('buff_type') ->get(); } catch (\Exception $e) { Log::error('获取用户有效神灵加持失败', [ 'user_id' => $userId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return collect(); } } /** * 获取用户指定类型的神灵加持 * * @param int $userId * @param int $buffType * @return FarmGodBuff|null */ public function getUserBuff(int $userId, int $buffType): ?FarmGodBuff { try { return FarmGodBuff::where('user_id', $userId) ->where('buff_type', $buffType) ->first(); } catch (\Exception $e) { Log::error('获取用户指定类型神灵加持失败', [ 'user_id' => $userId, 'buff_type' => $buffType, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 获取用户指定类型的有效神灵加持 * * @param int $userId * @param int $buffType * @return FarmGodBuff|null */ public function getActiveUserBuff(int $userId, int $buffType): ?FarmGodBuff { try { return FarmGodBuff::where('user_id', $userId) ->where('buff_type', $buffType) ->where('expire_time', '>', now()) ->first(); } catch (\Exception $e) { Log::error('获取用户指定类型有效神灵加持失败', [ 'user_id' => $userId, 'buff_type' => $buffType, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 激活神灵加持 * * @param int $userId * @param int $buffType * @param int $durationHours * @return FarmGodBuff|null */ public function activateBuff(int $userId, int $buffType, int $durationHours): ?FarmGodBuff { try { // 检查buff类型是否有效 if (!in_array($buffType, [ BUFF_TYPE::HARVEST_GOD->valueInt(), BUFF_TYPE::RAIN_GOD->valueInt(), BUFF_TYPE::WEED_KILLER_GOD->valueInt(), BUFF_TYPE::PEST_CLEANER_GOD->valueInt() ])) { throw new \Exception('无效的神灵加持类型'); } // 获取当前buff $buff = FarmGodBuff::where('user_id', $userId) ->where('buff_type', $buffType) ->first(); $now = now(); $expireTime = $now->copy()->addHours($durationHours); if ($buff) { // 如果已有buff,延长时间 if ($buff->expire_time > $now) { // 如果buff未过期,在当前过期时间基础上延长 $expireTime = $buff->expire_time->addHours($durationHours); } $buff->expire_time = $expireTime; $buff->save(); } else { // 创建新buff $buff = new FarmGodBuff(); $buff->user_id = $userId; $buff->buff_type = $buffType; $buff->expire_time = $expireTime; $buff->save(); } // 触发buff激活事件 event(new BuffActivatedEvent($userId, $buff)); Log::info('神灵加持激活成功', [ 'user_id' => $userId, 'buff_type' => $buffType, 'duration_hours' => $durationHours, 'expire_time' => $expireTime->toDateTimeString() ]); return $buff; } catch (\Exception $e) { Log::error('神灵加持激活失败', [ 'user_id' => $userId, 'buff_type' => $buffType, 'duration_hours' => $durationHours, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 清理过期的神灵加持 * * @return int */ public function clearExpiredBuffs(): int { try { $count = FarmGodBuff::where('expire_time', '<', now())->delete(); Log::info('清理过期神灵加持成功', [ 'count' => $count ]); return $count; } catch (\Exception $e) { Log::error('清理过期神灵加持失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 0; } } }