| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace app\crontab;
- use app\service\ComplexSummaryService;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\console\Input\Argument;
- use think\Db;
- use think\Exception;
- /**
- * 聚合-渠道数据汇总-统计脚本
- * 统计渠道数据汇总
- */
- class ComplexSummaryComplex extends Command
- {
- protected function configure()
- {
- $this->setName('ComplexSummaryComplex')
- ->setDescription('聚合-渠道数据汇总-统计脚本')
- ->addArgument('day', Argument::OPTIONAL, '统计日期,格式:Y-m-d,默认为昨天');
- }
- protected function execute(Input $input, Output $output)
- {
- $service = new ComplexSummaryService();
-
- // 获取统计日期(默认昨天)
- $day = $input->getArgument('day') ?: date('Y-m-d', strtotime('-1 day'));
-
- $output->writeln(date('Y-m-d H:i:s') . " 开始统计日期: {$day}");
- $gameIds = $service->getOnlineGameIds();
- try {
- // 1. 获取新增玩家数据
- $regNumList = $service->getRegNum($day, $gameIds, 1);
- $output->writeln("新增玩家统计完成: " . count($regNumList) . " 条");
- // 2. 获取新增付费玩家(当天有充值的用户)
- $payUserNumList = $service->getNewUserPayNum($day, $gameIds, 1);
- $output->writeln("新增付费玩家统计完成: " . count($payUserNumList) . " 条");
- // 3. 获取活跃玩家(使用玩家活跃日志表)
- $activeUserList = $service->getActiveUserByActiveLog($day, $gameIds, 1, 0);
- $output->writeln("活跃玩家统计完成: " . count($activeUserList) . " 条");
-
- // 4. 订单金额+实付金额
- $payMoneyList = $service->getPayMoney($day, $gameIds, 1, 0);
- $output->writeln("订单金额+实付金额统计完成: " . count($payMoneyList) . " 条");
- // 5. 合并所有统计数据
- $allStats = $service->mergeStatsData([
- $regNumList,
- $payUserNumList,
- $activeUserList,
- $payMoneyList
- ]);
- // 6. 保存数据
- // 先清空当天日期的数据,避免重新抓取时数据错乱(在事务外执行,确保清空操作生效)
- Db::table('cy_complex_summary_complex')
- ->where('day', $day)
- ->delete();
- Db::startTrans();
- try {
- foreach ($allStats as $stat) {
- $gameId = $stat['game_id'] ?? 0;
- $complexId = $stat['channel_id'] ?? 0;
-
- // 准备保存的数据
- $saveData = [
- 'day' => $day,
- 'game_id' => $gameId,
- 'complex_id' => $complexId,
- 'reg_num' => intval($stat['reg_num'] ?? 0),
- 'pay_user_num' => intval($stat['reg_pay_num'] ?? 0),
- 'active_user_num' => intval($stat['active_user_num'] ?? 0),
- 'pay_total' => floatval($stat['pay_money'] ?? 0),
- 'pay_amount_total' => floatval($stat['pay_amount_total'] ?? 0),
- ];
-
- // 直接插入(已清空当天数据,无需检查是否存在)
- Db::table('cy_complex_summary_complex')->insert($saveData);
-
- // 以下代码已注释,防止以后需要时可以恢复
- // // 检查记录是否存在
- // $exists = Db::table('cy_complex_summary_complex')
- // ->where([
- // 'day' => $day,
- // 'game_id' => $gameId,
- // 'complex_id' => $complexId
- // ])
- // ->find();
- //
- // if ($exists) {
- // // 更新
- // Db::table('cy_complex_summary_complex')
- // ->where(['id' => $exists['id']])
- // ->update($saveData);
- // } else {
- // // 插入
- // Db::table('cy_complex_summary_complex')->insert($saveData);
- // }
- }
-
- Db::commit();
- $output->writeln("数据保存成功,共处理 " . count($allStats) . " 条记录");
-
- } catch (Exception $e) {
- Db::rollback();
- $output->writeln("保存数据失败: " . $e->getMessage());
- throw $e;
- }
-
- } catch (Exception $e) {
- $output->writeln("统计失败: " . $e->getMessage() . ' - ' . $e->getFile() . ":" . $e->getLine());
- throw $e;
- }
-
- $output->writeln(date('Y-m-d H:i:s') . " 统计完成");
- }
- }
|