CropInfoDto.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Module\Farm\Dtos;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\Farm\Models\FarmCrop;
  5. use Illuminate\Support\Carbon;
  6. /**
  7. * 作物信息数据传输对象
  8. */
  9. class CropInfoDto
  10. {
  11. /**
  12. * 作物ID
  13. *
  14. * @var int
  15. */
  16. public $id;
  17. /**
  18. * 土地ID
  19. *
  20. * @var int
  21. */
  22. public $landId;
  23. /**
  24. * 用户ID
  25. *
  26. * @var int
  27. */
  28. public $userId;
  29. /**
  30. * 种子ID
  31. *
  32. * @var int
  33. */
  34. public $seedId;
  35. /**
  36. * 种子名称
  37. *
  38. * @var string
  39. */
  40. public $seedName;
  41. /**
  42. * 种植时间
  43. *
  44. * @var string
  45. */
  46. public $plantTime;
  47. /**
  48. * 生长阶段
  49. *
  50. * @var int
  51. */
  52. public int $growthStage;
  53. /**
  54. * 生长阶段名称
  55. *
  56. * @var string
  57. */
  58. public $growthStageName;
  59. /**
  60. * 当前阶段开始时间
  61. *
  62. * @var Carbon|null
  63. */
  64. public ?Carbon $stageStartTime;
  65. /**
  66. * 当前阶段结束时间
  67. *
  68. * @var string|null
  69. */
  70. public ?Carbon $stageEndTime;
  71. /**
  72. * 灾害情况
  73. *
  74. * @var array|null
  75. */
  76. public $disasters;
  77. /**
  78. * 是否已施肥
  79. *
  80. * @var bool
  81. */
  82. public $fertilized;
  83. /**
  84. * 最终产出果实ID(发芽期确定)
  85. *
  86. * @var int|null
  87. */
  88. public $finalOutputItemId;
  89. /**
  90. * 从模型创建DTO
  91. *
  92. * @param FarmCrop $crop
  93. * @return self
  94. */
  95. public static function fromModel(FarmCrop $crop): self
  96. {
  97. $dto = new self();
  98. $dto->id = $crop->id;
  99. $dto->landId = $crop->land_id;
  100. $dto->userId = $crop->user_id;
  101. $dto->seedId = $crop->seed_id;
  102. $dto->seedName = $crop->seed->name ?? '';
  103. $dto->plantTime = $crop->plant_time;
  104. $dto->growthStage = $crop->growth_stage->value();
  105. // $dto->growthStageName = $crop->growth_stage->name;
  106. $dto->stageStartTime = $crop->stage_start_time ? $crop->stage_start_time : null;
  107. $dto->stageEndTime = $crop->stage_end_time ? $crop->stage_end_time : null;
  108. $dto->disasters = $crop->disasters;
  109. $dto->fertilized = $crop->fertilized;
  110. $dto->finalOutputItemId = $crop->final_output_item_id;
  111. // dd($dto);
  112. return $dto;
  113. }
  114. /**
  115. * 转换为数组
  116. *
  117. * @return array
  118. */
  119. public function toArray(): array
  120. {
  121. return [
  122. 'id' => $this->id,
  123. 'land_id' => $this->landId,
  124. 'user_id' => $this->userId,
  125. 'seed_id' => $this->seedId,
  126. 'seed_name' => $this->seedName,
  127. 'plant_time' => $this->plantTime,
  128. 'growth_stage' => $this->growthStage,
  129. 'growth_stage_name' => $this->growthStageName,
  130. 'stage_end_time' => $this->stageEndTime,
  131. 'disasters' => $this->disasters,
  132. 'fertilized' => $this->fertilized,
  133. 'final_output_item_id' => $this->finalOutputItemId,
  134. ];
  135. }
  136. }