| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace App\Module\Farm\Models;
- use UCore\ModelCore;
- use Carbon\Carbon;
- /**
- * 农场每日统计模型
- * field start
- * @property int $id 主键ID
- * @property \Carbon\Carbon $stats_date 统计日期
- * @property int $total_users 总用户数
- * @property int $active_users 活跃用户数(当日有操作)
- * @property int $house_level_1 1级房屋数量
- * @property int $house_level_2 2级房屋数量
- * @property int $house_level_3 3级房屋数量
- * @property int $house_level_4 4级房屋数量
- * @property int $house_level_5 5级房屋数量
- * @property int $house_level_6 6级房屋数量
- * @property int $house_level_7 7级房屋数量
- * @property int $house_level_8 8级房屋数量
- * @property int $house_level_9 9级房屋数量
- * @property int $house_level_10 10级房屋数量
- * @property int $land_type_1 普通土地数量
- * @property int $land_type_2 红土地数量
- * @property int $land_type_3 黑土地数量
- * @property int $land_type_4 金色特殊土地数量
- * @property int $land_type_5 蓝色特殊土地数量
- * @property int $land_type_6 紫色特殊土地数量
- * @property int $land_status_0 空闲土地数量
- * @property int $land_status_1 种植中土地数量
- * @property int $land_status_2 灾害土地数量
- * @property int $land_status_3 可收获土地数量
- * @property int $land_status_4 枯萎土地数量
- * @property int $total_lands 总土地数量
- * @property int $total_special_lands 特殊土地总数量
- * @property int $total_crops 总作物数量
- * @property int $crops_seed_stage 种子期作物数量
- * @property int $crops_sprout_stage 发芽期作物数量
- * @property int $crops_growth_stage 生长期作物数量
- * @property int $crops_fruit_stage 果实期作物数量
- * @property int $crops_mature_stage 成熟期作物数量
- * @property int $crops_withered_stage 枯萎期作物数量
- * @property int $total_disasters 总灾害数量
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class FarmDailyStats extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'farm_daily_stats';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'stats_date',
- 'total_users',
- 'active_users',
- 'house_level_1',
- 'house_level_2',
- 'house_level_3',
- 'house_level_4',
- 'house_level_5',
- 'house_level_6',
- 'house_level_7',
- 'house_level_8',
- 'house_level_9',
- 'house_level_10',
- 'land_type_1',
- 'land_type_2',
- 'land_type_3',
- 'land_type_4',
- 'land_type_5',
- 'land_type_6',
- 'land_status_0',
- 'land_status_1',
- 'land_status_2',
- 'land_status_3',
- 'land_status_4',
- 'total_lands',
- 'total_special_lands',
- 'total_crops',
- 'crops_seed_stage',
- 'crops_sprout_stage',
- 'crops_growth_stage',
- 'crops_fruit_stage',
- 'crops_mature_stage',
- 'crops_withered_stage',
- 'total_disasters',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'stats_date' => 'date',
- 'total_users' => 'integer',
- 'active_users' => 'integer',
- 'house_level_1' => 'integer',
- 'house_level_2' => 'integer',
- 'house_level_3' => 'integer',
- 'house_level_4' => 'integer',
- 'house_level_5' => 'integer',
- 'house_level_6' => 'integer',
- 'house_level_7' => 'integer',
- 'house_level_8' => 'integer',
- 'house_level_9' => 'integer',
- 'house_level_10' => 'integer',
- 'land_type_1' => 'integer',
- 'land_type_2' => 'integer',
- 'land_type_3' => 'integer',
- 'land_type_4' => 'integer',
- 'land_type_5' => 'integer',
- 'land_type_6' => 'integer',
- 'land_status_0' => 'integer',
- 'land_status_1' => 'integer',
- 'land_status_2' => 'integer',
- 'land_status_3' => 'integer',
- 'land_status_4' => 'integer',
- 'total_lands' => 'integer',
- 'total_special_lands' => 'integer',
- 'total_crops' => 'integer',
- 'crops_seed_stage' => 'integer',
- 'crops_sprout_stage' => 'integer',
- 'crops_growth_stage' => 'integer',
- 'crops_fruit_stage' => 'integer',
- 'crops_mature_stage' => 'integer',
- 'crops_withered_stage' => 'integer',
- 'total_disasters' => 'integer',
- ];
- /**
- * 获取房屋等级统计数据
- *
- * @return array
- */
- public function getHouseLevelStatsAttribute(): array
- {
- $stats = [];
- for ($level = 1; $level <= 10; $level++) {
- $field = "house_level_{$level}";
- $stats[$level] = $this->$field ?? 0;
- }
- return $stats;
- }
- /**
- * 获取土地类型统计数据
- *
- * @return array
- */
- public function getLandTypeStatsAttribute(): array
- {
- return [
- 1 => $this->land_type_1 ?? 0, // 普通土地
- 2 => $this->land_type_2 ?? 0, // 红土地
- 3 => $this->land_type_3 ?? 0, // 黑土地
- 4 => $this->land_type_4 ?? 0, // 金色特殊土地
- 5 => $this->land_type_5 ?? 0, // 蓝色特殊土地
- 6 => $this->land_type_6 ?? 0, // 紫色特殊土地
- ];
- }
- /**
- * 获取土地状态统计数据
- *
- * @return array
- */
- public function getLandStatusStatsAttribute(): array
- {
- return [
- 0 => $this->land_status_0 ?? 0, // 空闲
- 1 => $this->land_status_1 ?? 0, // 种植中
- 2 => $this->land_status_2 ?? 0, // 灾害
- 3 => $this->land_status_3 ?? 0, // 可收获
- 4 => $this->land_status_4 ?? 0, // 枯萎
- ];
- }
- /**
- * 获取作物生长阶段统计数据
- *
- * @return array
- */
- public function getCropStageStatsAttribute(): array
- {
- return [
- 1 => $this->crops_seed_stage ?? 0, // 种子期
- 20 => $this->crops_sprout_stage ?? 0, // 发芽期
- 30 => $this->crops_growth_stage ?? 0, // 生长期
- 35 => $this->crops_fruit_stage ?? 0, // 果实期
- 40 => $this->crops_mature_stage ?? 0, // 成熟期
- 50 => $this->crops_withered_stage ?? 0, // 枯萎期
- ];
- }
- /**
- * 根据日期查找统计记录
- *
- * @param string|Carbon $date
- * @return static|null
- */
- public static function findByDate($date): ?static
- {
- if (is_string($date)) {
- $date = Carbon::parse($date);
- }
-
- return static::where('stats_date', $date->toDateString())->first();
- }
- /**
- * 获取最近N天的统计数据
- *
- * @param int $days
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function getRecentStats(int $days = 7): \Illuminate\Database\Eloquent\Collection
- {
- return static::where('stats_date', '>=', Carbon::now()->subDays($days)->toDateString())
- ->orderBy('stats_date', 'desc')
- ->get();
- }
- /**
- * 获取日期范围内的统计数据
- *
- * @param string|Carbon $startDate
- * @param string|Carbon $endDate
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function getStatsByDateRange($startDate, $endDate): \Illuminate\Database\Eloquent\Collection
- {
- if (is_string($startDate)) {
- $startDate = Carbon::parse($startDate);
- }
- if (is_string($endDate)) {
- $endDate = Carbon::parse($endDate);
- }
- return static::whereBetween('stats_date', [
- $startDate->toDateString(),
- $endDate->toDateString()
- ])->orderBy('stats_date', 'desc')->get();
- }
- }
|