FarmCrop.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Module\Farm\Models;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\GameItems\Models\Item;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasOne;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. /**
  10. * 作物信息模型
  11. *
  12. * field start
  13. * @property int $id 主键ID
  14. * @property int $land_id 土地ID
  15. * @property int $user_id 用户ID
  16. * @property int $seed_id 种子ID
  17. * @property int $land_level 种植时的土地等级,用于产出计算
  18. * @property \Carbon\Carbon $plant_time 种植时间
  19. * @property \App\Module\Farm\Enums\GROWTH_STAGE $growth_stage 生长阶段:1种子期,20发芽期,30生长期,35果实期,40成熟期,50枯萎期
  20. * @property \Carbon\Carbon $stage_start_time 当前阶段结束时间
  21. * @property \Carbon\Carbon $stage_end_time 当前阶段结束时间
  22. * @property array $disasters 灾害情况
  23. * @property bool $fertilized 当前阶段是否已使用化肥
  24. * @property \Carbon\Carbon $last_disaster_check_time 上次灾害检查时间
  25. * @property bool $can_disaster 当前阶段是否可以产生灾害:0否,1是
  26. * @property int $final_output_item_id 最终产出果实ID(发芽期确定)
  27. * @property int $final_output_amount 最终产出数量(成熟期确定)
  28. * @property int $picked_amount 已摘取数量
  29. * @property \Carbon\Carbon $last_pick_time 最后摘取时间
  30. * @property \Carbon\Carbon $pick_cooldown_end 摘取冷却结束时间
  31. * @property int $min_reserve_amount 最小保留数量(不可摘取)
  32. * @property \Carbon\Carbon $created_at 创建时间
  33. * @property \Carbon\Carbon $updated_at 更新时间
  34. * @property \Carbon\Carbon $deleted_at 删除时间
  35. * field end
  36. *
  37. *
  38. */
  39. class FarmCrop extends Model
  40. {
  41. use SoftDeletes;
  42. /**
  43. * 与模型关联的表名
  44. *
  45. * @var string
  46. */
  47. protected $table = 'farm_crops';
  48. /**
  49. * 可批量赋值的属性
  50. *
  51. * @var array
  52. */
  53. protected $fillable = [
  54. 'land_id',
  55. 'user_id',
  56. 'seed_id',
  57. 'land_level',
  58. 'plant_time',
  59. 'growth_stage',
  60. 'stage_start_time',
  61. 'stage_end_time',
  62. 'disasters',
  63. 'fertilized',
  64. 'last_disaster_check_time',
  65. 'can_disaster',
  66. 'final_output_item_id',
  67. 'final_output_amount',
  68. 'picked_amount',
  69. 'last_pick_time',
  70. 'pick_cooldown_end',
  71. 'min_reserve_amount',
  72. ];
  73. /**
  74. * 应该被转换为原生类型的属性
  75. *
  76. * @var array
  77. */
  78. protected $casts = [
  79. 'disasters' => 'json',
  80. 'growth_stage' => GROWTH_STAGE::class,
  81. 'fertilized' => 'boolean',
  82. 'plant_time' => 'datetime',
  83. 'stage_start_time' => 'datetime',
  84. 'stage_end_time' => 'datetime',
  85. 'last_disaster_check_time' => 'datetime',
  86. 'can_disaster' => 'boolean',
  87. 'last_pick_time' => 'datetime',
  88. 'pick_cooldown_end' => 'datetime',
  89. 'deleted_at' => 'datetime',
  90. ];
  91. /**
  92. * 获取关联的土地
  93. *
  94. * @return BelongsTo
  95. */
  96. public function land(): BelongsTo
  97. {
  98. return $this->belongsTo(FarmLand::class, 'land_id', 'id');
  99. }
  100. /**
  101. * 获取关联的种子
  102. *
  103. * @return BelongsTo
  104. */
  105. public function seed(): BelongsTo
  106. {
  107. return $this->belongsTo(FarmSeed::class, 'seed_id', 'id');
  108. }
  109. /**
  110. * 用户农场信息(房屋)
  111. * @return BelongsTo
  112. */
  113. public function user(): BelongsTo
  114. {
  115. return $this->belongsTo(FarmUser::class, 'user_id', 'user_id');
  116. }
  117. public function final_output_item():HasOne
  118. {
  119. return $this->hasOne(Item::class, 'id', 'final_output_item_id');
  120. }
  121. /**
  122. * 获取可摘取数量的访问器
  123. * 可摘取数量 = 总产量 - 已摘取数量 - 最小保留数量
  124. *
  125. * @return int
  126. */
  127. public function getPickableAmountAttribute(): int
  128. {
  129. if ($this->final_output_amount <= 0) {
  130. return 0;
  131. }
  132. $pickable = $this->final_output_amount - $this->picked_amount - $this->min_reserve_amount;
  133. return max(0, $pickable);
  134. }
  135. /**
  136. * 检查是否可以摘取
  137. *
  138. * @return bool
  139. */
  140. public function canBePicked(): bool
  141. {
  142. // 必须处于成熟期
  143. if ($this->growth_stage !== GROWTH_STAGE::MATURE) {
  144. return false;
  145. }
  146. // 必须有可摘取数量
  147. if ($this->pickable_amount <= 0) {
  148. return false;
  149. }
  150. // 检查冷却时间
  151. if ($this->pick_cooldown_end && now() < $this->pick_cooldown_end) {
  152. return false;
  153. }
  154. return true;
  155. }
  156. /**
  157. * 获取摘取记录的访问器(通过作物日志查询)
  158. *
  159. * @return \Illuminate\Database\Eloquent\Collection
  160. */
  161. public function getPickLogsAttribute()
  162. {
  163. return FarmCropLog::where('crop_id', $this->id)
  164. ->where('event_type', FarmCropLog::EVENT_PICKED)
  165. ->orderBy('created_at', 'desc')
  166. ->get();
  167. }
  168. /**
  169. * 获取摘取次数的访问器
  170. *
  171. * @return int
  172. */
  173. public function getPickCountAttribute(): int
  174. {
  175. return FarmCropLog::where('crop_id', $this->id)
  176. ->where('event_type', FarmCropLog::EVENT_PICKED)
  177. ->count();
  178. }
  179. }