SkinRewardProcessor.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Module\Game\Logics\RewardProcessors;
  3. use App\Module\Game\Dtos\RewardItemDto;
  4. use App\Module\Game\Services\SkinService;
  5. use Exception;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 皮肤奖励处理器
  9. *
  10. * 处理皮肤(SKIN)类型的奖励发放
  11. */
  12. class SkinRewardProcessor
  13. {
  14. /**
  15. * 处理皮肤奖励
  16. *
  17. * @param int $userId 用户ID
  18. * @param RewardItemDto $item 奖励项
  19. * @param string $sourceType 来源类型
  20. * @param int $sourceId 来源ID
  21. * @return void
  22. * @throws Exception
  23. */
  24. public static function process(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
  25. {
  26. try {
  27. // 验证皮肤ID
  28. if (!SkinService::isValidSkinId($item->targetId)) {
  29. throw new Exception("无效的皮肤ID: {$item->targetId}");
  30. }
  31. // 检查用户是否已拥有该皮肤
  32. if (SkinService::hasSkin($userId, $item->targetId)) {
  33. Log::info("用户已拥有该皮肤,跳过发放", [
  34. 'userId' => $userId,
  35. 'skinId' => $item->targetId,
  36. 'sourceType' => $sourceType,
  37. 'sourceId' => $sourceId
  38. ]);
  39. return; // 已拥有则跳过,不抛异常
  40. }
  41. // 为用户添加皮肤
  42. $result = SkinService::addSkin($userId, $item->targetId);
  43. if (!$result->success) {
  44. throw new Exception("皮肤奖励发放失败: " . $result->errorMessage);
  45. }
  46. Log::info("皮肤奖励发放成功", [
  47. 'userId' => $userId,
  48. 'skinId' => $item->targetId,
  49. 'skinName' => SkinService::getSkinName($item->targetId),
  50. 'quantity' => $item->quantity,
  51. 'sourceType' => $sourceType,
  52. 'sourceId' => $sourceId
  53. ]);
  54. } catch (Exception $e) {
  55. Log::error("皮肤奖励处理失败", [
  56. 'userId' => $userId,
  57. 'rewardType' => $item->rewardType,
  58. 'targetId' => $item->targetId,
  59. 'quantity' => $item->quantity,
  60. 'sourceType' => $sourceType,
  61. 'sourceId' => $sourceId,
  62. 'error' => $e->getMessage()
  63. ]);
  64. throw $e;
  65. }
  66. }
  67. }