setName('ComplexSummaryDaily') ->setDescription('聚合数据统计-每天定时执行所有统计脚本') ->addArgument('day', Argument::OPTIONAL, '统计日期,格式:Y-m-d,默认为昨天'); } protected function execute(Input $input, Output $output) { $startTime = time(); $output->writeln("=========================================="); $output->writeln(date('Y-m-d H:i:s') . " 开始执行聚合数据统计任务"); $output->writeln("=========================================="); // 获取统计日期(默认昨天) $day = $input->getArgument('day') ?: date('Y-m-d', strtotime('-1 day')); // $day = "2026-07-17"; $scripts = [ 'ComplexSummaryGame' => '游戏汇总表统计', 'ComplexSummaryComplex' => '渠道汇总表统计', 'ComplexSummaryGameDay' => '游戏单日报表统计', 'ComplexSummaryGameRetention' => '游戏留存报表统计', 'ComplexSummaryGameServer' => '游戏区服数据统计', ]; $successCount = 0; $failCount = 0; $errors = []; foreach ($scripts as $scriptName => $scriptDesc) { $output->writeln(""); $output->writeln(">>> 开始执行: {$scriptDesc} ({$scriptName})"); $output->writeln("------------------------------------------"); try { // 创建命令实例 $scriptClass = "\\app\\crontab\\{$scriptName}"; $script = new $scriptClass(); // 创建包含参数的数组(不包含命令名) $parameters = []; if ($day) { $parameters[] = $day; } // 创建 Input 对象并绑定命令定义 $scriptInput = new Input($parameters); // 调用 run 方法(会自动处理参数绑定和验证) $script->run($scriptInput, $output); $successCount++; $output->writeln(">>> 完成: {$scriptDesc} - 成功"); } catch (Exception $e) { $failCount++; $errorMsg = "{$scriptDesc} 执行失败: " . $e->getMessage(); $errors[] = $errorMsg; $output->writeln(">>> 失败: {$errorMsg}"); $output->writeln("错误文件: " . $e->getFile() . " 行号: " . $e->getLine()); } } $endTime = time(); $duration = $endTime - $startTime; $output->writeln(""); $output->writeln("=========================================="); $output->writeln(date('Y-m-d H:i:s') . " 聚合数据统计任务执行完成"); $output->writeln("统计日期: {$day}"); $output->writeln("成功: {$successCount} 个脚本"); $output->writeln("失败: {$failCount} 个脚本"); $output->writeln("总耗时: {$duration} 秒"); if (!empty($errors)) { $output->writeln(""); $output->writeln("错误详情:"); foreach ($errors as $error) { $output->writeln(" - {$error}"); } } $output->writeln("=========================================="); // 如果有失败的脚本,抛出异常 if ($failCount > 0) { throw new Exception("有 {$failCount} 个统计脚本执行失败"); } } }