HarvestResultDto.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * 团队收益
  59. *
  60. * @var array
  61. */
  62. public $teamProfits = [];
  63. /**
  64. * 从模型创建DTO
  65. *
  66. * @param FarmHarvestLog $harvestLog
  67. * @param int $outputItemId
  68. * @return self
  69. */
  70. public static function fromModel(FarmHarvestLog $harvestLog, int $outputItemId): self
  71. {
  72. $dto = new self();
  73. $dto->id = $harvestLog->id;
  74. $dto->userId = $harvestLog->user_id;
  75. $dto->landId = $harvestLog->land_id;
  76. $dto->cropId = $harvestLog->crop_id;
  77. $dto->seedId = $harvestLog->seed_id;
  78. $dto->outputItemId = $outputItemId;
  79. $dto->outputAmount = $harvestLog->output_amount;
  80. $dto->harvestTime = $harvestLog->harvest_time->toDateTimeString();
  81. // 加载团队收益
  82. if ($harvestLog->teamProfits->isNotEmpty()) {
  83. foreach ($harvestLog->teamProfits as $profit) {
  84. $dto->teamProfits[] = [
  85. 'user_id' => $profit->user_id,
  86. 'team_member_id' => $profit->team_member_id,
  87. 'profit_amount' => $profit->profit_amount,
  88. 'profit_rate' => $profit->profit_rate,
  89. ];
  90. }
  91. }
  92. return $dto;
  93. }
  94. /**
  95. * 转换为数组
  96. *
  97. * @return array
  98. */
  99. public function toArray(): array
  100. {
  101. return [
  102. 'id' => $this->id,
  103. 'user_id' => $this->userId,
  104. 'land_id' => $this->landId,
  105. 'crop_id' => $this->cropId,
  106. 'seed_id' => $this->seedId,
  107. 'output_item_id' => $this->outputItemId,
  108. 'output_amount' => $this->outputAmount,
  109. 'harvest_time' => $this->harvestTime,
  110. 'team_profits' => $this->teamProfits,
  111. ];
  112. }
  113. }