ConditionLogic.php 9.6 KB

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