RewardService.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Module\Game\Services;
  3. use App\Module\Game\Dtos\RewardGroupDto;
  4. use App\Module\Game\Dtos\RewardResultDto;
  5. use App\Module\Game\Logics\RewardLogic;
  6. /**
  7. * 奖励服务类
  8. *
  9. * 提供奖励相关的服务,对外接口
  10. */
  11. class RewardService
  12. {
  13. /**
  14. * 获取奖励组信息
  15. *
  16. * @param int|string $groupIdOrCode 奖励组ID或编码
  17. * @return RewardGroupDto|null 奖励组DTO,不存在时返回null
  18. */
  19. public static function getRewardGroup($groupIdOrCode): ?RewardGroupDto
  20. {
  21. $logic = new RewardLogic();
  22. return $logic->getRewardGroup($groupIdOrCode);
  23. }
  24. /**
  25. * 发放奖励
  26. *
  27. * @param int $userId 用户ID
  28. * @param int|string $groupIdOrCode 奖励组ID或编码
  29. * @param string $sourceType 来源类型(任务、活动、签到等)
  30. * @param int $sourceId 来源ID
  31. * @return RewardResultDto 奖励结果
  32. */
  33. public static function grantReward(int $userId, $groupIdOrCode, string $sourceType, int $sourceId): RewardResultDto
  34. {
  35. $logic = new RewardLogic();
  36. return $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId);
  37. }
  38. /**
  39. * 批量发放奖励
  40. *
  41. * @param array $userIds 用户ID数组
  42. * @param int|string $groupIdOrCode 奖励组ID或编码
  43. * @param string $sourceType 来源类型
  44. * @param int $sourceId 来源ID
  45. * @return array 奖励结果数组,键为用户ID,值为RewardResultDto
  46. */
  47. public static function batchGrantReward(array $userIds, $groupIdOrCode, string $sourceType, int $sourceId): array
  48. {
  49. $results = [];
  50. $logic = new RewardLogic();
  51. foreach ($userIds as $userId) {
  52. $results[$userId] = $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId);
  53. }
  54. return $results;
  55. }
  56. /**
  57. * 检查奖励组是否存在
  58. *
  59. * @param int|string $groupIdOrCode 奖励组ID或编码
  60. * @return bool 是否存在
  61. */
  62. public static function rewardGroupExists($groupIdOrCode): bool
  63. {
  64. return self::getRewardGroup($groupIdOrCode) !== null;
  65. }
  66. }