TaskLogic.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Module\Task\Logics;
  3. use App\Module\Task\Enums\TASK_STATUS;
  4. use App\Module\Task\Events\TaskRewardClaimedEvent;
  5. use App\Module\Task\Models\Task;
  6. use App\Module\Task\Models\TaskUserTask;
  7. use App\Module\Task\Services\TaskRewardGroupService;
  8. use Illuminate\Support\Carbon;
  9. use Illuminate\Support\Facades\Log;
  10. /**
  11. * 任务逻辑类
  12. *
  13. * 处理任务相关的业务逻辑,包括任务奖励的发放等
  14. */
  15. class TaskLogic
  16. {
  17. /**
  18. * 领取任务奖励
  19. *
  20. * @param int $userId 用户ID
  21. * @param int $taskId 任务ID
  22. * @return \UCore\Dto\Res 领取结果
  23. */
  24. public function claimTaskReward(int $userId, int $taskId): \UCore\Dto\Res
  25. {
  26. try {
  27. // 检查事务是否已开启
  28. \UCore\Db\Helper::check_tr();
  29. // 获取用户任务
  30. $userTask = TaskUserTask::where('user_id', $userId)
  31. ->where('task_id', $taskId)
  32. ->first();
  33. if (!$userTask) {
  34. throw new \Exception('未接取该任务');
  35. }
  36. // 检查任务状态
  37. if ($userTask->status !== TASK_STATUS::COMPLETED->value) {
  38. throw new \Exception('任务未完成,不能领取奖励');
  39. }
  40. if ($userTask->status === TASK_STATUS::REWARDED->value) {
  41. throw new \Exception('已领取过奖励');
  42. }
  43. // 获取任务信息
  44. $task = Task::find($taskId);
  45. if (!$task) {
  46. throw new \Exception('任务不存在');
  47. }
  48. // 使用奖励组服务发放奖励
  49. $result = TaskRewardGroupService::distributeRewards(
  50. $userId,
  51. $taskId,
  52. $userTask->id
  53. );
  54. if (!$result['success']) {
  55. throw new \Exception($result['message'] ?? '奖励发放失败');
  56. }
  57. // 更新任务状态
  58. $userTask->status = TASK_STATUS::REWARDED->value;
  59. $userTask->rewarded_at = Carbon::now();
  60. $userTask->save();
  61. // 触发奖励领取事件
  62. event(new TaskRewardClaimedEvent(
  63. $userId,
  64. $taskId,
  65. $task->name,
  66. $result['rewards'] ?? [],
  67. $userTask->rewarded_at,
  68. true
  69. ));
  70. return \UCore\Dto\Res::success('奖励领取成功', [
  71. 'rewards' => $result['rewards'] ?? [],
  72. ]);
  73. } catch (\Exception $e) {
  74. Log::error('任务奖励领取失败', [
  75. 'user_id' => $userId,
  76. 'task_id' => $taskId,
  77. 'error' => $e->getMessage(),
  78. 'trace' => $e->getTraceAsString()
  79. ]);
  80. return \UCore\Dto\Res::error($e->getMessage());
  81. }
  82. }
  83. /**
  84. * 获取任务奖励信息
  85. *
  86. * @param int $taskId 任务ID
  87. * @return \UCore\Dto\Res 奖励信息
  88. */
  89. public function getTaskRewardInfo(int $taskId): \UCore\Dto\Res
  90. {
  91. try {
  92. // 获取任务信息
  93. $task = Task::find($taskId);
  94. if (!$task) {
  95. return \UCore\Dto\Res::error('任务不存在', [
  96. 'rewards' => []
  97. ]);
  98. }
  99. // 使用奖励组服务获取奖励信息
  100. $rewards = TaskRewardGroupService::getTaskRewards($taskId);
  101. return \UCore\Dto\Res::success('获取奖励信息成功', [
  102. 'rewards' => $rewards
  103. ]);
  104. } catch (\Exception $e) {
  105. Log::error('获取任务奖励信息失败', [
  106. 'task_id' => $taskId,
  107. 'error' => $e->getMessage(),
  108. 'trace' => $e->getTraceAsString()
  109. ]);
  110. return \UCore\Dto\Res::error($e->getMessage(), [
  111. 'rewards' => []
  112. ]);
  113. }
  114. }
  115. /**
  116. * 迁移任务奖励到奖励组
  117. *
  118. * @param int $taskId 任务ID
  119. * @return array 迁移结果
  120. */
  121. public function migrateTaskReward(int $taskId): array
  122. {
  123. return TaskRewardGroupService::migrateTaskRewardsToRewardGroup($taskId);
  124. }
  125. }