FarmDailyStats.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Module\Farm\Models;
  3. use UCore\ModelCore;
  4. use Carbon\Carbon;
  5. /**
  6. * 农场每日统计模型
  7. * field start
  8. * @property int $id 主键ID
  9. * @property \Carbon\Carbon $stats_date 统计日期
  10. * @property int $total_users 总用户数
  11. * @property int $active_users 活跃用户数(当日有操作)
  12. * @property int $house_level_1 1级房屋数量
  13. * @property int $house_level_2 2级房屋数量
  14. * @property int $house_level_3 3级房屋数量
  15. * @property int $house_level_4 4级房屋数量
  16. * @property int $house_level_5 5级房屋数量
  17. * @property int $house_level_6 6级房屋数量
  18. * @property int $house_level_7 7级房屋数量
  19. * @property int $house_level_8 8级房屋数量
  20. * @property int $house_level_9 9级房屋数量
  21. * @property int $house_level_10 10级房屋数量
  22. * @property int $land_type_1 普通土地数量
  23. * @property int $land_type_2 红土地数量
  24. * @property int $land_type_3 黑土地数量
  25. * @property int $land_type_4 金色特殊土地数量
  26. * @property int $land_type_5 蓝色特殊土地数量
  27. * @property int $land_type_6 紫色特殊土地数量
  28. * @property int $land_status_0 空闲土地数量
  29. * @property int $land_status_1 种植中土地数量
  30. * @property int $land_status_2 灾害土地数量
  31. * @property int $land_status_3 可收获土地数量
  32. * @property int $land_status_4 枯萎土地数量
  33. * @property int $total_lands 总土地数量
  34. * @property int $total_special_lands 特殊土地总数量
  35. * @property int $total_crops 总作物数量
  36. * @property int $crops_seed_stage 种子期作物数量
  37. * @property int $crops_sprout_stage 发芽期作物数量
  38. * @property int $crops_growth_stage 生长期作物数量
  39. * @property int $crops_fruit_stage 果实期作物数量
  40. * @property int $crops_mature_stage 成熟期作物数量
  41. * @property int $crops_withered_stage 枯萎期作物数量
  42. * @property int $total_disasters 总灾害数量
  43. * @property \Carbon\Carbon $created_at 创建时间
  44. * @property \Carbon\Carbon $updated_at 更新时间
  45. * field end
  46. */
  47. class FarmDailyStats extends ModelCore
  48. {
  49. /**
  50. * 与模型关联的表名
  51. *
  52. * @var string
  53. */
  54. protected $table = 'farm_daily_stats';
  55. /**
  56. * 可批量赋值的属性
  57. *
  58. * @var array
  59. */
  60. protected $fillable = [
  61. 'stats_date',
  62. 'total_users',
  63. 'active_users',
  64. 'house_level_1',
  65. 'house_level_2',
  66. 'house_level_3',
  67. 'house_level_4',
  68. 'house_level_5',
  69. 'house_level_6',
  70. 'house_level_7',
  71. 'house_level_8',
  72. 'house_level_9',
  73. 'house_level_10',
  74. 'land_type_1',
  75. 'land_type_2',
  76. 'land_type_3',
  77. 'land_type_4',
  78. 'land_type_5',
  79. 'land_type_6',
  80. 'land_status_0',
  81. 'land_status_1',
  82. 'land_status_2',
  83. 'land_status_3',
  84. 'land_status_4',
  85. 'total_lands',
  86. 'total_special_lands',
  87. 'total_crops',
  88. 'crops_seed_stage',
  89. 'crops_sprout_stage',
  90. 'crops_growth_stage',
  91. 'crops_fruit_stage',
  92. 'crops_mature_stage',
  93. 'crops_withered_stage',
  94. 'total_disasters',
  95. ];
  96. /**
  97. * 应该被转换为原生类型的属性
  98. *
  99. * @var array
  100. */
  101. protected $casts = [
  102. 'stats_date' => 'date',
  103. 'total_users' => 'integer',
  104. 'active_users' => 'integer',
  105. 'house_level_1' => 'integer',
  106. 'house_level_2' => 'integer',
  107. 'house_level_3' => 'integer',
  108. 'house_level_4' => 'integer',
  109. 'house_level_5' => 'integer',
  110. 'house_level_6' => 'integer',
  111. 'house_level_7' => 'integer',
  112. 'house_level_8' => 'integer',
  113. 'house_level_9' => 'integer',
  114. 'house_level_10' => 'integer',
  115. 'land_type_1' => 'integer',
  116. 'land_type_2' => 'integer',
  117. 'land_type_3' => 'integer',
  118. 'land_type_4' => 'integer',
  119. 'land_type_5' => 'integer',
  120. 'land_type_6' => 'integer',
  121. 'land_status_0' => 'integer',
  122. 'land_status_1' => 'integer',
  123. 'land_status_2' => 'integer',
  124. 'land_status_3' => 'integer',
  125. 'land_status_4' => 'integer',
  126. 'total_lands' => 'integer',
  127. 'total_special_lands' => 'integer',
  128. 'total_crops' => 'integer',
  129. 'crops_seed_stage' => 'integer',
  130. 'crops_sprout_stage' => 'integer',
  131. 'crops_growth_stage' => 'integer',
  132. 'crops_fruit_stage' => 'integer',
  133. 'crops_mature_stage' => 'integer',
  134. 'crops_withered_stage' => 'integer',
  135. 'total_disasters' => 'integer',
  136. ];
  137. /**
  138. * 获取房屋等级统计数据
  139. *
  140. * @return array
  141. */
  142. public function getHouseLevelStatsAttribute(): array
  143. {
  144. $stats = [];
  145. for ($level = 1; $level <= 10; $level++) {
  146. $field = "house_level_{$level}";
  147. $stats[$level] = $this->$field ?? 0;
  148. }
  149. return $stats;
  150. }
  151. /**
  152. * 获取土地类型统计数据
  153. *
  154. * @return array
  155. */
  156. public function getLandTypeStatsAttribute(): array
  157. {
  158. return [
  159. 1 => $this->land_type_1 ?? 0, // 普通土地
  160. 2 => $this->land_type_2 ?? 0, // 红土地
  161. 3 => $this->land_type_3 ?? 0, // 黑土地
  162. 4 => $this->land_type_4 ?? 0, // 金色特殊土地
  163. 5 => $this->land_type_5 ?? 0, // 蓝色特殊土地
  164. 6 => $this->land_type_6 ?? 0, // 紫色特殊土地
  165. ];
  166. }
  167. /**
  168. * 获取土地状态统计数据
  169. *
  170. * @return array
  171. */
  172. public function getLandStatusStatsAttribute(): array
  173. {
  174. return [
  175. 0 => $this->land_status_0 ?? 0, // 空闲
  176. 1 => $this->land_status_1 ?? 0, // 种植中
  177. 2 => $this->land_status_2 ?? 0, // 灾害
  178. 3 => $this->land_status_3 ?? 0, // 可收获
  179. 4 => $this->land_status_4 ?? 0, // 枯萎
  180. ];
  181. }
  182. /**
  183. * 获取作物生长阶段统计数据
  184. *
  185. * @return array
  186. */
  187. public function getCropStageStatsAttribute(): array
  188. {
  189. return [
  190. 1 => $this->crops_seed_stage ?? 0, // 种子期
  191. 20 => $this->crops_sprout_stage ?? 0, // 发芽期
  192. 30 => $this->crops_growth_stage ?? 0, // 生长期
  193. 35 => $this->crops_fruit_stage ?? 0, // 果实期
  194. 40 => $this->crops_mature_stage ?? 0, // 成熟期
  195. 50 => $this->crops_withered_stage ?? 0, // 枯萎期
  196. ];
  197. }
  198. /**
  199. * 根据日期查找统计记录
  200. *
  201. * @param string|Carbon $date
  202. * @return static|null
  203. */
  204. public static function findByDate($date): ?static
  205. {
  206. if (is_string($date)) {
  207. $date = Carbon::parse($date);
  208. }
  209. return static::where('stats_date', $date->toDateString())->first();
  210. }
  211. /**
  212. * 获取最近N天的统计数据
  213. *
  214. * @param int $days
  215. * @return \Illuminate\Database\Eloquent\Collection
  216. */
  217. public static function getRecentStats(int $days = 7): \Illuminate\Database\Eloquent\Collection
  218. {
  219. return static::where('stats_date', '>=', Carbon::now()->subDays($days)->toDateString())
  220. ->orderBy('stats_date', 'desc')
  221. ->get();
  222. }
  223. /**
  224. * 获取日期范围内的统计数据
  225. *
  226. * @param string|Carbon $startDate
  227. * @param string|Carbon $endDate
  228. * @return \Illuminate\Database\Eloquent\Collection
  229. */
  230. public static function getStatsByDateRange($startDate, $endDate): \Illuminate\Database\Eloquent\Collection
  231. {
  232. if (is_string($startDate)) {
  233. $startDate = Carbon::parse($startDate);
  234. }
  235. if (is_string($endDate)) {
  236. $endDate = Carbon::parse($endDate);
  237. }
  238. return static::whereBetween('stats_date', [
  239. $startDate->toDateString(),
  240. $endDate->toDateString()
  241. ])->orderBy('stats_date', 'desc')->get();
  242. }
  243. }