| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Module\Farm\Models;
- use App\Module\Farm\Enums\LAND_STATUS;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- /**
- * 土地信息模型
- * field start
- * @property int $id 主键ID
- * @property int $user_id 用户ID
- * @property int $position 土地位置(1-20)
- * @property int $land_type 土地类型:1普通,2红土,3黑土,4金,5蓝,6紫
- * @property int $status 土地状态:0空闲,1种植中,2灾害,3可收获,4枯萎
- * @property bool $has_crop 是否有作物:0无,1有
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- * @property-read FarmUser $farmUser 关联的用户农场
- * @property-read FarmLandType $landType 关联的土地类型
- *
- * @package App\Module\Farm\Models
- */
- class FarmLand extends Model
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'farm_land_users';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'user_id',
- 'position',
- 'land_type',
- 'status',
- 'has_crop',
- ];
- // 暂时移除枚举类型转换,避免在表单处理时出现类型问题
- protected $casts = [
- // 'status' => LAND_STATUS::class
- 'has_crop' => 'boolean',
- ];
- /**
- * 获取关联的用户农场
- *
- * @return BelongsTo
- */
- public function farmUser(): BelongsTo
- {
- return $this->belongsTo(FarmUser::class, 'user_id', 'user_id');
- }
- /**
- * 获取关联的土地类型
- *
- * @return BelongsTo
- */
- public function landType(): BelongsTo
- {
- return $this->belongsTo(FarmLandType::class, 'land_type', 'id');
- }
- /**
- * 获取关联的作物
- *
- * @return HasOne
- */
- public function crop(): HasOne
- {
- return $this->hasOne(FarmCrop::class, 'land_id', 'id');
- }
- /**
- * 更新has_crop字段值
- * 根据当前状态自动更新has_crop字段
- *
- * @return void
- */
- public function updateHasCrop(): void
- {
- $this->has_crop = in_array($this->status, [
- LAND_STATUS::PLANTING->value, // 1 种植中
- LAND_STATUS::DISASTER->value, // 2 灾害
- LAND_STATUS::HARVESTABLE->value, // 3 可收获
- LAND_STATUS::WITHERED->value, // 4 枯萎
- ]);
- }
- }
|