TaskConditionDto.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Module\Task\Dtos;
  3. use App\Module\Task\Models\TaskAchievementCondition;
  4. use App\Module\Task\Models\TaskCondition;
  5. use UCore\Dto\BaseDto;
  6. /**
  7. * 任务条件数据传输对象
  8. *
  9. * 用于在不同层之间传递任务条件数据,特别是在服务层和控制器层之间。
  10. */
  11. class TaskConditionDto extends BaseDto
  12. {
  13. /**
  14. * 条件ID
  15. *
  16. * @var int
  17. */
  18. public int $id;
  19. /**
  20. * 任务ID
  21. *
  22. * @var int
  23. */
  24. public int $taskId;
  25. /**
  26. * 条件ID
  27. *
  28. * @var int
  29. */
  30. public int $conditionId;
  31. /**
  32. * 条件类型(prerequisite=前置条件,progress=进度条件)
  33. *
  34. * @var string
  35. */
  36. public string $conditionType;
  37. /**
  38. * 条件参数
  39. *
  40. * @var array
  41. */
  42. public array $conditionParams = [];
  43. /**
  44. * 目标值
  45. *
  46. * @var int
  47. */
  48. public int $targetValue;
  49. /**
  50. * 是否必要条件
  51. *
  52. * @var bool
  53. */
  54. public bool $isRequired = true;
  55. /**
  56. * 排序权重
  57. *
  58. * @var int
  59. */
  60. public int $sortOrder = 0;
  61. /**
  62. * 条件代码
  63. *
  64. * @var string|null
  65. */
  66. public ?string $conditionCode = null;
  67. /**
  68. * 条件名称
  69. *
  70. * @var string|null
  71. */
  72. public ?string $conditionName = null;
  73. /**
  74. * 条件描述
  75. *
  76. * @var string|null
  77. */
  78. public ?string $conditionDescription = null;
  79. /**
  80. * 当前进度值
  81. *
  82. * @var int|null
  83. */
  84. public ?int $currentValue = null;
  85. /**
  86. * 进度百分比
  87. *
  88. * @var int|null
  89. */
  90. public ?int $progressPercent = null;
  91. /**
  92. * 从TaskAchievementCondition模型创建DTO
  93. *
  94. * @param TaskAchievementCondition $condition 任务达成条件模型
  95. * @return static
  96. */
  97. public static function fromModel(TaskAchievementCondition $condition): self
  98. {
  99. $dto = new self();
  100. $dto->id = $condition->id;
  101. $dto->taskId = $condition->task_id;
  102. $dto->conditionId = $condition->condition_id;
  103. $dto->conditionType = $condition->condition_type;
  104. $dto->conditionParams = $condition->condition_params ?? [];
  105. $dto->targetValue = $condition->target_value;
  106. $dto->isRequired = (bool)$condition->is_required;
  107. $dto->sortOrder = $condition->sort_order ?? 0;
  108. // 加载条件信息
  109. if ($condition->relationLoaded('condition')) {
  110. $baseCondition = $condition->condition;
  111. $dto->conditionCode = $baseCondition->code;
  112. $dto->conditionName = $baseCondition->name;
  113. $dto->conditionDescription = $baseCondition->description;
  114. }
  115. return $dto;
  116. }
  117. /**
  118. * 转换为模型数据数组
  119. *
  120. * @return array
  121. */
  122. public function toModelArray(): array
  123. {
  124. return [
  125. 'id' => $this->id ?? null,
  126. 'task_id' => $this->taskId,
  127. 'condition_id' => $this->conditionId,
  128. 'condition_type' => $this->conditionType,
  129. 'condition_params' => $this->conditionParams,
  130. 'target_value' => $this->targetValue,
  131. 'is_required' => $this->isRequired,
  132. 'sort_order' => $this->sortOrder,
  133. ];
  134. }
  135. /**
  136. * 设置进度信息
  137. *
  138. * @param int $currentValue 当前值
  139. * @return self
  140. */
  141. public function setProgress(int $currentValue): self
  142. {
  143. $this->currentValue = $currentValue;
  144. $this->progressPercent = $this->targetValue > 0
  145. ? min(100, round(($currentValue / $this->targetValue) * 100))
  146. : 0;
  147. return $this;
  148. }
  149. }