CropInfoDto.php 2.7 KB

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