ComplexSummaryDaily.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\crontab;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. use think\console\Input\Argument;
  7. use think\Console;
  8. use think\Db;
  9. use think\Exception;
  10. /**
  11. * 聚合数据统计-每天定时执行所有统计脚本的主脚本
  12. * 依次执行5个统计脚本
  13. */
  14. class ComplexSummaryDaily extends Command
  15. {
  16. protected function configure()
  17. {
  18. $this->setName('ComplexSummaryDaily')
  19. ->setDescription('聚合数据统计-每天定时执行所有统计脚本')
  20. ->addArgument('day', Argument::OPTIONAL, '统计日期,格式:Y-m-d,默认为昨天');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $startTime = time();
  25. $output->writeln("==========================================");
  26. $output->writeln(date('Y-m-d H:i:s') . " 开始执行聚合数据统计任务");
  27. $output->writeln("==========================================");
  28. // 获取统计日期(默认昨天)
  29. $day = $input->getArgument('day') ?: date('Y-m-d', strtotime('-1 day'));
  30. // $day = "2026-07-17";
  31. $scripts = [
  32. 'ComplexSummaryGame' => '游戏汇总表统计',
  33. 'ComplexSummaryComplex' => '渠道汇总表统计',
  34. 'ComplexSummaryGameDay' => '游戏单日报表统计',
  35. 'ComplexSummaryGameRetention' => '游戏留存报表统计',
  36. 'ComplexSummaryGameServer' => '游戏区服数据统计',
  37. ];
  38. $successCount = 0;
  39. $failCount = 0;
  40. $errors = [];
  41. foreach ($scripts as $scriptName => $scriptDesc) {
  42. $output->writeln("");
  43. $output->writeln(">>> 开始执行: {$scriptDesc} ({$scriptName})");
  44. $output->writeln("------------------------------------------");
  45. try {
  46. // 创建命令实例
  47. $scriptClass = "\\app\\crontab\\{$scriptName}";
  48. $script = new $scriptClass();
  49. // 创建包含参数的数组(不包含命令名)
  50. $parameters = [];
  51. if ($day) {
  52. $parameters[] = $day;
  53. }
  54. // 创建 Input 对象并绑定命令定义
  55. $scriptInput = new Input($parameters);
  56. // 调用 run 方法(会自动处理参数绑定和验证)
  57. $script->run($scriptInput, $output);
  58. $successCount++;
  59. $output->writeln(">>> 完成: {$scriptDesc} - 成功");
  60. } catch (Exception $e) {
  61. $failCount++;
  62. $errorMsg = "{$scriptDesc} 执行失败: " . $e->getMessage();
  63. $errors[] = $errorMsg;
  64. $output->writeln(">>> 失败: {$errorMsg}");
  65. $output->writeln("错误文件: " . $e->getFile() . " 行号: " . $e->getLine());
  66. }
  67. }
  68. $endTime = time();
  69. $duration = $endTime - $startTime;
  70. $output->writeln("");
  71. $output->writeln("==========================================");
  72. $output->writeln(date('Y-m-d H:i:s') . " 聚合数据统计任务执行完成");
  73. $output->writeln("统计日期: {$day}");
  74. $output->writeln("成功: {$successCount} 个脚本");
  75. $output->writeln("失败: {$failCount} 个脚本");
  76. $output->writeln("总耗时: {$duration} 秒");
  77. if (!empty($errors)) {
  78. $output->writeln("");
  79. $output->writeln("错误详情:");
  80. foreach ($errors as $error) {
  81. $output->writeln(" - {$error}");
  82. }
  83. }
  84. $output->writeln("==========================================");
  85. // 如果有失败的脚本,抛出异常
  86. if ($failCount > 0) {
  87. throw new Exception("有 {$failCount} 个统计脚本执行失败");
  88. }
  89. }
  90. }