| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace App\Module\Farm\Models;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\GameItems\Models\Item;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Database\Eloquent\SoftDeletes;
- /**
- * 作物信息模型
- *
- * field start
- * @property int $id 主键ID
- * @property int $land_id 土地ID
- * @property int $user_id 用户ID
- * @property int $seed_id 种子ID
- * @property int $land_level 种植时的土地等级,用于产出计算
- * @property \Carbon\Carbon $plant_time 种植时间
- * @property \App\Module\Farm\Enums\GROWTH_STAGE $growth_stage 生长阶段:1种子期,20发芽期,30生长期,35果实期,40成熟期,50枯萎期
- * @property \Carbon\Carbon $stage_start_time 当前阶段结束时间
- * @property \Carbon\Carbon $stage_end_time 当前阶段结束时间
- * @property array $disasters 灾害情况
- * @property bool $fertilized 当前阶段是否已使用化肥
- * @property \Carbon\Carbon $last_disaster_check_time 上次灾害检查时间
- * @property bool $can_disaster 当前阶段是否可以产生灾害:0否,1是
- * @property int $final_output_item_id 最终产出果实ID(发芽期确定)
- * @property int $final_output_amount 最终产出数量(成熟期确定)
- * @property int $picked_amount 已摘取数量
- * @property \Carbon\Carbon $last_pick_time 最后摘取时间
- * @property \Carbon\Carbon $pick_cooldown_end 摘取冷却结束时间
- * @property int $min_reserve_amount 最小保留数量(不可摘取)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * @property \Carbon\Carbon $deleted_at 删除时间
- * field end
- *
- *
- */
- class FarmCrop extends Model
- {
- use SoftDeletes;
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'farm_crops';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'land_id',
- 'user_id',
- 'seed_id',
- 'land_level',
- 'plant_time',
- 'growth_stage',
- 'stage_start_time',
- 'stage_end_time',
- 'disasters',
- 'fertilized',
- 'last_disaster_check_time',
- 'can_disaster',
- 'final_output_item_id',
- 'final_output_amount',
- 'picked_amount',
- 'last_pick_time',
- 'pick_cooldown_end',
- 'min_reserve_amount',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'disasters' => 'json',
- 'growth_stage' => GROWTH_STAGE::class,
- 'fertilized' => 'boolean',
- 'plant_time' => 'datetime',
- 'stage_start_time' => 'datetime',
- 'stage_end_time' => 'datetime',
- 'last_disaster_check_time' => 'datetime',
- 'can_disaster' => 'boolean',
- 'last_pick_time' => 'datetime',
- 'pick_cooldown_end' => 'datetime',
- 'deleted_at' => 'datetime',
- ];
- /**
- * 获取关联的土地
- *
- * @return BelongsTo
- */
- public function land(): BelongsTo
- {
- return $this->belongsTo(FarmLand::class, 'land_id', 'id');
- }
- /**
- * 获取关联的种子
- *
- * @return BelongsTo
- */
- public function seed(): BelongsTo
- {
- return $this->belongsTo(FarmSeed::class, 'seed_id', 'id');
- }
- /**
- * 用户农场信息(房屋)
- * @return BelongsTo
- */
- public function user(): BelongsTo
- {
- return $this->belongsTo(FarmUser::class, 'user_id', 'user_id');
- }
- public function final_output_item():HasOne
- {
- return $this->hasOne(Item::class, 'id', 'final_output_item_id');
- }
- /**
- * 获取可摘取数量的访问器
- * 可摘取数量 = 总产量 - 已摘取数量 - 最小保留数量
- *
- * @return int
- */
- public function getPickableAmountAttribute(): int
- {
- if ($this->final_output_amount <= 0) {
- return 0;
- }
- $pickable = $this->final_output_amount - $this->picked_amount - $this->min_reserve_amount;
- return max(0, $pickable);
- }
- /**
- * 检查是否可以摘取
- *
- * @return bool
- */
- public function canBePicked(): bool
- {
- // 必须处于成熟期
- if ($this->growth_stage !== GROWTH_STAGE::MATURE) {
- return false;
- }
- // 必须有可摘取数量
- if ($this->pickable_amount <= 0) {
- return false;
- }
- // 检查冷却时间
- if ($this->pick_cooldown_end && now() < $this->pick_cooldown_end) {
- return false;
- }
- return true;
- }
- /**
- * 获取摘取记录的访问器(通过作物日志查询)
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function getPickLogsAttribute()
- {
- return FarmCropLog::where('crop_id', $this->id)
- ->where('event_type', FarmCropLog::EVENT_PICKED)
- ->orderBy('created_at', 'desc')
- ->get();
- }
- /**
- * 获取摘取次数的访问器
- *
- * @return int
- */
- public function getPickCountAttribute(): int
- {
- return FarmCropLog::where('crop_id', $this->id)
- ->where('event_type', FarmCropLog::EVENT_PICKED)
- ->count();
- }
- }
|