ConditionLogic.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace App\Module\Activity\Logics;
  3. use App\Module\Activity\Enums\CONDITION_TYPE;
  4. use App\Module\Activity\Models\ActivityCondition;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 条件检查逻辑类
  8. */
  9. class ConditionLogic
  10. {
  11. /**
  12. * 检查条件是否满足
  13. *
  14. * @param int $userId 用户ID
  15. * @param ActivityCondition $condition 条件
  16. * @return bool
  17. */
  18. public function checkCondition(int $userId, ActivityCondition $condition): bool
  19. {
  20. try {
  21. switch ($condition->condition_type) {
  22. case CONDITION_TYPE::LEVEL_REQUIREMENT:
  23. return $this->checkLevelRequirement($userId, $condition->condition_params);
  24. case CONDITION_TYPE::ITEM_REQUIREMENT:
  25. return $this->checkItemRequirement($userId, $condition->condition_params);
  26. case CONDITION_TYPE::TIME_REQUIREMENT:
  27. return $this->checkTimeRequirement($condition->condition_params);
  28. case CONDITION_TYPE::TASK_REQUIREMENT:
  29. return $this->checkTaskRequirement($userId, $condition->condition_params);
  30. case CONDITION_TYPE::FARM_REQUIREMENT:
  31. return $this->checkFarmRequirement($userId, $condition->condition_params);
  32. case CONDITION_TYPE::PET_REQUIREMENT:
  33. return $this->checkPetRequirement($userId, $condition->condition_params);
  34. default:
  35. Log::warning('未知的条件类型', [
  36. 'condition_type' => $condition->condition_type,
  37. 'condition_id' => $condition->id
  38. ]);
  39. return false;
  40. }
  41. } catch (\Exception $e) {
  42. Log::error('检查条件时发生错误', [
  43. 'condition_id' => $condition->id,
  44. 'error' => $e->getMessage()
  45. ]);
  46. return false;
  47. }
  48. }
  49. /**
  50. * 检查等级要求
  51. *
  52. * @param int $userId 用户ID
  53. * @param array $params 条件参数
  54. * @return bool
  55. */
  56. private function checkLevelRequirement(int $userId, array $params): bool
  57. {
  58. // 获取用户等级
  59. $userLevel = $this->getUserLevel($userId);
  60. // 检查最小等级要求
  61. if (isset($params['min_level']) && $userLevel < $params['min_level']) {
  62. return false;
  63. }
  64. // 检查最大等级要求
  65. if (isset($params['max_level']) && $userLevel > $params['max_level']) {
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * 检查物品要求
  72. *
  73. * @param int $userId 用户ID
  74. * @param array $params 条件参数
  75. * @return bool
  76. */
  77. private function checkItemRequirement(int $userId, array $params): bool
  78. {
  79. // 检查是否需要特定物品
  80. if (isset($params['items']) && is_array($params['items'])) {
  81. foreach ($params['items'] as $item) {
  82. $itemId = $item['item_id'] ?? 0;
  83. $quantity = $item['quantity'] ?? 1;
  84. if ($itemId > 0) {
  85. // 获取用户物品数量
  86. $userItemCount = $this->getUserItemCount($userId, $itemId);
  87. if ($userItemCount < $quantity) {
  88. return false;
  89. }
  90. }
  91. }
  92. }
  93. return true;
  94. }
  95. /**
  96. * 检查时间要求
  97. *
  98. * @param array $params 条件参数
  99. * @return bool
  100. */
  101. private function checkTimeRequirement(array $params): bool
  102. {
  103. $now = now();
  104. // 检查开始时间
  105. if (isset($params['start_time']) && $now < \Carbon\Carbon::parse($params['start_time'])) {
  106. return false;
  107. }
  108. // 检查结束时间
  109. if (isset($params['end_time']) && $now > \Carbon\Carbon::parse($params['end_time'])) {
  110. return false;
  111. }
  112. // 检查星期几
  113. if (isset($params['days_of_week']) && is_array($params['days_of_week'])) {
  114. $currentDayOfWeek = $now->dayOfWeek;
  115. if (!in_array($currentDayOfWeek, $params['days_of_week'])) {
  116. return false;
  117. }
  118. }
  119. // 检查每日时间段
  120. if (isset($params['daily_start_time']) && isset($params['daily_end_time'])) {
  121. $dailyStartTime = \Carbon\Carbon::createFromFormat('H:i', $params['daily_start_time']);
  122. $dailyEndTime = \Carbon\Carbon::createFromFormat('H:i', $params['daily_end_time']);
  123. $currentTime = \Carbon\Carbon::createFromFormat('H:i', $now->format('H:i'));
  124. if ($currentTime < $dailyStartTime || $currentTime > $dailyEndTime) {
  125. return false;
  126. }
  127. }
  128. return true;
  129. }
  130. /**
  131. * 检查任务要求
  132. *
  133. * @param int $userId 用户ID
  134. * @param array $params 条件参数
  135. * @return bool
  136. */
  137. private function checkTaskRequirement(int $userId, array $params): bool
  138. {
  139. // 检查是否需要完成特定任务
  140. if (isset($params['task_ids']) && is_array($params['task_ids'])) {
  141. foreach ($params['task_ids'] as $taskId) {
  142. // 检查用户是否完成了任务
  143. if (!$this->isTaskCompleted($userId, $taskId)) {
  144. return false;
  145. }
  146. }
  147. }
  148. return true;
  149. }
  150. /**
  151. * 检查农场要求
  152. *
  153. * @param int $userId 用户ID
  154. * @param array $params 条件参数
  155. * @return bool
  156. */
  157. private function checkFarmRequirement(int $userId, array $params): bool
  158. {
  159. // 检查农场等级要求
  160. if (isset($params['farm_level']) && $this->getFarmLevel($userId) < $params['farm_level']) {
  161. return false;
  162. }
  163. // 检查是否拥有特定作物
  164. if (isset($params['crops']) && is_array($params['crops'])) {
  165. foreach ($params['crops'] as $crop) {
  166. $cropId = $crop['crop_id'] ?? 0;
  167. $quantity = $crop['quantity'] ?? 1;
  168. if ($cropId > 0) {
  169. // 获取用户作物数量
  170. $userCropCount = $this->getUserCropCount($userId, $cropId);
  171. if ($userCropCount < $quantity) {
  172. return false;
  173. }
  174. }
  175. }
  176. }
  177. return true;
  178. }
  179. /**
  180. * 检查宠物要求
  181. *
  182. * @param int $userId 用户ID
  183. * @param array $params 条件参数
  184. * @return bool
  185. */
  186. private function checkPetRequirement(int $userId, array $params): bool
  187. {
  188. // 检查是否拥有特定宠物
  189. if (isset($params['pet_type_id']) && !$this->hasPet($userId, $params['pet_type_id'])) {
  190. return false;
  191. }
  192. // 检查宠物等级要求
  193. if (isset($params['pet_level']) && isset($params['pet_type_id'])) {
  194. $petLevel = $this->getPetLevel($userId, $params['pet_type_id']);
  195. if ($petLevel < $params['pet_level']) {
  196. return false;
  197. }
  198. }
  199. return true;
  200. }
  201. /**
  202. * 获取用户等级
  203. *
  204. * @param int $userId 用户ID
  205. * @return int
  206. */
  207. private function getUserLevel(int $userId): int
  208. {
  209. // 这里应该调用用户等级服务获取用户等级
  210. // 由于没有实际的用户等级服务,这里返回一个模拟值
  211. return 1;
  212. }
  213. /**
  214. * 获取用户物品数量
  215. *
  216. * @param int $userId 用户ID
  217. * @param int $itemId 物品ID
  218. * @return int
  219. */
  220. private function getUserItemCount(int $userId, int $itemId): int
  221. {
  222. // 这里应该调用物品服务获取用户物品数量
  223. // 由于没有实际的物品服务,这里返回一个模拟值
  224. return 0;
  225. }
  226. /**
  227. * 检查用户是否完成了任务
  228. *
  229. * @param int $userId 用户ID
  230. * @param int $taskId 任务ID
  231. * @return bool
  232. */
  233. private function isTaskCompleted(int $userId, int $taskId): bool
  234. {
  235. // 这里应该调用任务服务检查用户是否完成了任务
  236. // 由于没有实际的任务服务,这里返回一个模拟值
  237. return false;
  238. }
  239. /**
  240. * 获取农场等级
  241. *
  242. * @param int $userId 用户ID
  243. * @return int
  244. */
  245. private function getFarmLevel(int $userId): int
  246. {
  247. // 这里应该调用农场服务获取农场等级
  248. // 由于没有实际的农场服务,这里返回一个模拟值
  249. return 1;
  250. }
  251. /**
  252. * 获取用户作物数量
  253. *
  254. * @param int $userId 用户ID
  255. * @param int $cropId 作物ID
  256. * @return int
  257. */
  258. private function getUserCropCount(int $userId, int $cropId): int
  259. {
  260. // 这里应该调用农场服务获取用户作物数量
  261. // 由于没有实际的农场服务,这里返回一个模拟值
  262. return 0;
  263. }
  264. /**
  265. * 检查用户是否拥有特定宠物
  266. *
  267. * @param int $userId 用户ID
  268. * @param int $petTypeId 宠物类型ID
  269. * @return bool
  270. */
  271. private function hasPet(int $userId, int $petTypeId): bool
  272. {
  273. // 这里应该调用宠物服务检查用户是否拥有特定宠物
  274. // 由于没有实际的宠物服务,这里返回一个模拟值
  275. return false;
  276. }
  277. /**
  278. * 获取宠物等级
  279. *
  280. * @param int $userId 用户ID
  281. * @param int $petTypeId 宠物类型ID
  282. * @return int
  283. */
  284. private function getPetLevel(int $userId, int $petTypeId): int
  285. {
  286. // 这里应该调用宠物服务获取宠物等级
  287. // 由于没有实际的宠物服务,这里返回一个模拟值
  288. return 1;
  289. }
  290. }