| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Module\Task\Logics;
- use App\Module\Task\Enums\TASK_STATUS;
- use App\Module\Task\Events\TaskRewardClaimedEvent;
- use App\Module\Task\Models\Task;
- use App\Module\Task\Models\TaskUserTask;
- use App\Module\Task\Services\TaskRewardGroupService;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Log;
- /**
- * 任务逻辑类
- *
- * 处理任务相关的业务逻辑,包括任务奖励的发放等
- */
- class TaskLogic
- {
- /**
- * 领取任务奖励
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @return \UCore\Dto\Res 领取结果
- */
- public function claimTaskReward(int $userId, int $taskId): \UCore\Dto\Res
- {
- try {
- // 检查事务是否已开启
- \UCore\Db\Helper::check_tr();
- // 获取用户任务
- $userTask = TaskUserTask::where('user_id', $userId)
- ->where('task_id', $taskId)
- ->first();
- if (!$userTask) {
- throw new \Exception('未接取该任务');
- }
- // 检查任务状态
- if ($userTask->status !== TASK_STATUS::COMPLETED->value) {
- throw new \Exception('任务未完成,不能领取奖励');
- }
- if ($userTask->status === TASK_STATUS::REWARDED->value) {
- throw new \Exception('已领取过奖励');
- }
- // 获取任务信息
- $task = Task::find($taskId);
- if (!$task) {
- throw new \Exception('任务不存在');
- }
- // 使用奖励组服务发放奖励
- $result = TaskRewardGroupService::distributeRewards(
- $userId,
- $taskId,
- $userTask->id
- );
- if (!$result['success']) {
- throw new \Exception($result['message'] ?? '奖励发放失败');
- }
- // 更新任务状态
- $userTask->status = TASK_STATUS::REWARDED->value;
- $userTask->rewarded_at = Carbon::now();
- $userTask->save();
- // 触发奖励领取事件
- event(new TaskRewardClaimedEvent(
- $userId,
- $taskId,
- $task->name,
- $result['rewards'] ?? [],
- $userTask->rewarded_at,
- true
- ));
- return \UCore\Dto\Res::success('奖励领取成功', [
- 'rewards' => $result['rewards'] ?? [],
- ]);
- } catch (\Exception $e) {
- Log::error('任务奖励领取失败', [
- 'user_id' => $userId,
- 'task_id' => $taskId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return \UCore\Dto\Res::error($e->getMessage());
- }
- }
- /**
- * 获取任务奖励信息
- *
- * @param int $taskId 任务ID
- * @return \UCore\Dto\Res 奖励信息
- */
- public function getTaskRewardInfo(int $taskId): \UCore\Dto\Res
- {
- try {
- // 获取任务信息
- $task = Task::find($taskId);
- if (!$task) {
- return \UCore\Dto\Res::error('任务不存在', [
- 'rewards' => []
- ]);
- }
- // 使用奖励组服务获取奖励信息
- $rewards = TaskRewardGroupService::getTaskRewards($taskId);
- return \UCore\Dto\Res::success('获取奖励信息成功', [
- 'rewards' => $rewards
- ]);
- } catch (\Exception $e) {
- Log::error('获取任务奖励信息失败', [
- 'task_id' => $taskId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return \UCore\Dto\Res::error($e->getMessage(), [
- 'rewards' => []
- ]);
- }
- }
- /**
- * 迁移任务奖励到奖励组
- *
- * @param int $taskId 任务ID
- * @return array 迁移结果
- */
- public function migrateTaskReward(int $taskId): array
- {
- return TaskRewardGroupService::migrateTaskRewardsToRewardGroup($taskId);
- }
- }
|