| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace App\Module\Task\Dtos;
- use App\Module\Task\Models\TaskAchievementCondition;
- use App\Module\Task\Models\TaskCondition;
- use UCore\Dto\BaseDto;
- /**
- * 任务条件数据传输对象
- *
- * 用于在不同层之间传递任务条件数据,特别是在服务层和控制器层之间。
- */
- class TaskConditionDto extends BaseDto
- {
- /**
- * 条件ID
- *
- * @var int
- */
- public int $id;
-
- /**
- * 任务ID
- *
- * @var int
- */
- public int $taskId;
-
- /**
- * 条件ID
- *
- * @var int
- */
- public int $conditionId;
-
- /**
- * 条件类型(prerequisite=前置条件,progress=进度条件)
- *
- * @var string
- */
- public string $conditionType;
-
- /**
- * 条件参数
- *
- * @var array
- */
- public array $conditionParams = [];
-
- /**
- * 目标值
- *
- * @var int
- */
- public int $targetValue;
-
- /**
- * 是否必要条件
- *
- * @var bool
- */
- public bool $isRequired = true;
-
- /**
- * 排序权重
- *
- * @var int
- */
- public int $sortOrder = 0;
-
- /**
- * 条件代码
- *
- * @var string|null
- */
- public ?string $conditionCode = null;
-
- /**
- * 条件名称
- *
- * @var string|null
- */
- public ?string $conditionName = null;
-
- /**
- * 条件描述
- *
- * @var string|null
- */
- public ?string $conditionDescription = null;
-
- /**
- * 当前进度值
- *
- * @var int|null
- */
- public ?int $currentValue = null;
-
- /**
- * 进度百分比
- *
- * @var int|null
- */
- public ?int $progressPercent = null;
-
- /**
- * 从TaskAchievementCondition模型创建DTO
- *
- * @param TaskAchievementCondition $condition 任务达成条件模型
- * @return static
- */
- public static function fromModel(TaskAchievementCondition $condition): self
- {
- $dto = new self();
-
- $dto->id = $condition->id;
- $dto->taskId = $condition->task_id;
- $dto->conditionId = $condition->condition_id;
- $dto->conditionType = $condition->condition_type;
- $dto->conditionParams = $condition->condition_params ?? [];
- $dto->targetValue = $condition->target_value;
- $dto->isRequired = (bool)$condition->is_required;
- $dto->sortOrder = $condition->sort_order ?? 0;
-
- // 加载条件信息
- if ($condition->relationLoaded('condition')) {
- $baseCondition = $condition->condition;
- $dto->conditionCode = $baseCondition->code;
- $dto->conditionName = $baseCondition->name;
- $dto->conditionDescription = $baseCondition->description;
- }
-
- return $dto;
- }
-
- /**
- * 转换为模型数据数组
- *
- * @return array
- */
- public function toModelArray(): array
- {
- return [
- 'id' => $this->id ?? null,
- 'task_id' => $this->taskId,
- 'condition_id' => $this->conditionId,
- 'condition_type' => $this->conditionType,
- 'condition_params' => $this->conditionParams,
- 'target_value' => $this->targetValue,
- 'is_required' => $this->isRequired,
- 'sort_order' => $this->sortOrder,
- ];
- }
-
- /**
- * 设置进度信息
- *
- * @param int $currentValue 当前值
- * @return self
- */
- public function setProgress(int $currentValue): self
- {
- $this->currentValue = $currentValue;
- $this->progressPercent = $this->targetValue > 0
- ? min(100, round(($currentValue / $this->targetValue) * 100))
- : 0;
-
- return $this;
- }
- }
|