info('开始执行手续费统计任务...'); $startTime = microtime(true); try { // 清理过期数据 if ($this->option('cleanup')) { $this->handleCleanup(); return 0; } // 验证数据完整性 if ($this->option('validate')) { $this->handleValidation(); return 0; } // 执行统计 $this->handleStatistics(); $endTime = microtime(true); $duration = round($endTime - $startTime, 2); $this->info("手续费统计任务完成,耗时:{$duration}秒"); return 0; } catch (\Exception $e) { $this->error("手续费统计任务失败:" . $e->getMessage()); $this->error("错误详情:" . $e->getTraceAsString()); return 1; } } /** * 处理统计任务 */ protected function handleStatistics() { $date = $this->option('date') ?: Carbon::yesterday()->format('Y-m-d'); $appId = (int) $this->option('app-id'); $rerun = $this->option('rerun'); $this->info("统计日期:{$date}"); $this->info("应用ID:" . ($appId > 0 ? $appId : '所有应用')); if ($rerun) { $this->warn('重新统计模式:将删除已有统计数据'); if (!$this->confirm('确认要重新统计吗?')) { $this->info('已取消操作'); return; } $result = FeeStatisticsService::restatistics($date, $appId); $this->displayRestatisticsResult($result); } else { $result = FeeStatisticsService::runDailyStatistics($date); $this->displayStatisticsResult($result); } } /** * 处理数据验证 */ protected function handleValidation() { $date = $this->option('date') ?: Carbon::yesterday()->format('Y-m-d'); $this->info("验证统计数据完整性,日期:{$date}"); $result = FeeStatisticsService::validateStatistics($date); if ($result['valid']) { $this->info('✅ 统计数据验证通过'); } else { $this->error('❌ 统计数据验证失败'); } // 显示详细结果 $headers = ['应用ID', '应用名称', '状态', '记录订单数', '实际订单数', '记录手续费', '实际手续费', '说明']; $rows = []; foreach ($result['details'] as $detail) { $rows[] = [ $detail['app_id'], $detail['app_name'], $detail['valid'] ? '✅ 正常' : '❌ 异常', $detail['recorded_orders'] ?? '-', $detail['actual_orders'] ?? '-', $detail['recorded_fee'] ?? '-', $detail['actual_fee'] ?? '-', $detail['message'] ]; } $this->table($headers, $rows); } /** * 处理数据清理 */ protected function handleCleanup() { $retentionDays = (int) $this->option('retention-days'); $this->info("清理过期统计数据,保留天数:{$retentionDays}"); if (!$this->confirm('确认要清理过期数据吗?')) { $this->info('已取消操作'); return; } $deletedCount = FeeStatisticsService::cleanupExpiredStats($retentionDays); $this->info("清理完成,删除了 {$deletedCount} 条过期记录"); } /** * 显示统计结果 */ protected function displayStatisticsResult(array $result) { $this->info("统计完成!"); $this->info("统计日期:{$result['date']}"); $this->info("处理应用数:{$result['summary']['processed_apps']}"); $this->info("总订单数:{$result['summary']['total_orders']}"); $this->info("总手续费:{$result['summary']['total_fee']}"); // 显示各应用统计详情 if (!empty($result['apps'])) { $headers = ['应用ID', '应用名称', '订单数', '手续费金额', '状态']; $rows = []; foreach ($result['apps'] as $app) { $rows[] = [ $app['app_id'], $app['app_name'], $app['total_orders'], $app['total_fee'], $app['message'] ]; } $this->table($headers, $rows); } } /** * 显示重新统计结果 */ protected function displayRestatisticsResult(array $result) { $this->info("重新统计完成!"); if (isset($result['app_id'])) { // 单个应用重新统计 $this->info("统计日期:{$result['date']}"); $this->info("应用ID:{$result['app_id']}"); $this->info("结果:{$result['result']['message']}"); $this->info("订单数:{$result['result']['total_orders']}"); $this->info("手续费:{$result['result']['total_fee']}"); } else { // 所有应用重新统计 $this->displayStatisticsResult($result); } } /** * 显示配置信息 */ protected function showConfig() { $config = FeeStatisticsService::getStatisticsConfig(); $this->info('手续费统计配置信息:'); $this->info("执行时间:{$config['schedule_time']}"); $this->info("时区:{$config['timezone']}"); $this->info("数据保留天数:{$config['retention_days']}"); $this->info("批量处理大小:{$config['batch_size']}"); $this->info("启用应用数:{$config['enabled_apps']}"); $this->info("总应用数:{$config['total_apps']}"); } }