| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Module\Farm\Dtos;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\Farm\Models\FarmCrop;
- use Illuminate\Support\Carbon;
- /**
- * 作物信息数据传输对象
- */
- class CropInfoDto
- {
- /**
- * 作物ID
- *
- * @var int
- */
- public $id;
- /**
- * 土地ID
- *
- * @var int
- */
- public $landId;
- /**
- * 用户ID
- *
- * @var int
- */
- public $userId;
- /**
- * 种子ID
- *
- * @var int
- */
- public $seedId;
- /**
- * 种子名称
- *
- * @var string
- */
- public $seedName;
- /**
- * 种植时间
- *
- * @var string
- */
- public $plantTime;
- /**
- * 生长阶段
- *
- * @var int
- */
- public int $growthStage;
- /**
- * 生长阶段名称
- *
- * @var string
- */
- public $growthStageName;
- /**
- * 当前阶段开始时间
- *
- * @var Carbon|null
- */
- public ?Carbon $stageStartTime;
- /**
- * 当前阶段结束时间
- *
- * @var string|null
- */
- public ?Carbon $stageEndTime;
- /**
- * 灾害情况
- *
- * @var array|null
- */
- public $disasters;
- /**
- * 是否已施肥
- *
- * @var bool
- */
- public $fertilized;
- /**
- * 最终产出果实ID(发芽期确定)
- *
- * @var int|null
- */
- public $finalOutputItemId;
- /**
- * 从模型创建DTO
- *
- * @param FarmCrop $crop
- * @return self
- */
- public static function fromModel(FarmCrop $crop): self
- {
- $dto = new self();
- $dto->id = $crop->id;
- $dto->landId = $crop->land_id;
- $dto->userId = $crop->user_id;
- $dto->seedId = $crop->seed_id;
- $dto->seedName = $crop->seed->name ?? '';
- $dto->plantTime = $crop->plant_time;
- $dto->growthStage = $crop->growth_stage->value();
- // $dto->growthStageName = $crop->growth_stage->name;
- $dto->stageStartTime = $crop->stage_start_time ? $crop->stage_start_time : null;
- $dto->stageEndTime = $crop->stage_end_time ? $crop->stage_end_time : null;
- $dto->disasters = $crop->disasters;
- $dto->fertilized = $crop->fertilized;
- $dto->finalOutputItemId = $crop->final_output_item_id;
- // dd($dto);
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'id' => $this->id,
- 'land_id' => $this->landId,
- 'user_id' => $this->userId,
- 'seed_id' => $this->seedId,
- 'seed_name' => $this->seedName,
- 'plant_time' => $this->plantTime,
- 'growth_stage' => $this->growthStage,
- 'growth_stage_name' => $this->growthStageName,
- 'stage_end_time' => $this->stageEndTime,
- 'disasters' => $this->disasters,
- 'fertilized' => $this->fertilized,
- 'final_output_item_id' => $this->finalOutputItemId,
- ];
- }
- }
|