option('date') ? Carbon::parse($this->option('date')) : Carbon::yesterday(); $force = $this->option('force'); $this->info("开始生成农场每日统计数据..."); $this->info("统计日期: {$date->toDateString()}"); try { // 检查是否已存在统计数据 $existingStats = FarmDailyStats::findByDate($date); if ($existingStats && !$force) { $this->warn("日期 {$date->toDateString()} 的统计数据已存在,使用 --force 参数强制重新生成"); return; } // 生成统计数据 $statsData = $this->generateStatsData($date); // 保存或更新统计数据 if ($existingStats) { $existingStats->update($statsData); $this->info("已更新统计数据"); } else { FarmDailyStats::create($statsData); $this->info("已创建新的统计数据"); } // 显示统计结果 $this->displayStatsResult($statsData); $this->info("农场每日统计数据生成完成!"); } catch (\Exception $e) { $this->error("生成统计数据时发生错误: " . $e->getMessage()); $this->error("错误位置: " . $e->getFile() . ':' . $e->getLine()); throw $e; } } /** * 生成统计数据 * * @param Carbon $date * @return array */ protected function generateStatsData(Carbon $date): array { $this->info("正在统计用户数据..."); // 统计用户数据 $totalUsers = FarmUser::count(); // 活跃用户数(当日有操作的用户,这里简化为有农场记录的用户) $activeUsers = FarmUser::whereDate('updated_at', $date)->count(); $this->info("正在统计房屋等级数据..."); // 统计房屋等级数据 $houseLevelStats = FarmUser::select('house_level', DB::raw('count(*) as count')) ->groupBy('house_level') ->pluck('count', 'house_level') ->toArray(); $this->info("正在统计土地类型数据..."); // 统计土地类型数据 $landTypeStats = FarmLand::select('land_type', DB::raw('count(*) as count')) ->groupBy('land_type') ->pluck('count', 'land_type') ->toArray(); $this->info("正在统计土地状态数据..."); // 统计土地状态数据 $landStatusStats = FarmLand::select('status', DB::raw('count(*) as count')) ->groupBy('status') ->pluck('count', 'status') ->toArray(); $this->info("正在统计作物和灾害数据..."); // 统计总土地数量 $totalLands = FarmLand::count(); // 统计特殊土地数量(类型4、5、6) $totalSpecialLands = FarmLand::whereIn('land_type', [4, 5, 6])->count(); // 统计总作物数量 $totalCrops = FarmCrop::count(); // 统计按生长阶段分组的作物数量 $this->info("正在统计作物生长阶段数据..."); $cropStageStats = FarmCrop::select('growth_stage', DB::raw('count(*) as count')) ->groupBy('growth_stage') ->pluck('count', 'growth_stage') ->toArray(); // 统计总灾害数量(有活跃灾害的作物) $totalDisasters = FarmCrop::whereNotNull('disasters') ->where('disasters', '!=', '[]') ->where('disasters', '!=', 'null') ->count(); // 组装统计数据 $statsData = [ 'stats_date' => $date->toDateString(), 'total_users' => $totalUsers, 'active_users' => $activeUsers, 'total_lands' => $totalLands, 'total_special_lands' => $totalSpecialLands, 'total_crops' => $totalCrops, 'total_disasters' => $totalDisasters, ]; // 添加房屋等级统计 for ($level = 1; $level <= 10; $level++) { $statsData["house_level_{$level}"] = $houseLevelStats[$level] ?? 0; } // 添加土地类型统计 for ($type = 1; $type <= 6; $type++) { $statsData["land_type_{$type}"] = $landTypeStats[$type] ?? 0; } // 添加土地状态统计 for ($status = 0; $status <= 4; $status++) { $statsData["land_status_{$status}"] = $landStatusStats[$status] ?? 0; } // 添加作物生长阶段统计 // 使用GROWTH_STAGE枚举的值作为键 $statsData['crops_seed_stage'] = $cropStageStats[GROWTH_STAGE::SEED->value] ?? 0; $statsData['crops_sprout_stage'] = $cropStageStats[GROWTH_STAGE::SPROUT->value] ?? 0; $statsData['crops_growth_stage'] = $cropStageStats[GROWTH_STAGE::GROWTH->value] ?? 0; $statsData['crops_fruit_stage'] = $cropStageStats[GROWTH_STAGE::FRUIT->value] ?? 0; $statsData['crops_mature_stage'] = $cropStageStats[GROWTH_STAGE::MATURE->value] ?? 0; $statsData['crops_withered_stage'] = $cropStageStats[GROWTH_STAGE::WITHERED->value] ?? 0; return $statsData; } /** * 显示统计结果 * * @param array $statsData */ protected function displayStatsResult(array $statsData): void { $this->line(''); $this->info('=== 统计结果 ==='); // 用户统计 $this->line("总用户数: {$statsData['total_users']}"); $this->line("活跃用户数: {$statsData['active_users']}"); // 房屋等级统计 $this->line(''); $this->info('房屋等级统计:'); for ($level = 1; $level <= 10; $level++) { $count = $statsData["house_level_{$level}"]; if ($count > 0) { $this->line(" {$level}级房屋: {$count}个"); } } // 土地类型统计 $this->line(''); $this->info('土地类型统计:'); $landTypeNames = [ 1 => '普通土地', 2 => '红土地', 3 => '黑土地', 4 => '金色特殊土地', 5 => '蓝色特殊土地', 6 => '紫色特殊土地', ]; for ($type = 1; $type <= 6; $type++) { $count = $statsData["land_type_{$type}"]; if ($count > 0) { $typeName = $landTypeNames[$type]; $this->line(" {$typeName}: {$count}块"); } } // 土地状态统计 $this->line(''); $this->info('土地状态统计:'); $landStatusNames = [ 0 => '空闲', 1 => '种植中', 2 => '灾害', 3 => '可收获', 4 => '枯萎', ]; for ($status = 0; $status <= 4; $status++) { $count = $statsData["land_status_{$status}"]; if ($count > 0) { $statusName = $landStatusNames[$status]; $this->line(" {$statusName}土地: {$count}块"); } } // 总计统计 $this->line(''); $this->info('总计统计:'); $this->line("总土地数: {$statsData['total_lands']}块"); $this->line("特殊土地数: {$statsData['total_special_lands']}块"); $this->line("总作物数: {$statsData['total_crops']}个"); $this->line("总灾害数: {$statsData['total_disasters']}个"); } }