ActivityService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace App\Module\Activity\Services;
  3. use App\Module\Activity\Dtos\ActivityConfigDto;
  4. use App\Module\Activity\Dtos\ActivityParticipationDto;
  5. use App\Module\Activity\Dtos\UserActivityDataDto;
  6. use App\Module\Activity\Logics\ActivityLogic;
  7. use App\Module\Activity\Logics\ParticipationLogic;
  8. use App\Module\Activity\Logics\ProgressLogic;
  9. use App\Module\Activity\Logics\RewardLogic;
  10. use Exception;
  11. use Illuminate\Support\Facades\Log;
  12. use UCore\Helper\Logger;
  13. /**
  14. * 活动服务类
  15. */
  16. class ActivityService
  17. {
  18. /**
  19. * 获取活动详情
  20. *
  21. * @param int $activityId 活动ID
  22. * @param bool $withConditions 是否包含条件
  23. * @return ActivityConfigDto|null
  24. */
  25. public static function getActivityDetail(int $activityId, bool $withConditions = false): ?ActivityConfigDto
  26. {
  27. try {
  28. $activityLogic = new ActivityLogic();
  29. return $activityLogic->getActivityDetail($activityId, $withConditions);
  30. } catch (Exception $e) {
  31. Logger::error('获取活动详情失败', [
  32. 'activity_id' => $activityId,
  33. 'error' => $e->getMessage()
  34. ]);
  35. return null;
  36. }
  37. }
  38. /**
  39. * 获取用户可参与的活动列表
  40. *
  41. * @param int $userId 用户ID
  42. * @return array
  43. */
  44. public static function getUserAvailableActivities(int $userId): array
  45. {
  46. try {
  47. $activityLogic = new ActivityLogic();
  48. return $activityLogic->getUserAvailableActivities($userId);
  49. } catch (Exception $e) {
  50. Logger::error('获取用户可参与活动列表失败', [
  51. 'user_id' => $userId,
  52. 'error' => $e->getMessage()
  53. ]);
  54. return [];
  55. }
  56. }
  57. /**
  58. * 参与活动
  59. *
  60. * @param int $userId 用户ID
  61. * @param int $activityId 活动ID
  62. * @return array
  63. */
  64. public static function participateActivity(int $userId, int $activityId): array
  65. {
  66. try {
  67. $activityLogic = new ActivityLogic();
  68. $participation = $activityLogic->participateActivity($userId, $activityId);
  69. return [
  70. 'success' => true,
  71. 'message' => '参与活动成功',
  72. 'participation' => $participation
  73. ];
  74. } catch (Exception $e) {
  75. Logger::error('参与活动失败', [
  76. 'user_id' => $userId,
  77. 'activity_id' => $activityId,
  78. 'error' => $e->getMessage()
  79. ]);
  80. return [
  81. 'success' => false,
  82. 'message' => $e->getMessage()
  83. ];
  84. }
  85. }
  86. /**
  87. * 更新活动进度
  88. *
  89. * @param int $userId 用户ID
  90. * @param int $activityId 活动ID
  91. * @param int $progress 进度值
  92. * @param array|null $progressData 进度数据
  93. * @return array
  94. */
  95. public static function updateActivityProgress(int $userId, int $activityId, int $progress, ?array $progressData = null): array
  96. {
  97. try {
  98. $activityLogic = new ActivityLogic();
  99. $result = $activityLogic->updateActivityProgress($userId, $activityId, $progress, $progressData);
  100. return [
  101. 'success' => $result,
  102. 'message' => $result ? '更新进度成功' : '更新进度失败'
  103. ];
  104. } catch (Exception $e) {
  105. Logger::error('更新活动进度失败', [
  106. 'user_id' => $userId,
  107. 'activity_id' => $activityId,
  108. 'progress' => $progress,
  109. 'error' => $e->getMessage()
  110. ]);
  111. return [
  112. 'success' => false,
  113. 'message' => $e->getMessage()
  114. ];
  115. }
  116. }
  117. /**
  118. * 增加活动进度
  119. *
  120. * @param int $userId 用户ID
  121. * @param int $activityId 活动ID
  122. * @param int $increment 增量值
  123. * @param array|null $progressData 进度数据
  124. * @return array
  125. */
  126. public static function incrementActivityProgress(int $userId, int $activityId, int $increment, ?array $progressData = null): array
  127. {
  128. try {
  129. $progressLogic = new ProgressLogic();
  130. $participationLogic = new ParticipationLogic();
  131. // 获取用户参与记录
  132. $participation = $participationLogic->getUserParticipation($userId, $activityId);
  133. if (!$participation) {
  134. return [
  135. 'success' => false,
  136. 'message' => '用户未参与此活动'
  137. ];
  138. }
  139. // 获取当前进度
  140. $userData = $progressLogic->getUserActivityProgress($userId, $activityId);
  141. $currentProgress = $userData ? $userData->progress : 0;
  142. // 增加进度
  143. $newUserData = $progressLogic->incrementUserActivityProgress($userId, $activityId, $increment, $progressData);
  144. // 更新活动进度
  145. $activityLogic = new ActivityLogic();
  146. $activityLogic->updateActivityProgress($userId, $activityId, $newUserData->progress, $newUserData->progressData);
  147. return [
  148. 'success' => true,
  149. 'message' => '增加进度成功',
  150. 'old_progress' => $currentProgress,
  151. 'new_progress' => $newUserData->progress
  152. ];
  153. } catch (Exception $e) {
  154. Logger::error('增加活动进度失败', [
  155. 'user_id' => $userId,
  156. 'activity_id' => $activityId,
  157. 'increment' => $increment,
  158. 'error' => $e->getMessage()
  159. ]);
  160. return [
  161. 'success' => false,
  162. 'message' => $e->getMessage()
  163. ];
  164. }
  165. }
  166. /**
  167. * 领取活动奖励
  168. *
  169. * @param int $userId 用户ID
  170. * @param int $activityId 活动ID
  171. * @return array
  172. */
  173. public static function claimActivityReward(int $userId, int $activityId): array
  174. {
  175. try {
  176. $activityLogic = new ActivityLogic();
  177. $result = $activityLogic->claimActivityReward($userId, $activityId);
  178. return [
  179. 'success' => $result,
  180. 'message' => $result ? '领取奖励成功' : '领取奖励失败'
  181. ];
  182. } catch (Exception $e) {
  183. Logger::error('领取活动奖励失败', [
  184. 'user_id' => $userId,
  185. 'activity_id' => $activityId,
  186. 'error' => $e->getMessage()
  187. ]);
  188. return [
  189. 'success' => false,
  190. 'message' => $e->getMessage()
  191. ];
  192. }
  193. }
  194. /**
  195. * 获取用户参与记录
  196. *
  197. * @param int $userId 用户ID
  198. * @param int $activityId 活动ID
  199. * @return ActivityParticipationDto|null
  200. */
  201. public static function getUserParticipation(int $userId, int $activityId): ?ActivityParticipationDto
  202. {
  203. try {
  204. $participationLogic = new ParticipationLogic();
  205. return $participationLogic->getUserParticipation($userId, $activityId);
  206. } catch (Exception $e) {
  207. Logger::error('获取用户参与记录失败', [
  208. 'user_id' => $userId,
  209. 'activity_id' => $activityId,
  210. 'error' => $e->getMessage()
  211. ]);
  212. return null;
  213. }
  214. }
  215. /**
  216. * 获取用户活动进度
  217. *
  218. * @param int $userId 用户ID
  219. * @param int $activityId 活动ID
  220. * @return UserActivityDataDto|null
  221. */
  222. public static function getUserActivityProgress(int $userId, int $activityId): ?UserActivityDataDto
  223. {
  224. try {
  225. $progressLogic = new ProgressLogic();
  226. return $progressLogic->getUserActivityProgress($userId, $activityId);
  227. } catch (Exception $e) {
  228. Logger::error('获取用户活动进度失败', [
  229. 'user_id' => $userId,
  230. 'activity_id' => $activityId,
  231. 'error' => $e->getMessage()
  232. ]);
  233. return null;
  234. }
  235. }
  236. /**
  237. * 检查用户是否可以领取活动奖励
  238. *
  239. * @param int $userId 用户ID
  240. * @param int $activityId 活动ID
  241. * @return bool
  242. */
  243. public static function canClaimReward(int $userId, int $activityId): bool
  244. {
  245. try {
  246. $rewardLogic = new RewardLogic();
  247. return $rewardLogic->canClaimReward($userId, $activityId);
  248. } catch (Exception $e) {
  249. Logger::error('检查用户是否可以领取活动奖励失败', [
  250. 'user_id' => $userId,
  251. 'activity_id' => $activityId,
  252. 'error' => $e->getMessage()
  253. ]);
  254. return false;
  255. }
  256. }
  257. /**
  258. * 获取用户进行中的活动
  259. *
  260. * @param int $userId 用户ID
  261. * @return array
  262. */
  263. public static function getUserInProgressActivities(int $userId): array
  264. {
  265. try {
  266. $participationLogic = new ParticipationLogic();
  267. return $participationLogic->getUserInProgressActivities($userId);
  268. } catch (Exception $e) {
  269. Logger::error('获取用户进行中的活动失败', [
  270. 'user_id' => $userId,
  271. 'error' => $e->getMessage()
  272. ]);
  273. return [];
  274. }
  275. }
  276. /**
  277. * 获取用户已完成但未领取奖励的活动
  278. *
  279. * @param int $userId 用户ID
  280. * @return array
  281. */
  282. public static function getUserCompletedUnclaimedActivities(int $userId): array
  283. {
  284. try {
  285. $participationLogic = new ParticipationLogic();
  286. return $participationLogic->getUserCompletedUnclaimedActivities($userId);
  287. } catch (Exception $e) {
  288. Logger::error('获取用户已完成但未领取奖励的活动失败', [
  289. 'user_id' => $userId,
  290. 'error' => $e->getMessage()
  291. ]);
  292. return [];
  293. }
  294. }
  295. }