| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?php
- namespace App\Module\Activity\Logics;
- use App\Module\Activity\Enums\CONDITION_TYPE;
- use App\Module\Activity\Models\ActivityCondition;
- use Illuminate\Support\Facades\Log;
- use UCore\Helper\Logger;
- /**
- * 条件检查逻辑类
- */
- class ConditionLogic
- {
- /**
- * 检查条件是否满足
- *
- * @param int $userId 用户ID
- * @param ActivityCondition $condition 条件
- * @return bool
- */
- public function checkCondition(int $userId, ActivityCondition $condition): bool
- {
- try {
- switch ($condition->condition_type) {
- case CONDITION_TYPE::LEVEL_REQUIREMENT:
- return $this->checkLevelRequirement($userId, $condition->condition_params);
- case CONDITION_TYPE::ITEM_REQUIREMENT:
- return $this->checkItemRequirement($userId, $condition->condition_params);
- case CONDITION_TYPE::TIME_REQUIREMENT:
- return $this->checkTimeRequirement($condition->condition_params);
- case CONDITION_TYPE::TASK_REQUIREMENT:
- return $this->checkTaskRequirement($userId, $condition->condition_params);
- case CONDITION_TYPE::FARM_REQUIREMENT:
- return $this->checkFarmRequirement($userId, $condition->condition_params);
- case CONDITION_TYPE::PET_REQUIREMENT:
- return $this->checkPetRequirement($userId, $condition->condition_params);
- default:
- Log::warning('未知的条件类型', [
- 'condition_type' => $condition->condition_type,
- 'condition_id' => $condition->id
- ]);
- return false;
- }
- } catch (\Exception $e) {
- Logger::error('检查条件时发生错误', [
- 'condition_id' => $condition->id,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 检查等级要求
- *
- * @param int $userId 用户ID
- * @param array $params 条件参数
- * @return bool
- */
- private function checkLevelRequirement(int $userId, array $params): bool
- {
- // 获取用户等级
- $userLevel = $this->getUserLevel($userId);
- // 检查最小等级要求
- if (isset($params['min_level']) && $userLevel < $params['min_level']) {
- return false;
- }
- // 检查最大等级要求
- if (isset($params['max_level']) && $userLevel > $params['max_level']) {
- return false;
- }
- return true;
- }
- /**
- * 检查物品要求
- *
- * @param int $userId 用户ID
- * @param array $params 条件参数
- * @return bool
- */
- private function checkItemRequirement(int $userId, array $params): bool
- {
- // 检查是否需要特定物品
- if (isset($params['items']) && is_array($params['items'])) {
- foreach ($params['items'] as $item) {
- $itemId = $item['item_id'] ?? 0;
- $quantity = $item['quantity'] ?? 1;
- if ($itemId > 0) {
- // 获取用户物品数量
- $userItemCount = $this->getUserItemCount($userId, $itemId);
- if ($userItemCount < $quantity) {
- return false;
- }
- }
- }
- }
- return true;
- }
- /**
- * 检查时间要求
- *
- * @param array $params 条件参数
- * @return bool
- */
- private function checkTimeRequirement(array $params): bool
- {
- $now = now();
- // 检查开始时间
- if (isset($params['start_time']) && $now < \Carbon\Carbon::parse($params['start_time'])) {
- return false;
- }
- // 检查结束时间
- if (isset($params['end_time']) && $now > \Carbon\Carbon::parse($params['end_time'])) {
- return false;
- }
- // 检查星期几
- if (isset($params['days_of_week']) && is_array($params['days_of_week'])) {
- $currentDayOfWeek = $now->dayOfWeek;
- if (!in_array($currentDayOfWeek, $params['days_of_week'])) {
- return false;
- }
- }
- // 检查每日时间段
- if (isset($params['daily_start_time']) && isset($params['daily_end_time'])) {
- $dailyStartTime = \Carbon\Carbon::createFromFormat('H:i', $params['daily_start_time']);
- $dailyEndTime = \Carbon\Carbon::createFromFormat('H:i', $params['daily_end_time']);
- $currentTime = \Carbon\Carbon::createFromFormat('H:i', $now->format('H:i'));
- if ($currentTime < $dailyStartTime || $currentTime > $dailyEndTime) {
- return false;
- }
- }
- return true;
- }
- /**
- * 检查任务要求
- *
- * @param int $userId 用户ID
- * @param array $params 条件参数
- * @return bool
- */
- private function checkTaskRequirement(int $userId, array $params): bool
- {
- // 检查是否需要完成特定任务
- if (isset($params['task_ids']) && is_array($params['task_ids'])) {
- foreach ($params['task_ids'] as $taskId) {
- // 检查用户是否完成了任务
- if (!$this->isTaskCompleted($userId, $taskId)) {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * 检查农场要求
- *
- * @param int $userId 用户ID
- * @param array $params 条件参数
- * @return bool
- */
- private function checkFarmRequirement(int $userId, array $params): bool
- {
- // 检查农场等级要求
- if (isset($params['farm_level']) && $this->getFarmLevel($userId) < $params['farm_level']) {
- return false;
- }
- // 检查是否拥有特定作物
- if (isset($params['crops']) && is_array($params['crops'])) {
- foreach ($params['crops'] as $crop) {
- $cropId = $crop['crop_id'] ?? 0;
- $quantity = $crop['quantity'] ?? 1;
- if ($cropId > 0) {
- // 获取用户作物数量
- $userCropCount = $this->getUserCropCount($userId, $cropId);
- if ($userCropCount < $quantity) {
- return false;
- }
- }
- }
- }
- return true;
- }
- /**
- * 检查宠物要求
- *
- * @param int $userId 用户ID
- * @param array $params 条件参数
- * @return bool
- */
- private function checkPetRequirement(int $userId, array $params): bool
- {
- // 检查是否拥有特定宠物
- if (isset($params['pet_type_id']) && !$this->hasPet($userId, $params['pet_type_id'])) {
- return false;
- }
- // 检查宠物等级要求
- if (isset($params['pet_level']) && isset($params['pet_type_id'])) {
- $petLevel = $this->getPetLevel($userId, $params['pet_type_id']);
- if ($petLevel < $params['pet_level']) {
- return false;
- }
- }
- return true;
- }
- /**
- * 获取用户等级
- *
- * @param int $userId 用户ID
- * @return int
- */
- private function getUserLevel(int $userId): int
- {
- // 这里应该调用用户等级服务获取用户等级
- // 由于没有实际的用户等级服务,这里返回一个模拟值
- return 1;
- }
- /**
- * 获取用户物品数量
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @return int
- */
- private function getUserItemCount(int $userId, int $itemId): int
- {
- // 这里应该调用物品服务获取用户物品数量
- // 由于没有实际的物品服务,这里返回一个模拟值
- return 0;
- }
- /**
- * 检查用户是否完成了任务
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @return bool
- */
- private function isTaskCompleted(int $userId, int $taskId): bool
- {
- // 这里应该调用任务服务检查用户是否完成了任务
- // 由于没有实际的任务服务,这里返回一个模拟值
- return false;
- }
- /**
- * 获取农场等级
- *
- * @param int $userId 用户ID
- * @return int
- */
- private function getFarmLevel(int $userId): int
- {
- // 这里应该调用农场服务获取农场等级
- // 由于没有实际的农场服务,这里返回一个模拟值
- return 1;
- }
- /**
- * 获取用户作物数量
- *
- * @param int $userId 用户ID
- * @param int $cropId 作物ID
- * @return int
- */
- private function getUserCropCount(int $userId, int $cropId): int
- {
- // 这里应该调用农场服务获取用户作物数量
- // 由于没有实际的农场服务,这里返回一个模拟值
- return 0;
- }
- /**
- * 检查用户是否拥有特定宠物
- *
- * @param int $userId 用户ID
- * @param int $petTypeId 宠物类型ID
- * @return bool
- */
- private function hasPet(int $userId, int $petTypeId): bool
- {
- // 这里应该调用宠物服务检查用户是否拥有特定宠物
- // 由于没有实际的宠物服务,这里返回一个模拟值
- return false;
- }
- /**
- * 获取宠物等级
- *
- * @param int $userId 用户ID
- * @param int $petTypeId 宠物类型ID
- * @return int
- */
- private function getPetLevel(int $userId, int $petTypeId): int
- {
- // 这里应该调用宠物服务获取宠物等级
- // 由于没有实际的宠物服务,这里返回一个模拟值
- return 1;
- }
- }
|