| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace App\Module\Farm\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- /**
- * 作物事件日志模型
- *
- * @property int $id 主键ID
- * @property int $user_id 用户ID
- * @property int $land_id 土地ID
- * @property int $crop_id 作物ID
- * @property int $seed_id 种子ID
- * @property string $event_type 事件类型
- * @property array|null $event_data 事件详细数据
- * @property int $growth_stage 发生时的生长阶段
- * @property int $land_type 土地类型
- * @property \Carbon\Carbon $created_at 事件发生时间
- * @property \Carbon\Carbon $updated_at 更新时间
- *
- * @property-read FarmCrop $crop 关联的作物
- * @property-read FarmSeed $seed 关联的种子
- * @property-read FarmLand $land 关联的土地
- */
- class FarmCropLog extends ModelCore
- {
- protected $table = 'farm_crop_logs';
- // 事件类型常量
- const EVENT_FRUIT_CONFIRMED = 'fruit_confirmed'; // 确认果实种类
- const EVENT_OUTPUT_CALCULATED = 'output_calculated'; // 确认产出数量
- const EVENT_DISASTER_OCCURRED = 'disaster_occurred'; // 灾害产生
- const EVENT_DISASTER_CLEARED = 'disaster_cleared'; // 灾害清除
- const EVENT_HARVESTED = 'harvested'; // 收获
- protected $fillable = [
- 'user_id',
- 'land_id',
- 'crop_id',
- 'seed_id',
- 'event_type',
- 'event_data',
- 'growth_stage',
- 'land_type',
- ];
- protected $casts = [
- 'event_data' => 'array',
- 'growth_stage' => 'integer',
- 'land_type' => 'integer',
- ];
- /**
- * 关联作物
- */
- public function crop(): BelongsTo
- {
- return $this->belongsTo(FarmCrop::class, 'crop_id');
- }
- /**
- * 关联种子
- */
- public function seed(): BelongsTo
- {
- return $this->belongsTo(FarmSeed::class, 'seed_id');
- }
- /**
- * 关联土地
- */
- public function land(): BelongsTo
- {
- return $this->belongsTo(FarmLand::class, 'land_id');
- }
- /**
- * 获取生长阶段名称
- */
- public function getGrowthStageNameAttribute(): string
- {
- return GROWTH_STAGE::getName($this->growth_stage);
- }
- /**
- * 获取事件类型名称
- */
- public function getEventTypeNameAttribute(): string
- {
- return match($this->event_type) {
- self::EVENT_FRUIT_CONFIRMED => '确认果实种类',
- self::EVENT_OUTPUT_CALCULATED => '确认产出数量',
- self::EVENT_DISASTER_OCCURRED => '灾害产生',
- self::EVENT_DISASTER_CLEARED => '灾害清除',
- self::EVENT_HARVESTED => '收获',
- default => '未知事件'
- };
- }
- /**
- * 作用域:按事件类型筛选
- */
- public function scopeByEventType($query, string $eventType)
- {
- return $query->where('event_type', $eventType);
- }
- /**
- * 作用域:按作物筛选
- */
- public function scopeByCrop($query, int $cropId)
- {
- return $query->where('crop_id', $cropId);
- }
- /**
- * 作用域:按用户筛选
- */
- public function scopeByUser($query, int $userId)
- {
- return $query->where('user_id', $userId);
- }
- /**
- * 静态方法:记录确认果实种类事件
- */
- public static function logFruitConfirmed(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
- {
- return self::create([
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'crop_id' => $cropId,
- 'seed_id' => $seedId,
- 'event_type' => self::EVENT_FRUIT_CONFIRMED,
- 'event_data' => $eventData,
- 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::SPROUT->value,
- 'land_type' => $eventData['land_type'] ?? 1,
- ]);
- }
- /**
- * 静态方法:记录确认产出数量事件
- */
- public static function logOutputCalculated(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
- {
- return self::create([
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'crop_id' => $cropId,
- 'seed_id' => $seedId,
- 'event_type' => self::EVENT_OUTPUT_CALCULATED,
- 'event_data' => $eventData,
- 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::MATURE->value,
- 'land_type' => $eventData['land_type'] ?? 1,
- ]);
- }
- /**
- * 静态方法:记录灾害产生事件
- */
- public static function logDisasterOccurred(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
- {
- return self::create([
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'crop_id' => $cropId,
- 'seed_id' => $seedId,
- 'event_type' => self::EVENT_DISASTER_OCCURRED,
- 'event_data' => $eventData,
- 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::GROWING->value,
- 'land_type' => $eventData['land_type'] ?? 1,
- ]);
- }
- /**
- * 静态方法:记录灾害清除事件
- */
- public static function logDisasterCleared(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
- {
- return self::create([
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'crop_id' => $cropId,
- 'seed_id' => $seedId,
- 'event_type' => self::EVENT_DISASTER_CLEARED,
- 'event_data' => $eventData,
- 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::GROWING->value,
- 'land_type' => $eventData['land_type'] ?? 1,
- ]);
- }
- /**
- * 静态方法:记录收获事件
- */
- public static function logHarvested(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
- {
- return self::create([
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'crop_id' => $cropId,
- 'seed_id' => $seedId,
- 'event_type' => self::EVENT_HARVESTED,
- 'event_data' => $eventData,
- 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::MATURE->value,
- 'land_type' => $eventData['land_type'] ?? 1,
- ]);
- }
- }
|