ActivityService.php 9.9 KB

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