| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <?php
- namespace App\Module\Task\Services;
- use App\Module\Task\Enums\REWARD_TYPE;
- use App\Module\Task\Models\TaskReward;
- use App\Module\Task\Models\TaskRewardLog;
- use App\Module\Task\Repositorys\TaskRewardLogRepository;
- use App\Module\Task\Repositorys\TaskRewardRepository;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 任务奖励服务类
- *
- * 提供任务奖励相关的服务,包括奖励发放、奖励记录等功能。
- * 该类主要处理任务奖励的逻辑,与TaskService配合使用。
- */
- class TaskRewardService
- {
- /**
- * 任务奖励数据仓库
- *
- * @var TaskRewardRepository
- */
- protected $rewardRepository;
-
- /**
- * 任务奖励日志数据仓库
- *
- * @var TaskRewardLogRepository
- */
- protected $rewardLogRepository;
-
- /**
- * 构造函数
- *
- * @param TaskRewardRepository $rewardRepository
- * @param TaskRewardLogRepository $rewardLogRepository
- */
- public function __construct(
- TaskRewardRepository $rewardRepository,
- TaskRewardLogRepository $rewardLogRepository
- ) {
- $this->rewardRepository = $rewardRepository;
- $this->rewardLogRepository = $rewardLogRepository;
- }
-
- /**
- * 获取任务奖励列表
- *
- * @param int $taskId 任务ID
- * @return array 奖励列表
- */
- public function getTaskRewards(int $taskId): array
- {
- return $this->rewardRepository->getRewardsByTaskId($taskId);
- }
-
- /**
- * 发放任务奖励
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @param int $userTaskId 用户任务ID
- * @return array 发放结果
- */
- public function distributeRewards(int $userId, int $taskId, int $userTaskId): array
- {
- try {
- // 开始事务
- DB::beginTransaction();
-
- // 获取任务奖励
- $rewards = $this->getTaskRewards($taskId);
-
- if (empty($rewards)) {
- throw new \Exception('任务没有奖励');
- }
-
- // 发放每个奖励
- $results = [];
- $allSuccess = true;
-
- foreach ($rewards as $reward) {
- $result = $this->distributeReward($userId, $reward);
- $results[] = $result;
-
- if (!$result['success']) {
- $allSuccess = false;
- }
- }
-
- // 记录奖励发放日志
- $this->recordRewardLog($userId, $taskId, $userTaskId, $rewards, $results);
-
- // 提交事务
- DB::commit();
-
- return [
- 'success' => $allSuccess,
- 'message' => $allSuccess ? '奖励发放成功' : '部分奖励发放失败',
- 'rewards' => $results,
- ];
- } catch (\Exception $e) {
- // 回滚事务
- DB::rollBack();
-
- Log::error('奖励发放失败', [
- 'user_id' => $userId,
- 'task_id' => $taskId,
- 'user_task_id' => $userTaskId,
- 'error' => $e->getMessage(),
- ]);
-
- return [
- 'success' => false,
- 'message' => '奖励发放失败: ' . $e->getMessage(),
- ];
- }
- }
-
- /**
- * 发放单个奖励
- *
- * @param int $userId 用户ID
- * @param array|TaskReward $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeReward(int $userId, $reward): array
- {
- try {
- // 将TaskReward对象转换为数组
- if ($reward instanceof TaskReward) {
- $reward = $reward->toArray();
- }
-
- // 根据奖励类型调用不同的发放方法
- $rewardType = REWARD_TYPE::tryFrom($reward['reward_type']) ?? null;
-
- if (!$rewardType) {
- throw new \Exception('未知的奖励类型: ' . $reward['reward_type']);
- }
-
- switch ($rewardType) {
- case REWARD_TYPE::ITEM:
- return $this->distributeItemReward($userId, $reward);
-
- case REWARD_TYPE::CURRENCY:
- return $this->distributeCurrencyReward($userId, $reward);
-
- case REWARD_TYPE::EXPERIENCE:
- return $this->distributeExperienceReward($userId, $reward);
-
- case REWARD_TYPE::FEATURE_UNLOCK:
- return $this->distributeFeatureUnlockReward($userId, $reward);
-
- case REWARD_TYPE::VIP_POINTS:
- return $this->distributeVipPointsReward($userId, $reward);
-
- case REWARD_TYPE::TITLE:
- return $this->distributeTitleReward($userId, $reward);
-
- case REWARD_TYPE::AVATAR_FRAME:
- return $this->distributeAvatarFrameReward($userId, $reward);
-
- case REWARD_TYPE::PET:
- return $this->distributePetReward($userId, $reward);
-
- case REWARD_TYPE::PET_ITEM:
- return $this->distributePetItemReward($userId, $reward);
-
- case REWARD_TYPE::FARM_DECORATION:
- return $this->distributeFarmDecorationReward($userId, $reward);
-
- default:
- throw new \Exception('未实现的奖励类型: ' . $rewardType->value);
- }
- } catch (\Exception $e) {
- Log::error('发放单个奖励失败', [
- 'user_id' => $userId,
- 'reward' => $reward,
- 'error' => $e->getMessage(),
- ]);
-
- return [
- 'success' => false,
- 'message' => '发放奖励失败: ' . $e->getMessage(),
- 'reward_type' => $reward['reward_type'] ?? 'unknown',
- 'reward_param1' => $reward['reward_param1'] ?? '',
- 'reward_param2' => $reward['reward_param2'] ?? '',
- 'quantity' => $reward['quantity'] ?? 0,
- ];
- }
- }
-
- /**
- * 发放物品奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeItemReward(int $userId, array $reward): array
- {
- // 这里应该调用物品模块的服务来发放物品
- // 例如:ItemService::addItem($userId, $reward['reward_param2'], $reward['quantity'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '物品发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放货币奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeCurrencyReward(int $userId, array $reward): array
- {
- // 这里应该调用货币模块的服务来发放货币
- // 例如:CurrencyService::addCurrency($userId, $reward['reward_param1'], $reward['reward_param2'], $reward['quantity'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '货币发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放经验奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeExperienceReward(int $userId, array $reward): array
- {
- // 这里应该调用经验模块的服务来发放经验
- // 例如:ExperienceService::addExperience($userId, $reward['reward_param1'], $reward['quantity'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '经验发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放功能解锁奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeFeatureUnlockReward(int $userId, array $reward): array
- {
- // 这里应该调用功能解锁模块的服务来解锁功能
- // 例如:FeatureService::unlockFeature($userId, $reward['reward_param1'], $reward['reward_param2'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '功能解锁成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放VIP点数奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeVipPointsReward(int $userId, array $reward): array
- {
- // 这里应该调用VIP模块的服务来发放VIP点数
- // 例如:VipService::addPoints($userId, $reward['quantity'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = 'VIP点数发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放称号奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeTitleReward(int $userId, array $reward): array
- {
- // 这里应该调用称号模块的服务来发放称号
- // 例如:TitleService::addTitle($userId, $reward['reward_param1'], $reward['reward_param2'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '称号发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放头像框奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeAvatarFrameReward(int $userId, array $reward): array
- {
- // 这里应该调用头像框模块的服务来发放头像框
- // 例如:AvatarFrameService::addFrame($userId, $reward['reward_param1'], $reward['reward_param2'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '头像框发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放宠物奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributePetReward(int $userId, array $reward): array
- {
- // 这里应该调用宠物模块的服务来发放宠物
- // 例如:PetService::addPet($userId, $reward['reward_param1'], $reward['reward_param2'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '宠物发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放宠物物品奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributePetItemReward(int $userId, array $reward): array
- {
- // 这里应该调用宠物物品模块的服务来发放宠物物品
- // 例如:PetItemService::addItem($userId, $reward['reward_param1'], $reward['reward_param2'], $reward['quantity'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '宠物物品发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 发放农场装饰物奖励
- *
- * @param int $userId 用户ID
- * @param array $reward 奖励信息
- * @return array 发放结果
- */
- protected function distributeFarmDecorationReward(int $userId, array $reward): array
- {
- // 这里应该调用农场装饰物模块的服务来发放农场装饰物
- // 例如:FarmDecorationService::addDecoration($userId, $reward['reward_param1'], $reward['reward_param2'], $reward['quantity'], [...]);
-
- // 模拟发放结果
- $success = true;
- $message = '农场装饰物发放成功';
-
- return [
- 'success' => $success,
- 'message' => $message,
- 'reward_type' => $reward['reward_type'],
- 'reward_param1' => $reward['reward_param1'],
- 'reward_param2' => $reward['reward_param2'],
- 'quantity' => $reward['quantity'],
- ];
- }
-
- /**
- * 记录奖励发放日志
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @param int $userTaskId 用户任务ID
- * @param array $rewards 奖励列表
- * @param array $results 发放结果
- * @return bool 是否成功
- */
- protected function recordRewardLog(int $userId, int $taskId, int $userTaskId, array $rewards, array $results): bool
- {
- try {
- // 创建奖励日志
- $this->rewardLogRepository->create([
- 'user_id' => $userId,
- 'task_id' => $taskId,
- 'user_task_id' => $userTaskId,
- 'rewards' => $rewards,
- 'rewarded_at' => Carbon::now(),
- 'ip_address' => request()->ip(),
- 'device_info' => request()->userAgent(),
- ]);
-
- return true;
- } catch (\Exception $e) {
- Log::error('记录奖励发放日志失败', [
- 'user_id' => $userId,
- 'task_id' => $taskId,
- 'user_task_id' => $userTaskId,
- 'error' => $e->getMessage(),
- ]);
-
- return false;
- }
- }
-
- /**
- * 获取用户的奖励发放历史
- *
- * @param int $userId 用户ID
- * @param int|null $taskId 任务ID(可选)
- * @param int $page 页码
- * @param int $pageSize 每页数量
- * @return array 奖励发放历史
- */
- public function getUserRewardHistory(int $userId, ?int $taskId = null, int $page = 1, int $pageSize = 10): array
- {
- try {
- // 构建查询
- $query = $this->rewardLogRepository->query()
- ->where('user_id', $userId);
-
- if ($taskId) {
- $query->where('task_id', $taskId);
- }
-
- // 分页查询
- $total = $query->count();
- $logs = $query->orderBy('id', 'desc')
- ->forPage($page, $pageSize)
- ->get();
-
- return [
- 'success' => true,
- 'total' => $total,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'logs' => $logs,
- ];
- } catch (\Exception $e) {
- Log::error('获取用户奖励发放历史失败', [
- 'user_id' => $userId,
- 'task_id' => $taskId,
- 'error' => $e->getMessage(),
- ]);
-
- return [
- 'success' => false,
- 'message' => '获取用户奖励发放历史失败: ' . $e->getMessage(),
- ];
- }
- }
- }
|