'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(); } }