RewardLogic.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Module\Activity\Logics;
  3. use App\Module\Activity\Dtos\ActivityRewardDto;
  4. use App\Module\Activity\Enums\REWARD_STATUS;
  5. use App\Module\Activity\Models\ActivityConfig;
  6. use App\Module\Activity\Models\ActivityParticipation;
  7. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  8. use Exception;
  9. use Illuminate\Support\Facades\Log;
  10. use UCore\Helper\Logger;
  11. /**
  12. * 奖励逻辑类
  13. */
  14. class RewardLogic
  15. {
  16. /**
  17. * 检查用户是否可以领取活动奖励
  18. *
  19. * @param int $userId 用户ID
  20. * @param int $activityId 活动ID
  21. * @return bool
  22. */
  23. public function canClaimReward(int $userId, int $activityId): bool
  24. {
  25. // 获取用户参与记录
  26. $participation = ActivityParticipation::where('user_id', $userId)
  27. ->where('activity_id', $activityId)
  28. ->first();
  29. if (!$participation) {
  30. return false;
  31. }
  32. // 检查是否已完成且未领取奖励
  33. return $participation->completion_status === 1 && $participation->reward_status === REWARD_STATUS::NOT_CLAIMED;
  34. }
  35. /**
  36. * 标记奖励为已领取
  37. *
  38. * @param int $userId 用户ID
  39. * @param int $activityId 活动ID
  40. * @return bool
  41. * @throws Exception
  42. */
  43. public function markRewardAsClaimed(int $userId, int $activityId): bool
  44. {
  45. // 获取用户参与记录
  46. $participation = ActivityParticipation::where('user_id', $userId)
  47. ->where('activity_id', $activityId)
  48. ->first();
  49. if (!$participation) {
  50. throw new Exception('用户未参与此活动');
  51. }
  52. if ($participation->completion_status !== 1) {
  53. throw new Exception('活动未完成,无法领取奖励');
  54. }
  55. if ($participation->reward_status === REWARD_STATUS::CLAIMED) {
  56. throw new Exception('奖励已领取');
  57. }
  58. // 更新奖励状态为已领取
  59. $participation->reward_status = REWARD_STATUS::CLAIMED;
  60. return $participation->save();
  61. }
  62. /**
  63. * 标记奖励为已过期
  64. *
  65. * @param int $userId 用户ID
  66. * @param int $activityId 活动ID
  67. * @return bool
  68. * @throws Exception
  69. */
  70. public function markRewardAsExpired(int $userId, int $activityId): bool
  71. {
  72. // 获取用户参与记录
  73. $participation = ActivityParticipation::where('user_id', $userId)
  74. ->where('activity_id', $activityId)
  75. ->first();
  76. if (!$participation) {
  77. throw new Exception('用户未参与此活动');
  78. }
  79. if ($participation->reward_status !== REWARD_STATUS::NOT_CLAIMED) {
  80. throw new Exception('奖励状态不是未领取,无法标记为已过期');
  81. }
  82. // 更新奖励状态为已过期
  83. $participation->reward_status = REWARD_STATUS::EXPIRED;
  84. return $participation->save();
  85. }
  86. /**
  87. * 获取活动奖励信息
  88. *
  89. * @param int $activityId 活动ID
  90. * @return ActivityRewardDto|null
  91. */
  92. public function getActivityReward(int $activityId): ?ActivityRewardDto
  93. {
  94. // 获取活动信息
  95. $activity = ActivityConfig::find($activityId);
  96. if (!$activity) {
  97. return null;
  98. }
  99. // 如果活动没有配置奖励组,返回空
  100. if (!$activity->reward_group_id && !$activity->reward_group_code) {
  101. return null;
  102. }
  103. // 创建奖励DTO
  104. $rewardDto = ActivityRewardDto::create(
  105. $activity->reward_group_id,
  106. $activity->reward_group_code
  107. );
  108. // 尝试获取奖励组信息
  109. try {
  110. // 这里应该调用奖励组服务获取奖励组信息
  111. // 由于没有实际的奖励组服务,这里只返回基本信息
  112. $rewardDto->rewardGroupName = '活动奖励';
  113. $rewardDto->rewardGroupDescription = '完成活动获得的奖励';
  114. // 模拟奖励项
  115. $rewardDto->rewardItems = [
  116. [
  117. 'type' => 'item',
  118. 'id' => 1001,
  119. 'name' => '金币',
  120. 'quantity' => 100
  121. ]
  122. ];
  123. } catch (Exception $e) {
  124. Logger::error('获取奖励组信息失败', [
  125. 'activity_id' => $activityId,
  126. 'reward_group_id' => $activity->reward_group_id,
  127. 'reward_group_code' => $activity->reward_group_code,
  128. 'error' => $e->getMessage()
  129. ]);
  130. }
  131. return $rewardDto;
  132. }
  133. /**
  134. * 发放活动奖励
  135. *
  136. * @param int $userId 用户ID
  137. * @param int $activityId 活动ID
  138. * @return array 奖励结果
  139. * @throws Exception
  140. */
  141. public function grantActivityReward(int $userId, int $activityId): array
  142. {
  143. // 获取活动信息
  144. $activity = ActivityConfig::find($activityId);
  145. if (!$activity) {
  146. throw new Exception('活动不存在');
  147. }
  148. // 如果活动没有配置奖励组,返回空结果
  149. if (!$activity->reward_group_id && !$activity->reward_group_code) {
  150. return [
  151. 'success' => true,
  152. 'message' => '活动未配置奖励',
  153. 'rewards' => []
  154. ];
  155. }
  156. // 尝试发放奖励
  157. try {
  158. // 使用奖励组服务发放奖励
  159. $rewardGroupId = $activity->reward_group_id;
  160. $rewardGroupCode = $activity->reward_group_code;
  161. // 调用奖励组服务
  162. $rewardService = new \App\Module\Game\Services\RewardService();
  163. $rewardResult = $rewardService::grantReward(
  164. $userId,
  165. $rewardGroupId ?: $rewardGroupCode,
  166. REWARD_SOURCE_TYPE::ACTIVITY,
  167. $activity->id
  168. );
  169. if ($rewardResult->success) {
  170. return [
  171. 'success' => true,
  172. 'message' => '奖励发放成功',
  173. 'rewards' => $rewardResult->items
  174. ];
  175. } else {
  176. return [
  177. 'success' => false,
  178. 'message' => $rewardResult->errorMessage,
  179. 'rewards' => []
  180. ];
  181. }
  182. } catch (Exception $e) {
  183. Log::error('发放活动奖励失败', [
  184. 'user_id' => $userId,
  185. 'activity_id' => $activityId,
  186. 'error' => $e->getMessage()
  187. ]);
  188. throw new Exception('发放奖励失败: ' . $e->getMessage());
  189. }
  190. }
  191. }