ActivityConfigDto.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Module\Activity\Dtos;
  3. use App\Module\Activity\Models\ActivityConfig;
  4. use UCore\Dto\BaseDto;
  5. /**
  6. * 活动配置数据传输对象
  7. */
  8. class ActivityConfigDto extends BaseDto
  9. {
  10. /**
  11. * 活动ID
  12. *
  13. * @var int
  14. */
  15. public int $id;
  16. /**
  17. * 活动名称
  18. *
  19. * @var string
  20. */
  21. public string $name;
  22. /**
  23. * 活动类型
  24. *
  25. * @var int
  26. */
  27. public int $type;
  28. /**
  29. * 活动类型名称
  30. *
  31. * @var string
  32. */
  33. public string $typeName;
  34. /**
  35. * 活动描述
  36. *
  37. * @var string
  38. */
  39. public string $description;
  40. /**
  41. * 开始时间
  42. *
  43. * @var string
  44. */
  45. public string $startTime;
  46. /**
  47. * 结束时间
  48. *
  49. * @var string
  50. */
  51. public string $endTime;
  52. /**
  53. * 活动状态
  54. *
  55. * @var int
  56. */
  57. public int $status;
  58. /**
  59. * 活动状态名称
  60. *
  61. * @var string
  62. */
  63. public string $statusName;
  64. /**
  65. * 显示顺序
  66. *
  67. * @var int
  68. */
  69. public int $displayOrder;
  70. /**
  71. * 活动图标URL
  72. *
  73. * @var string|null
  74. */
  75. public ?string $icon;
  76. /**
  77. * 活动横幅URL
  78. *
  79. * @var string|null
  80. */
  81. public ?string $banner;
  82. /**
  83. * 奖励组ID
  84. *
  85. * @var int|null
  86. */
  87. public ?int $rewardGroupId;
  88. /**
  89. * 奖励组编码
  90. *
  91. * @var string|null
  92. */
  93. public ?string $rewardGroupCode;
  94. /**
  95. * 活动特定配置参数
  96. *
  97. * @var array
  98. */
  99. public array $configParams = [];
  100. /**
  101. * 活动条件列表
  102. *
  103. * @var ActivityConditionDto[]
  104. */
  105. public array $conditions = [];
  106. /**
  107. * 用户参与状态
  108. *
  109. * @var ActivityParticipationDto|null
  110. */
  111. public ?ActivityParticipationDto $participation = null;
  112. /**
  113. * 从模型创建DTO
  114. *
  115. * @param ActivityConfig $model 活动配置模型
  116. * @param bool $withConditions 是否包含条件
  117. * @return self
  118. */
  119. public static function fromModel(ActivityConfig $model, bool $withConditions = false): self
  120. {
  121. $dto = new self();
  122. $dto->id = $model->id;
  123. $dto->name = $model->name;
  124. $dto->type = $model->type;
  125. $dto->typeName = $model->getTypeName();
  126. $dto->description = $model->description ?? '';
  127. $dto->startTime = $model->start_time ? $model->start_time->format('Y-m-d H:i:s') : '';
  128. $dto->endTime = $model->end_time ? $model->end_time->format('Y-m-d H:i:s') : '';
  129. $dto->status = $model->status;
  130. $dto->statusName = $model->getStatusName();
  131. $dto->displayOrder = $model->display_order;
  132. $dto->icon = $model->icon;
  133. $dto->banner = $model->banner;
  134. $dto->rewardGroupId = $model->reward_group_id;
  135. $dto->rewardGroupCode = $model->reward_group_code;
  136. $dto->configParams = $model->config_params ?? [];
  137. // 加载条件
  138. if ($withConditions && $model->relationLoaded('conditions')) {
  139. foreach ($model->conditions as $condition) {
  140. $dto->conditions[] = ActivityConditionDto::fromModel($condition);
  141. }
  142. }
  143. return $dto;
  144. }
  145. /**
  146. * 转换为模型数据数组
  147. *
  148. * @return array
  149. */
  150. public function toModelArray(): array
  151. {
  152. return [
  153. 'id' => $this->id ?? null,
  154. 'name' => $this->name,
  155. 'type' => $this->type,
  156. 'description' => $this->description,
  157. 'start_time' => $this->startTime,
  158. 'end_time' => $this->endTime,
  159. 'status' => $this->status,
  160. 'display_order' => $this->displayOrder,
  161. 'icon' => $this->icon,
  162. 'banner' => $this->banner,
  163. 'reward_group_id' => $this->rewardGroupId,
  164. 'reward_group_code' => $this->rewardGroupCode,
  165. 'config_params' => $this->configParams,
  166. ];
  167. }
  168. }