HarvestResultDto.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Module\Farm\Dtos;
  3. use App\Module\Farm\Models\FarmHarvestLog;
  4. /**
  5. * 收获结果数据传输对象
  6. */
  7. class HarvestResultDto
  8. {
  9. /**
  10. * 收获记录ID
  11. *
  12. * @var int
  13. */
  14. public $id;
  15. /**
  16. * 用户ID
  17. *
  18. * @var int
  19. */
  20. public $userId;
  21. /**
  22. * 土地ID
  23. *
  24. * @var int
  25. */
  26. public $landId;
  27. /**
  28. * 作物ID
  29. *
  30. * @var int
  31. */
  32. public $cropId;
  33. /**
  34. * 种子ID
  35. *
  36. * @var int
  37. */
  38. public $seedId;
  39. /**
  40. * 产出物品ID
  41. *
  42. * @var int
  43. */
  44. public $outputItemId;
  45. /**
  46. * 产出数量
  47. *
  48. * @var int
  49. */
  50. public $outputAmount;
  51. /**
  52. * 收获时间
  53. *
  54. * @var string
  55. */
  56. public $harvestTime;
  57. /**
  58. * 从模型创建DTO
  59. *
  60. * @param FarmHarvestLog $harvestLog
  61. * @param int $outputItemId
  62. * @return self
  63. */
  64. public static function fromModel(FarmHarvestLog $harvestLog, int $outputItemId): self
  65. {
  66. $dto = new self();
  67. $dto->id = $harvestLog->id;
  68. $dto->userId = $harvestLog->user_id;
  69. $dto->landId = $harvestLog->land_id;
  70. $dto->cropId = $harvestLog->crop_id;
  71. $dto->seedId = $harvestLog->seed_id;
  72. $dto->outputItemId = $outputItemId;
  73. $dto->outputAmount = $harvestLog->output_amount;
  74. $dto->harvestTime = $harvestLog->harvest_time->toDateTimeString();
  75. return $dto;
  76. }
  77. /**
  78. * 转换为数组
  79. *
  80. * @return array
  81. */
  82. public function toArray(): array
  83. {
  84. return [
  85. 'id' => $this->id,
  86. 'user_id' => $this->userId,
  87. 'land_id' => $this->landId,
  88. 'crop_id' => $this->cropId,
  89. 'seed_id' => $this->seedId,
  90. 'output_item_id' => $this->outputItemId,
  91. 'output_amount' => $this->outputAmount,
  92. 'harvest_time' => $this->harvestTime,
  93. ];
  94. }
  95. }