FarmDailyStats.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 $total_disasters 总灾害数量
  37. * @property \Carbon\Carbon $created_at 创建时间
  38. * @property \Carbon\Carbon $updated_at 更新时间
  39. * field end
  40. */
  41. class FarmDailyStats extends ModelCore
  42. {
  43. /**
  44. * 与模型关联的表名
  45. *
  46. * @var string
  47. */
  48. protected $table = 'farm_daily_stats';
  49. /**
  50. * 可批量赋值的属性
  51. *
  52. * @var array
  53. */
  54. protected $fillable = [
  55. 'stats_date',
  56. 'total_users',
  57. 'active_users',
  58. 'house_level_1',
  59. 'house_level_2',
  60. 'house_level_3',
  61. 'house_level_4',
  62. 'house_level_5',
  63. 'house_level_6',
  64. 'house_level_7',
  65. 'house_level_8',
  66. 'house_level_9',
  67. 'house_level_10',
  68. 'land_type_1',
  69. 'land_type_2',
  70. 'land_type_3',
  71. 'land_type_4',
  72. 'land_type_5',
  73. 'land_type_6',
  74. 'land_status_0',
  75. 'land_status_1',
  76. 'land_status_2',
  77. 'land_status_3',
  78. 'land_status_4',
  79. 'total_lands',
  80. 'total_special_lands',
  81. 'total_crops',
  82. 'total_disasters',
  83. ];
  84. /**
  85. * 应该被转换为原生类型的属性
  86. *
  87. * @var array
  88. */
  89. protected $casts = [
  90. 'stats_date' => 'date',
  91. 'total_users' => 'integer',
  92. 'active_users' => 'integer',
  93. 'house_level_1' => 'integer',
  94. 'house_level_2' => 'integer',
  95. 'house_level_3' => 'integer',
  96. 'house_level_4' => 'integer',
  97. 'house_level_5' => 'integer',
  98. 'house_level_6' => 'integer',
  99. 'house_level_7' => 'integer',
  100. 'house_level_8' => 'integer',
  101. 'house_level_9' => 'integer',
  102. 'house_level_10' => 'integer',
  103. 'land_type_1' => 'integer',
  104. 'land_type_2' => 'integer',
  105. 'land_type_3' => 'integer',
  106. 'land_type_4' => 'integer',
  107. 'land_type_5' => 'integer',
  108. 'land_type_6' => 'integer',
  109. 'land_status_0' => 'integer',
  110. 'land_status_1' => 'integer',
  111. 'land_status_2' => 'integer',
  112. 'land_status_3' => 'integer',
  113. 'land_status_4' => 'integer',
  114. 'total_lands' => 'integer',
  115. 'total_special_lands' => 'integer',
  116. 'total_crops' => 'integer',
  117. 'total_disasters' => 'integer',
  118. ];
  119. /**
  120. * 获取房屋等级统计数据
  121. *
  122. * @return array
  123. */
  124. public function getHouseLevelStatsAttribute(): array
  125. {
  126. $stats = [];
  127. for ($level = 1; $level <= 10; $level++) {
  128. $field = "house_level_{$level}";
  129. $stats[$level] = $this->$field ?? 0;
  130. }
  131. return $stats;
  132. }
  133. /**
  134. * 获取土地类型统计数据
  135. *
  136. * @return array
  137. */
  138. public function getLandTypeStatsAttribute(): array
  139. {
  140. return [
  141. 1 => $this->land_type_1 ?? 0, // 普通土地
  142. 2 => $this->land_type_2 ?? 0, // 红土地
  143. 3 => $this->land_type_3 ?? 0, // 黑土地
  144. 4 => $this->land_type_4 ?? 0, // 金色特殊土地
  145. 5 => $this->land_type_5 ?? 0, // 蓝色特殊土地
  146. 6 => $this->land_type_6 ?? 0, // 紫色特殊土地
  147. ];
  148. }
  149. /**
  150. * 获取土地状态统计数据
  151. *
  152. * @return array
  153. */
  154. public function getLandStatusStatsAttribute(): array
  155. {
  156. return [
  157. 0 => $this->land_status_0 ?? 0, // 空闲
  158. 1 => $this->land_status_1 ?? 0, // 种植中
  159. 2 => $this->land_status_2 ?? 0, // 灾害
  160. 3 => $this->land_status_3 ?? 0, // 可收获
  161. 4 => $this->land_status_4 ?? 0, // 枯萎
  162. ];
  163. }
  164. /**
  165. * 根据日期查找统计记录
  166. *
  167. * @param string|Carbon $date
  168. * @return static|null
  169. */
  170. public static function findByDate($date): ?static
  171. {
  172. if (is_string($date)) {
  173. $date = Carbon::parse($date);
  174. }
  175. return static::where('stats_date', $date->toDateString())->first();
  176. }
  177. /**
  178. * 获取最近N天的统计数据
  179. *
  180. * @param int $days
  181. * @return \Illuminate\Database\Eloquent\Collection
  182. */
  183. public static function getRecentStats(int $days = 7): \Illuminate\Database\Eloquent\Collection
  184. {
  185. return static::where('stats_date', '>=', Carbon::now()->subDays($days)->toDateString())
  186. ->orderBy('stats_date', 'desc')
  187. ->get();
  188. }
  189. /**
  190. * 获取日期范围内的统计数据
  191. *
  192. * @param string|Carbon $startDate
  193. * @param string|Carbon $endDate
  194. * @return \Illuminate\Database\Eloquent\Collection
  195. */
  196. public static function getStatsByDateRange($startDate, $endDate): \Illuminate\Database\Eloquent\Collection
  197. {
  198. if (is_string($startDate)) {
  199. $startDate = Carbon::parse($startDate);
  200. }
  201. if (is_string($endDate)) {
  202. $endDate = Carbon::parse($endDate);
  203. }
  204. return static::whereBetween('stats_date', [
  205. $startDate->toDateString(),
  206. $endDate->toDateString()
  207. ])->orderBy('stats_date', 'desc')->get();
  208. }
  209. }