FarmCropLog.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 match($this->growth_stage) {
  76. 1 => '种子期',
  77. 2 => '发芽期',
  78. 3 => '生长期',
  79. 4 => '成熟期',
  80. default => '未知阶段'
  81. };
  82. }
  83. /**
  84. * 获取事件类型名称
  85. */
  86. public function getEventTypeNameAttribute(): string
  87. {
  88. return match($this->event_type) {
  89. self::EVENT_FRUIT_CONFIRMED => '确认果实种类',
  90. self::EVENT_OUTPUT_CALCULATED => '确认产出数量',
  91. self::EVENT_DISASTER_OCCURRED => '灾害产生',
  92. self::EVENT_DISASTER_CLEARED => '灾害清除',
  93. self::EVENT_HARVESTED => '收获',
  94. default => '未知事件'
  95. };
  96. }
  97. /**
  98. * 作用域:按事件类型筛选
  99. */
  100. public function scopeByEventType($query, string $eventType)
  101. {
  102. return $query->where('event_type', $eventType);
  103. }
  104. /**
  105. * 作用域:按作物筛选
  106. */
  107. public function scopeByCrop($query, int $cropId)
  108. {
  109. return $query->where('crop_id', $cropId);
  110. }
  111. /**
  112. * 作用域:按用户筛选
  113. */
  114. public function scopeByUser($query, int $userId)
  115. {
  116. return $query->where('user_id', $userId);
  117. }
  118. /**
  119. * 静态方法:记录确认果实种类事件
  120. */
  121. public static function logFruitConfirmed(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  122. {
  123. return self::create([
  124. 'user_id' => $userId,
  125. 'land_id' => $landId,
  126. 'crop_id' => $cropId,
  127. 'seed_id' => $seedId,
  128. 'event_type' => self::EVENT_FRUIT_CONFIRMED,
  129. 'event_data' => $eventData,
  130. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::SPROUT->value,
  131. 'land_type' => $eventData['land_type'] ?? 1,
  132. ]);
  133. }
  134. /**
  135. * 静态方法:记录确认产出数量事件
  136. */
  137. public static function logOutputCalculated(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  138. {
  139. return self::create([
  140. 'user_id' => $userId,
  141. 'land_id' => $landId,
  142. 'crop_id' => $cropId,
  143. 'seed_id' => $seedId,
  144. 'event_type' => self::EVENT_OUTPUT_CALCULATED,
  145. 'event_data' => $eventData,
  146. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::MATURE->value,
  147. 'land_type' => $eventData['land_type'] ?? 1,
  148. ]);
  149. }
  150. /**
  151. * 静态方法:记录灾害产生事件
  152. */
  153. public static function logDisasterOccurred(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  154. {
  155. return self::create([
  156. 'user_id' => $userId,
  157. 'land_id' => $landId,
  158. 'crop_id' => $cropId,
  159. 'seed_id' => $seedId,
  160. 'event_type' => self::EVENT_DISASTER_OCCURRED,
  161. 'event_data' => $eventData,
  162. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::GROWING->value,
  163. 'land_type' => $eventData['land_type'] ?? 1,
  164. ]);
  165. }
  166. /**
  167. * 静态方法:记录灾害清除事件
  168. */
  169. public static function logDisasterCleared(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  170. {
  171. return self::create([
  172. 'user_id' => $userId,
  173. 'land_id' => $landId,
  174. 'crop_id' => $cropId,
  175. 'seed_id' => $seedId,
  176. 'event_type' => self::EVENT_DISASTER_CLEARED,
  177. 'event_data' => $eventData,
  178. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::GROWING->value,
  179. 'land_type' => $eventData['land_type'] ?? 1,
  180. ]);
  181. }
  182. /**
  183. * 静态方法:记录收获事件
  184. */
  185. public static function logHarvested(int $userId, int $landId, int $cropId, int $seedId, array $eventData): self
  186. {
  187. return self::create([
  188. 'user_id' => $userId,
  189. 'land_id' => $landId,
  190. 'crop_id' => $cropId,
  191. 'seed_id' => $seedId,
  192. 'event_type' => self::EVENT_HARVESTED,
  193. 'event_data' => $eventData,
  194. 'growth_stage' => $eventData['growth_stage'] ?? GROWTH_STAGE::MATURE->value,
  195. 'land_type' => $eventData['land_type'] ?? 1,
  196. ]);
  197. }
  198. }