RewardGroupDto.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. /**
  4. * 奖励组DTO
  5. */
  6. class RewardGroupDto
  7. {
  8. /**
  9. * 奖励组ID
  10. *
  11. * @var int
  12. */
  13. public int $id;
  14. /**
  15. * 奖励组名称
  16. *
  17. * @var string
  18. */
  19. public string $name;
  20. /**
  21. * 奖励组编码
  22. *
  23. * @var string
  24. */
  25. public string $code;
  26. /**
  27. * 奖励组描述
  28. *
  29. * @var string|null
  30. */
  31. public ?string $description;
  32. /**
  33. * 是否随机发放
  34. *
  35. * @var bool
  36. */
  37. public bool $isRandom;
  38. /**
  39. * 随机发放时的奖励数量
  40. *
  41. * @var int
  42. */
  43. public int $randomCount;
  44. /**
  45. * 奖励模式(1:权重选择模式, 2:独立概率模式)
  46. *
  47. * @var int
  48. */
  49. public int $rewardMode;
  50. /**
  51. * 奖励项列表
  52. *
  53. * @var RewardItemDto[]
  54. */
  55. public array $items = [];
  56. /**
  57. * 从模型创建DTO
  58. *
  59. * @param \App\Module\Game\Models\GameRewardGroup $model
  60. * @param bool $withItems 是否包含奖励项
  61. * @return self
  62. */
  63. public static function fromModel($model, bool $withItems = false): self
  64. {
  65. $dto = new self();
  66. $dto->id = $model->id;
  67. $dto->name = $model->name;
  68. $dto->code = $model->code;
  69. $dto->description = $model->description;
  70. $dto->isRandom = (bool)$model->is_random;
  71. $dto->randomCount = $model->random_count;
  72. $dto->rewardMode = $model->reward_mode ?? 1; // 默认为权重选择模式
  73. if ($withItems && $model->relationLoaded('rewardItems')) {
  74. foreach ($model->rewardItems as $item) {
  75. $dto->items[] = RewardItemDto::fromModel($item);
  76. }
  77. }
  78. return $dto;
  79. }
  80. }