FarmCropLog.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Module\Farm\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use App\Module\Farm\Enums\GROWTH_STAGE;
  6. /**
  7. * 作物事件日志模型
  8. *
  9. * @property int $id 主键ID
  10. * @property int $user_id 用户ID
  11. * @property int $land_id 土地ID
  12. * @property int $crop_id 作物ID
  13. * @property int $seed_id 种子ID
  14. * @property string $event_type 事件类型
  15. * @property array|null $event_data 事件详细数据
  16. * @property int $growth_stage 发生时的生长阶段
  17. * @property int $land_type 土地类型
  18. * @property \Carbon\Carbon $created_at 事件发生时间
  19. * @property \Carbon\Carbon $updated_at 更新时间
  20. *
  21. * @property-read FarmCrop $crop 关联的作物
  22. * @property-read FarmSeed $seed 关联的种子
  23. * @property-read FarmLand $land 关联的土地
  24. */
  25. class FarmCropLog extends ModelCore
  26. {
  27. protected $table = 'farm_crop_logs';
  28. // 事件类型常量
  29. const EVENT_FRUIT_CONFIRMED = 'fruit_confirmed'; // 确认果实种类
  30. const EVENT_OUTPUT_CALCULATED = 'output_calculated'; // 确认产出数量
  31. const EVENT_DISASTER_OCCURRED = 'disaster_occurred'; // 灾害产生
  32. const EVENT_DISASTER_CLEARED = 'disaster_cleared'; // 灾害清除
  33. const EVENT_HARVESTED = 'harvested'; // 收获
  34. protected $fillable = [
  35. 'user_id',
  36. 'land_id',
  37. 'crop_id',
  38. 'seed_id',
  39. 'event_type',
  40. 'event_data',
  41. 'growth_stage',
  42. 'land_type',
  43. ];
  44. protected $casts = [
  45. 'event_data' => 'array',
  46. 'growth_stage' => 'integer',
  47. 'land_type' => 'integer',
  48. ];
  49. /**
  50. * 关联作物
  51. */
  52. public function crop(): BelongsTo
  53. {
  54. return $this->belongsTo(FarmCrop::class, 'crop_id');
  55. }
  56. /**
  57. * 关联种子
  58. */
  59. public function seed(): BelongsTo
  60. {
  61. return $this->belongsTo(FarmSeed::class, 'seed_id');
  62. }
  63. /**
  64. * 关联土地
  65. */
  66. public function land(): BelongsTo
  67. {
  68. return $this->belongsTo(FarmLand::class, 'land_id');
  69. }
  70. /**
  71. * 获取生长阶段名称
  72. */
  73. public function getGrowthStageNameAttribute(): string
  74. {
  75. return GROWTH_STAGE::getName($this->growth_stage);
  76. }
  77. /**
  78. * 获取事件类型名称
  79. */
  80. public function getEventTypeNameAttribute(): string
  81. {
  82. return match($this->event_type) {
  83. self::EVENT_FRUIT_CONFIRMED => '确认果实种类',
  84. self::EVENT_OUTPUT_CALCULATED => '确认产出数量',
  85. self::EVENT_DISASTER_OCCURRED => '灾害产生',
  86. self::EVENT_DISASTER_CLEARED => '灾害清除',
  87. self::EVENT_HARVESTED => '收获',
  88. default => '未知事件'
  89. };
  90. }
  91. /**
  92. * 作用域:按事件类型筛选
  93. */
  94. public function scopeByEventType($query, string $eventType)
  95. {
  96. return $query->where('event_type', $eventType);
  97. }
  98. /**
  99. * 作用域:按作物筛选
  100. */
  101. public function scopeByCrop($query, int $cropId)
  102. {
  103. return $query->where('crop_id', $cropId);
  104. }
  105. /**
  106. * 作用域:按用户筛选
  107. */
  108. public function scopeByUser($query, int $userId)
  109. {
  110. return $query->where('user_id', $userId);
  111. }
  112. /**
  113. * 静态方法:记录确认果实种类事件
  114. */
  115. public static function logFruitConfirmed(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  116. {
  117. return self::create([
  118. 'user_id' => $userId,
  119. 'land_id' => $landId,
  120. 'crop_id' => $cropId,
  121. 'seed_id' => $seedId,
  122. 'event_type' => self::EVENT_FRUIT_CONFIRMED,
  123. 'event_data' => $eventData,
  124. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::SPROUT->value,
  125. 'land_type' => $eventData['land_type'] ?? 1,
  126. ]);
  127. }
  128. /**
  129. * 静态方法:记录确认产出数量事件
  130. */
  131. public static function logOutputCalculated(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  132. {
  133. return self::create([
  134. 'user_id' => $userId,
  135. 'land_id' => $landId,
  136. 'crop_id' => $cropId,
  137. 'seed_id' => $seedId,
  138. 'event_type' => self::EVENT_OUTPUT_CALCULATED,
  139. 'event_data' => $eventData,
  140. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::MATURE->value,
  141. 'land_type' => $eventData['land_type'] ?? 1,
  142. ]);
  143. }
  144. /**
  145. * 静态方法:记录灾害产生事件
  146. */
  147. public static function logDisasterOccurred(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  148. {
  149. return self::create([
  150. 'user_id' => $userId,
  151. 'land_id' => $landId,
  152. 'crop_id' => $cropId,
  153. 'seed_id' => $seedId,
  154. 'event_type' => self::EVENT_DISASTER_OCCURRED,
  155. 'event_data' => $eventData,
  156. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::GROWING->value,
  157. 'land_type' => $eventData['land_type'] ?? 1,
  158. ]);
  159. }
  160. /**
  161. * 静态方法:记录灾害清除事件
  162. */
  163. public static function logDisasterCleared(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  164. {
  165. return self::create([
  166. 'user_id' => $userId,
  167. 'land_id' => $landId,
  168. 'crop_id' => $cropId,
  169. 'seed_id' => $seedId,
  170. 'event_type' => self::EVENT_DISASTER_CLEARED,
  171. 'event_data' => $eventData,
  172. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::GROWING->value,
  173. 'land_type' => $eventData['land_type'] ?? 1,
  174. ]);
  175. }
  176. /**
  177. * 静态方法:记录收获事件
  178. */
  179. public static function logHarvested(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  180. {
  181. return self::create([
  182. 'user_id' => $userId,
  183. 'land_id' => $landId,
  184. 'crop_id' => $cropId,
  185. 'seed_id' => $seedId,
  186. 'event_type' => self::EVENT_HARVESTED,
  187. 'event_data' => $eventData,
  188. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::MATURE->value,
  189. 'land_type' => $eventData['land_type'] ?? 1,
  190. ]);
  191. }
  192. }