| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace app\crontab;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\console\Input\Argument;
- use think\Console;
- use think\Db;
- use think\Exception;
- /**
- * 聚合数据统计-每天定时执行所有统计脚本的主脚本
- * 依次执行5个统计脚本
- */
- class ComplexSummaryDaily extends Command
- {
- protected function configure()
- {
- $this->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} 个统计脚本执行失败");
- }
- }
- }
|