TaskRewardDto.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Module\Task\Dtos;
  3. use App\Module\Task\Models\TaskReward;
  4. use UCore\Dto\BaseDto;
  5. /**
  6. * 任务奖励数据传输对象
  7. *
  8. * 用于在不同层之间传递任务奖励数据,特别是在服务层和控制器层之间。
  9. */
  10. class TaskRewardDto extends BaseDto
  11. {
  12. /**
  13. * 奖励ID
  14. *
  15. * @var int
  16. */
  17. public int $id;
  18. /**
  19. * 任务ID
  20. *
  21. * @var int
  22. */
  23. public int $taskId;
  24. /**
  25. * 奖励类型
  26. *
  27. * @var string
  28. */
  29. public string $rewardType;
  30. /**
  31. * 奖励参数1
  32. *
  33. * @var string
  34. */
  35. public string $rewardParam1;
  36. /**
  37. * 奖励参数2
  38. *
  39. * @var string
  40. */
  41. public string $rewardParam2;
  42. /**
  43. * 奖励数量
  44. *
  45. * @var int
  46. */
  47. public int $quantity;
  48. /**
  49. * 额外数据
  50. *
  51. * @var array
  52. */
  53. public array $extraData = [];
  54. /**
  55. * 排序权重
  56. *
  57. * @var int
  58. */
  59. public int $sortOrder = 0;
  60. /**
  61. * 从TaskReward模型创建DTO
  62. *
  63. * @param TaskReward $reward 任务奖励模型
  64. * @return static
  65. */
  66. public static function fromModel(TaskReward $reward): self
  67. {
  68. $dto = new self();
  69. $dto->id = $reward->id;
  70. $dto->taskId = $reward->task_id;
  71. $dto->rewardType = $reward->reward_type;
  72. $dto->rewardParam1 = $reward->reward_param1;
  73. $dto->rewardParam2 = $reward->reward_param2;
  74. $dto->quantity = $reward->quantity;
  75. $dto->extraData = $reward->extra_data ?? [];
  76. $dto->sortOrder = $reward->sort_order ?? 0;
  77. return $dto;
  78. }
  79. /**
  80. * 转换为模型数据数组
  81. *
  82. * @return array
  83. */
  84. public function toModelArray(): array
  85. {
  86. return [
  87. 'id' => $this->id ?? null,
  88. 'task_id' => $this->taskId,
  89. 'reward_type' => $this->rewardType,
  90. 'reward_param1' => $this->rewardParam1,
  91. 'reward_param2' => $this->rewardParam2,
  92. 'quantity' => $this->quantity,
  93. 'extra_data' => $this->extraData,
  94. 'sort_order' => $this->sortOrder,
  95. ];
  96. }
  97. }