| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace App\Module\Task\Dtos;
- use App\Module\Task\Models\Task;
- use UCore\Dto\BaseDto;
- /**
- * 任务数据传输对象
- *
- * 用于在不同层之间传递任务数据,特别是在服务层和控制器层之间。
- */
- class TaskDto extends BaseDto
- {
- /**
- * 任务ID
- *
- * @var int
- */
- public int $id;
-
- /**
- * 任务分类ID
- *
- * @var int|null
- */
- public ?int $categoryId;
-
- /**
- * 任务名称
- *
- * @var string
- */
- public string $name;
-
- /**
- * 任务描述
- *
- * @var string
- */
- public string $description;
-
- /**
- * 任务类型
- *
- * @var string
- */
- public string $type;
-
- /**
- * 前置任务ID列表
- *
- * @var array
- */
- public array $prerequisiteTasks = [];
-
- /**
- * 所需等级
- *
- * @var int
- */
- public int $levelRequired = 1;
-
- /**
- * 时间限制(秒)
- *
- * @var int|null
- */
- public ?int $timeLimit = null;
-
- /**
- * 最大完成次数
- *
- * @var int|null
- */
- public ?int $maxCompletions = null;
-
- /**
- * 重置类型
- *
- * @var string
- */
- public string $resetType = 'none';
-
- /**
- * 显示参数
- *
- * @var array
- */
- public array $displayParams = [];
-
- /**
- * 排序权重
- *
- * @var int
- */
- public int $sortOrder = 0;
-
- /**
- * 是否激活
- *
- * @var bool
- */
- public bool $isActive = true;
-
- /**
- * 开始时间
- *
- * @var string|null
- */
- public ?string $startTime = null;
-
- /**
- * 结束时间
- *
- * @var string|null
- */
- public ?string $endTime = null;
-
- /**
- * 任务奖励列表
- *
- * @var TaskRewardDto[]
- */
- public array $rewards = [];
-
- /**
- * 任务条件列表
- *
- * @var TaskConditionDto[]
- */
- public array $conditions = [];
-
- /**
- * 用户任务状态
- *
- * @var int|null
- */
- public ?int $userStatus = null;
-
- /**
- * 用户任务进度
- *
- * @var int|null
- */
- public ?int $progress = null;
-
- /**
- * 完成时间
- *
- * @var string|null
- */
- public ?string $completedAt = null;
-
- /**
- * 奖励领取时间
- *
- * @var string|null
- */
- public ?string $rewardedAt = null;
-
- /**
- * 从Task模型创建DTO
- *
- * @param Task $task 任务模型
- * @return static
- */
- public static function fromModel(Task $task): self
- {
- $dto = new self();
-
- $dto->id = $task->id;
- $dto->categoryId = $task->category_id;
- $dto->name = $task->name;
- $dto->description = $task->description;
- $dto->type = $task->type;
- $dto->prerequisiteTasks = $task->prerequisite_tasks ?? [];
- $dto->levelRequired = $task->level_required ?? 1;
- $dto->timeLimit = $task->time_limit;
- $dto->maxCompletions = $task->max_completions;
- $dto->resetType = $task->reset_type ?? 'none';
- $dto->displayParams = $task->display_params ?? [];
- $dto->sortOrder = $task->sort_order ?? 0;
- $dto->isActive = (bool)$task->is_active;
- $dto->startTime = $task->start_time ? $task->start_time->format('Y-m-d H:i:s') : null;
- $dto->endTime = $task->end_time ? $task->end_time->format('Y-m-d H:i:s') : null;
-
- // 加载任务奖励
- if ($task->relationLoaded('rewards')) {
- foreach ($task->rewards as $reward) {
- $dto->rewards[] = TaskRewardDto::fromModel($reward);
- }
- }
-
- // 加载任务条件
- if ($task->relationLoaded('achievementConditions')) {
- foreach ($task->achievementConditions as $condition) {
- $dto->conditions[] = TaskConditionDto::fromModel($condition);
- }
- }
-
- return $dto;
- }
-
- /**
- * 从数组创建DTO
- *
- * @param array $data 数据数组
- * @return static
- */
- public static function fromArray(array $data): static
- {
- $dto = parent::fromArray($data);
-
- // 处理奖励数组
- if (isset($data['rewards']) && is_array($data['rewards'])) {
- $dto->rewards = [];
- foreach ($data['rewards'] as $reward) {
- $dto->rewards[] = TaskRewardDto::fromArray($reward);
- }
- }
-
- // 处理条件数组
- if (isset($data['conditions']) && is_array($data['conditions'])) {
- $dto->conditions = [];
- foreach ($data['conditions'] as $condition) {
- $dto->conditions[] = TaskConditionDto::fromArray($condition);
- }
- }
-
- return $dto;
- }
-
- /**
- * 转换为模型数据数组
- *
- * @return array
- */
- public function toModelArray(): array
- {
- return [
- 'id' => $this->id ?? null,
- 'category_id' => $this->categoryId,
- 'name' => $this->name,
- 'description' => $this->description,
- 'type' => $this->type,
- 'prerequisite_tasks' => $this->prerequisiteTasks,
- 'level_required' => $this->levelRequired,
- 'time_limit' => $this->timeLimit,
- 'max_completions' => $this->maxCompletions,
- 'reset_type' => $this->resetType,
- 'display_params' => $this->displayParams,
- 'sort_order' => $this->sortOrder,
- 'is_active' => $this->isActive,
- 'start_time' => $this->startTime,
- 'end_time' => $this->endTime,
- ];
- }
- }
|