| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- namespace App\Module\Activity\Services;
- use App\Module\Activity\Dtos\ActivityConfigDto;
- use App\Module\Activity\Dtos\ActivityParticipationDto;
- use App\Module\Activity\Dtos\UserActivityDataDto;
- use App\Module\Activity\Logics\ActivityLogic;
- use App\Module\Activity\Logics\ParticipationLogic;
- use App\Module\Activity\Logics\ProgressLogic;
- use App\Module\Activity\Logics\RewardLogic;
- use Exception;
- use Illuminate\Support\Facades\Log;
- /**
- * 活动服务类
- */
- class ActivityService
- {
- /**
- * 获取活动详情
- *
- * @param int $activityId 活动ID
- * @param bool $withConditions 是否包含条件
- * @return ActivityConfigDto|null
- */
- public static function getActivityDetail(int $activityId, bool $withConditions = false): ?ActivityConfigDto
- {
- try {
- $activityLogic = new ActivityLogic();
- return $activityLogic->getActivityDetail($activityId, $withConditions);
- } catch (Exception $e) {
- Log::error('获取活动详情失败', [
- 'activity_id' => $activityId,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
- /**
- * 获取用户可参与的活动列表
- *
- * @param int $userId 用户ID
- * @return array
- */
- public static function getUserAvailableActivities(int $userId): array
- {
- try {
- $activityLogic = new ActivityLogic();
- return $activityLogic->getUserAvailableActivities($userId);
- } catch (Exception $e) {
- Log::error('获取用户可参与活动列表失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 参与活动
- *
- * @param int $userId 用户ID
- * @param int $activityId 活动ID
- * @return array
- */
- public static function participateActivity(int $userId, int $activityId): array
- {
- try {
- $activityLogic = new ActivityLogic();
- $participation = $activityLogic->participateActivity($userId, $activityId);
-
- return [
- 'success' => true,
- 'message' => '参与活动成功',
- 'participation' => $participation
- ];
- } catch (Exception $e) {
- Log::error('参与活动失败', [
- 'user_id' => $userId,
- 'activity_id' => $activityId,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 更新活动进度
- *
- * @param int $userId 用户ID
- * @param int $activityId 活动ID
- * @param int $progress 进度值
- * @param array|null $progressData 进度数据
- * @return array
- */
- public static function updateActivityProgress(int $userId, int $activityId, int $progress, ?array $progressData = null): array
- {
- try {
- $activityLogic = new ActivityLogic();
- $result = $activityLogic->updateActivityProgress($userId, $activityId, $progress, $progressData);
-
- return [
- 'success' => $result,
- 'message' => $result ? '更新进度成功' : '更新进度失败'
- ];
- } catch (Exception $e) {
- Log::error('更新活动进度失败', [
- 'user_id' => $userId,
- 'activity_id' => $activityId,
- 'progress' => $progress,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 增加活动进度
- *
- * @param int $userId 用户ID
- * @param int $activityId 活动ID
- * @param int $increment 增量值
- * @param array|null $progressData 进度数据
- * @return array
- */
- public static function incrementActivityProgress(int $userId, int $activityId, int $increment, ?array $progressData = null): array
- {
- try {
- $progressLogic = new ProgressLogic();
- $participationLogic = new ParticipationLogic();
-
- // 获取用户参与记录
- $participation = $participationLogic->getUserParticipation($userId, $activityId);
-
- if (!$participation) {
- return [
- 'success' => false,
- 'message' => '用户未参与此活动'
- ];
- }
-
- // 获取当前进度
- $userData = $progressLogic->getUserActivityProgress($userId, $activityId);
- $currentProgress = $userData ? $userData->progress : 0;
-
- // 增加进度
- $newUserData = $progressLogic->incrementUserActivityProgress($userId, $activityId, $increment, $progressData);
-
- // 更新活动进度
- $activityLogic = new ActivityLogic();
- $activityLogic->updateActivityProgress($userId, $activityId, $newUserData->progress, $newUserData->progressData);
-
- return [
- 'success' => true,
- 'message' => '增加进度成功',
- 'old_progress' => $currentProgress,
- 'new_progress' => $newUserData->progress
- ];
- } catch (Exception $e) {
- Log::error('增加活动进度失败', [
- 'user_id' => $userId,
- 'activity_id' => $activityId,
- 'increment' => $increment,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 领取活动奖励
- *
- * @param int $userId 用户ID
- * @param int $activityId 活动ID
- * @return array
- */
- public static function claimActivityReward(int $userId, int $activityId): array
- {
- try {
- $activityLogic = new ActivityLogic();
- $result = $activityLogic->claimActivityReward($userId, $activityId);
-
- return [
- 'success' => $result,
- 'message' => $result ? '领取奖励成功' : '领取奖励失败'
- ];
- } catch (Exception $e) {
- Log::error('领取活动奖励失败', [
- 'user_id' => $userId,
- 'activity_id' => $activityId,
- 'error' => $e->getMessage()
- ]);
-
- return [
- 'success' => false,
- 'message' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取用户参与记录
- *
- * @param int $userId 用户ID
- * @param int $activityId 活动ID
- * @return ActivityParticipationDto|null
- */
- public static function getUserParticipation(int $userId, int $activityId): ?ActivityParticipationDto
- {
- try {
- $participationLogic = new ParticipationLogic();
- return $participationLogic->getUserParticipation($userId, $activityId);
- } catch (Exception $e) {
- Log::error('获取用户参与记录失败', [
- 'user_id' => $userId,
- 'activity_id' => $activityId,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
- /**
- * 获取用户活动进度
- *
- * @param int $userId 用户ID
- * @param int $activityId 活动ID
- * @return UserActivityDataDto|null
- */
- public static function getUserActivityProgress(int $userId, int $activityId): ?UserActivityDataDto
- {
- try {
- $progressLogic = new ProgressLogic();
- return $progressLogic->getUserActivityProgress($userId, $activityId);
- } catch (Exception $e) {
- Log::error('获取用户活动进度失败', [
- 'user_id' => $userId,
- 'activity_id' => $activityId,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
- /**
- * 检查用户是否可以领取活动奖励
- *
- * @param int $userId 用户ID
- * @param int $activityId 活动ID
- * @return bool
- */
- public static function canClaimReward(int $userId, int $activityId): bool
- {
- try {
- $rewardLogic = new RewardLogic();
- return $rewardLogic->canClaimReward($userId, $activityId);
- } catch (Exception $e) {
- Log::error('检查用户是否可以领取活动奖励失败', [
- 'user_id' => $userId,
- 'activity_id' => $activityId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 获取用户进行中的活动
- *
- * @param int $userId 用户ID
- * @return array
- */
- public static function getUserInProgressActivities(int $userId): array
- {
- try {
- $participationLogic = new ParticipationLogic();
- return $participationLogic->getUserInProgressActivities($userId);
- } catch (Exception $e) {
- Log::error('获取用户进行中的活动失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 获取用户已完成但未领取奖励的活动
- *
- * @param int $userId 用户ID
- * @return array
- */
- public static function getUserCompletedUnclaimedActivities(int $userId): array
- {
- try {
- $participationLogic = new ParticipationLogic();
- return $participationLogic->getUserCompletedUnclaimedActivities($userId);
- } catch (Exception $e) {
- Log::error('获取用户已完成但未领取奖励的活动失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- }
|