| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace App\Module\Farm\Commands;
- use App\Module\Farm\Models\FarmDailyStats;
- use App\Module\Farm\Models\FarmUser;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmCrop;
- use UCore\Command\Command;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- /**
- * 生成农场每日统计数据命令
- */
- class GenerateFarmDailyStatsCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'farm:generate-daily-stats
- {--date= : 指定统计日期 (Y-m-d 格式,默认为昨天)}
- {--force : 强制重新生成已存在的统计数据}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '生成农场每日统计数据,包括土地和房屋等级统计';
- /**
- * 执行命令的具体逻辑
- *
- * @return void
- */
- public function handleRun(): void
- {
- $date = $this->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();
-
- // 统计总灾害数量(有活跃灾害的作物)
- $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;
- }
- 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']}个");
- }
- }
|