ComplexSummaryComplex.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\crontab;
  3. use app\service\ComplexSummaryService;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\console\Input\Argument;
  8. use think\Db;
  9. use think\Exception;
  10. /**
  11. * 聚合-渠道数据汇总-统计脚本
  12. * 统计渠道数据汇总
  13. */
  14. class ComplexSummaryComplex extends Command
  15. {
  16. protected function configure()
  17. {
  18. $this->setName('ComplexSummaryComplex')
  19. ->setDescription('聚合-渠道数据汇总-统计脚本')
  20. ->addArgument('day', Argument::OPTIONAL, '统计日期,格式:Y-m-d,默认为昨天');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $service = new ComplexSummaryService();
  25. // 获取统计日期(默认昨天)
  26. $day = $input->getArgument('day') ?: date('Y-m-d', strtotime('-1 day'));
  27. $output->writeln(date('Y-m-d H:i:s') . " 开始统计日期: {$day}");
  28. $gameIds = $service->getOnlineGameIds();
  29. try {
  30. // 1. 获取新增玩家数据
  31. $regNumList = $service->getRegNum($day, $gameIds, 1);
  32. $output->writeln("新增玩家统计完成: " . count($regNumList) . " 条");
  33. // 2. 获取新增付费玩家(当天有充值的用户)
  34. $payUserNumList = $service->getNewUserPayNum($day, $gameIds, 1);
  35. $output->writeln("新增付费玩家统计完成: " . count($payUserNumList) . " 条");
  36. // 3. 获取活跃玩家(使用玩家活跃日志表)
  37. $activeUserList = $service->getActiveUserByActiveLog($day, $gameIds, 1, 0);
  38. $output->writeln("活跃玩家统计完成: " . count($activeUserList) . " 条");
  39. // 4. 订单金额+实付金额
  40. $payMoneyList = $service->getPayMoney($day, $gameIds, 1, 0);
  41. $output->writeln("订单金额+实付金额统计完成: " . count($payMoneyList) . " 条");
  42. // 5. 合并所有统计数据
  43. $allStats = $service->mergeStatsData([
  44. $regNumList,
  45. $payUserNumList,
  46. $activeUserList,
  47. $payMoneyList
  48. ]);
  49. // 6. 保存数据
  50. // 先清空当天日期的数据,避免重新抓取时数据错乱(在事务外执行,确保清空操作生效)
  51. Db::table('cy_complex_summary_complex')
  52. ->where('day', $day)
  53. ->delete();
  54. Db::startTrans();
  55. try {
  56. foreach ($allStats as $stat) {
  57. $gameId = $stat['game_id'] ?? 0;
  58. $complexId = $stat['channel_id'] ?? 0;
  59. // 准备保存的数据
  60. $saveData = [
  61. 'day' => $day,
  62. 'game_id' => $gameId,
  63. 'complex_id' => $complexId,
  64. 'reg_num' => intval($stat['reg_num'] ?? 0),
  65. 'pay_user_num' => intval($stat['reg_pay_num'] ?? 0),
  66. 'active_user_num' => intval($stat['active_user_num'] ?? 0),
  67. 'pay_total' => floatval($stat['pay_money'] ?? 0),
  68. 'pay_amount_total' => floatval($stat['pay_amount_total'] ?? 0),
  69. ];
  70. // 直接插入(已清空当天数据,无需检查是否存在)
  71. Db::table('cy_complex_summary_complex')->insert($saveData);
  72. // 以下代码已注释,防止以后需要时可以恢复
  73. // // 检查记录是否存在
  74. // $exists = Db::table('cy_complex_summary_complex')
  75. // ->where([
  76. // 'day' => $day,
  77. // 'game_id' => $gameId,
  78. // 'complex_id' => $complexId
  79. // ])
  80. // ->find();
  81. //
  82. // if ($exists) {
  83. // // 更新
  84. // Db::table('cy_complex_summary_complex')
  85. // ->where(['id' => $exists['id']])
  86. // ->update($saveData);
  87. // } else {
  88. // // 插入
  89. // Db::table('cy_complex_summary_complex')->insert($saveData);
  90. // }
  91. }
  92. Db::commit();
  93. $output->writeln("数据保存成功,共处理 " . count($allStats) . " 条记录");
  94. } catch (Exception $e) {
  95. Db::rollback();
  96. $output->writeln("保存数据失败: " . $e->getMessage());
  97. throw $e;
  98. }
  99. } catch (Exception $e) {
  100. $output->writeln("统计失败: " . $e->getMessage() . ' - ' . $e->getFile() . ":" . $e->getLine());
  101. throw $e;
  102. }
  103. $output->writeln(date('Y-m-d H:i:s') . " 统计完成");
  104. }
  105. }