TaskDto.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Module\Task\Dtos;
  3. use App\Module\Task\Models\Task;
  4. use UCore\Dto\BaseDto;
  5. /**
  6. * 任务数据传输对象
  7. *
  8. * 用于在不同层之间传递任务数据,特别是在服务层和控制器层之间。
  9. */
  10. class TaskDto extends BaseDto
  11. {
  12. /**
  13. * 任务ID
  14. *
  15. * @var int
  16. */
  17. public int $id;
  18. /**
  19. * 任务分类ID
  20. *
  21. * @var int|null
  22. */
  23. public ?int $categoryId;
  24. /**
  25. * 任务名称
  26. *
  27. * @var string
  28. */
  29. public string $name;
  30. /**
  31. * 任务描述
  32. *
  33. * @var string
  34. */
  35. public string $description;
  36. /**
  37. * 任务类型
  38. *
  39. * @var string
  40. */
  41. public string $type;
  42. /**
  43. * 前置任务ID列表
  44. *
  45. * @var array
  46. */
  47. public array $prerequisiteTasks = [];
  48. /**
  49. * 所需等级
  50. *
  51. * @var int
  52. */
  53. public int $levelRequired = 1;
  54. /**
  55. * 时间限制(秒)
  56. *
  57. * @var int|null
  58. */
  59. public ?int $timeLimit = null;
  60. /**
  61. * 最大完成次数
  62. *
  63. * @var int|null
  64. */
  65. public ?int $maxCompletions = null;
  66. /**
  67. * 重置类型
  68. *
  69. * @var string
  70. */
  71. public string $resetType = 'none';
  72. /**
  73. * 显示参数
  74. *
  75. * @var array
  76. */
  77. public array $displayParams = [];
  78. /**
  79. * 排序权重
  80. *
  81. * @var int
  82. */
  83. public int $sortOrder = 0;
  84. /**
  85. * 是否激活
  86. *
  87. * @var bool
  88. */
  89. public bool $isActive = true;
  90. /**
  91. * 开始时间
  92. *
  93. * @var string|null
  94. */
  95. public ?string $startTime = null;
  96. /**
  97. * 结束时间
  98. *
  99. * @var string|null
  100. */
  101. public ?string $endTime = null;
  102. /**
  103. * 任务奖励列表
  104. *
  105. * @var TaskRewardDto[]
  106. */
  107. public array $rewards = [];
  108. /**
  109. * 任务条件列表
  110. *
  111. * @var TaskConditionDto[]
  112. */
  113. public array $conditions = [];
  114. /**
  115. * 用户任务状态
  116. *
  117. * @var int|null
  118. */
  119. public ?int $userStatus = null;
  120. /**
  121. * 用户任务进度
  122. *
  123. * @var int|null
  124. */
  125. public ?int $progress = null;
  126. /**
  127. * 完成时间
  128. *
  129. * @var string|null
  130. */
  131. public ?string $completedAt = null;
  132. /**
  133. * 奖励领取时间
  134. *
  135. * @var string|null
  136. */
  137. public ?string $rewardedAt = null;
  138. /**
  139. * 从Task模型创建DTO
  140. *
  141. * @param Task $task 任务模型
  142. * @return static
  143. */
  144. public static function fromModel(Task $task): self
  145. {
  146. $dto = new self();
  147. $dto->id = $task->id;
  148. $dto->categoryId = $task->category_id;
  149. $dto->name = $task->name;
  150. $dto->description = $task->description;
  151. $dto->type = $task->type;
  152. $dto->prerequisiteTasks = $task->prerequisite_tasks ?? [];
  153. $dto->levelRequired = $task->level_required ?? 1;
  154. $dto->timeLimit = $task->time_limit;
  155. $dto->maxCompletions = $task->max_completions;
  156. $dto->resetType = $task->reset_type ?? 'none';
  157. $dto->displayParams = $task->display_params ?? [];
  158. $dto->sortOrder = $task->sort_order ?? 0;
  159. $dto->isActive = (bool)$task->is_active;
  160. $dto->startTime = $task->start_time ? $task->start_time->format('Y-m-d H:i:s') : null;
  161. $dto->endTime = $task->end_time ? $task->end_time->format('Y-m-d H:i:s') : null;
  162. // 加载任务奖励
  163. if ($task->relationLoaded('rewards')) {
  164. foreach ($task->rewards as $reward) {
  165. $dto->rewards[] = TaskRewardDto::fromModel($reward);
  166. }
  167. }
  168. // 加载任务条件
  169. if ($task->relationLoaded('achievementConditions')) {
  170. foreach ($task->achievementConditions as $condition) {
  171. $dto->conditions[] = TaskConditionDto::fromModel($condition);
  172. }
  173. }
  174. return $dto;
  175. }
  176. /**
  177. * 从数组创建DTO
  178. *
  179. * @param array $data 数据数组
  180. * @return static
  181. */
  182. public static function fromArray(array $data): static
  183. {
  184. $dto = parent::fromArray($data);
  185. // 处理奖励数组
  186. if (isset($data['rewards']) && is_array($data['rewards'])) {
  187. $dto->rewards = [];
  188. foreach ($data['rewards'] as $reward) {
  189. $dto->rewards[] = TaskRewardDto::fromArray($reward);
  190. }
  191. }
  192. // 处理条件数组
  193. if (isset($data['conditions']) && is_array($data['conditions'])) {
  194. $dto->conditions = [];
  195. foreach ($data['conditions'] as $condition) {
  196. $dto->conditions[] = TaskConditionDto::fromArray($condition);
  197. }
  198. }
  199. return $dto;
  200. }
  201. /**
  202. * 转换为模型数据数组
  203. *
  204. * @return array
  205. */
  206. public function toModelArray(): array
  207. {
  208. return [
  209. 'id' => $this->id ?? null,
  210. 'category_id' => $this->categoryId,
  211. 'name' => $this->name,
  212. 'description' => $this->description,
  213. 'type' => $this->type,
  214. 'prerequisite_tasks' => $this->prerequisiteTasks,
  215. 'level_required' => $this->levelRequired,
  216. 'time_limit' => $this->timeLimit,
  217. 'max_completions' => $this->maxCompletions,
  218. 'reset_type' => $this->resetType,
  219. 'display_params' => $this->displayParams,
  220. 'sort_order' => $this->sortOrder,
  221. 'is_active' => $this->isActive,
  222. 'start_time' => $this->startTime,
  223. 'end_time' => $this->endTime,
  224. ];
  225. }
  226. }