format('Y-m-d'); Log::info("开始执行手续费每日统计", ['date' => $date]); $result = FeeStatisticsLogic::runDailyStatistics($date); Log::info("手续费每日统计完成", [ 'date' => $date, 'processed_apps' => count($result['apps']), 'total_orders' => $result['summary']['total_orders'], 'total_fee' => $result['summary']['total_fee'] ]); return $result; } catch (\Exception $e) { Log::error("手续费每日统计失败", [ 'date' => $date ?? 'unknown', 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); throw $e; } } /** * 获取指定日期范围的统计数据 * * @param string $startDate 开始日期 * @param string $endDate 结束日期 * @param int $appId 应用ID(0表示所有应用) * @return array */ public static function getStatsByDateRange(string $startDate, string $endDate,$appId): array { try { return FeeStatisticsLogic::getStatsByDateRange($startDate, $endDate, $appId); } catch (\Exception $e) { Log::error("获取手续费统计数据失败", [ 'start_date' => $startDate, 'end_date' => $endDate, 'app_id' => $appId, 'error' => $e->getMessage() ]); return [ 'error' => $e->getMessage(), 'data' => [], 'summary' => [ 'total_days' => 0, 'total_orders' => 0, 'total_amount' => '0.0000000000', 'total_fee' => '0.0000000000', 'avg_fee_rate' => '0.00000' ] ]; } } /** * 获取月度统计数据 * * @param int $year 年份 * @param int $month 月份 * @param int $appId 应用ID(0表示所有应用) * @return array */ public static function getMonthlyStats(int $year, int $month, int $appId = 0): array { try { return FeeStatisticsLogic::getMonthlyStats($year, $month, $appId); } catch (\Exception $e) { Log::error("获取月度手续费统计失败", [ 'year' => $year, 'month' => $month, 'app_id' => $appId, 'error' => $e->getMessage() ]); return [ 'error' => $e->getMessage(), 'year' => $year, 'month' => $month, 'stat_days' => 0, 'total_orders' => 0, 'total_amount' => '0.0000000000', 'total_fee' => '0.0000000000', 'avg_fee_rate' => '0.00000' ]; } } /** * 获取应用汇总统计 * * @param int $appId 应用ID(0表示所有应用) * @return array */ public static function getAppSummary(int $appId = 0): array { try { return FeeStatisticsLogic::getAppSummary($appId); } catch (\Exception $e) { Log::error("获取应用汇总统计失败", [ 'app_id' => $appId, 'error' => $e->getMessage() ]); return [ 'error' => $e->getMessage(), 'data' => [] ]; } } /** * 获取最近N天的统计趋势 * * @param int $days 天数 * @param int $appId 应用ID(0表示所有应用) * @return array */ public static function getRecentTrend(int $days = 7, int $appId = 0): array { try { $endDate = Carbon::yesterday()->format('Y-m-d'); $startDate = Carbon::yesterday()->subDays($days - 1)->format('Y-m-d'); return self::getStatsByDateRange($startDate, $endDate, $appId); } catch (\Exception $e) { Log::error("获取手续费趋势数据失败", [ 'days' => $days, 'app_id' => $appId, 'error' => $e->getMessage() ]); return [ 'error' => $e->getMessage(), 'data' => [], 'summary' => [] ]; } } /** * 获取手续费收入排行榜 * * @param string $startDate 开始日期 * @param string $endDate 结束日期 * @param int $limit 限制数量 * @return array */ public static function getFeeRanking(string $startDate, string $endDate, int $limit = 10): array { try { return FeeStatisticsLogic::getFeeRanking($startDate, $endDate, $limit); } catch (\Exception $e) { Log::error("获取手续费排行榜失败", [ 'start_date' => $startDate, 'end_date' => $endDate, 'limit' => $limit, 'error' => $e->getMessage() ]); return [ 'error' => $e->getMessage(), 'data' => [] ]; } } /** * 重新统计指定日期的数据 * * @param string $date 统计日期 * @param int $appId 应用ID(0表示所有应用) * @return array */ public static function restatistics(string $date, int $appId = 0): array { try { Log::info("开始重新统计手续费数据", ['date' => $date, 'app_id' => $appId]); $result = FeeStatisticsLogic::restatistics($date, $appId); Log::info("重新统计手续费数据完成", [ 'date' => $date, 'app_id' => $appId, 'result' => $result ]); return $result; } catch (\Exception $e) { Log::error("重新统计手续费数据失败", [ 'date' => $date, 'app_id' => $appId, 'error' => $e->getMessage() ]); throw $e; } } /** * 获取统计配置信息 * * @return array */ public static function getStatisticsConfig(): array { return [ 'schedule_time' => '22:00', 'timezone' => config('app.timezone', 'Asia/Shanghai'), 'retention_days' => 365, // 保留365天的统计数据 'batch_size' => 1000, // 批量处理大小 'enabled_apps' => TransferApp::where('is_enabled', true)->count(), 'total_apps' => TransferApp::count(), ]; } /** * 清理过期的统计数据 * * @param int $retentionDays 保留天数 * @return int 清理的记录数 */ public static function cleanupExpiredStats(int $retentionDays = 365): int { try { $cutoffDate = Carbon::now()->subDays($retentionDays)->format('Y-m-d'); $deletedCount = TransferFeeDailyStats::where('stat_date', '<', $cutoffDate)->delete(); Log::info("清理过期手续费统计数据", [ 'cutoff_date' => $cutoffDate, 'deleted_count' => $deletedCount ]); return $deletedCount; } catch (\Exception $e) { Log::error("清理过期手续费统计数据失败", [ 'retention_days' => $retentionDays, 'error' => $e->getMessage() ]); throw $e; } } /** * 验证统计数据的完整性 * * @param string $date 检查日期 * @return array */ public static function validateStatistics(string $date): array { try { return FeeStatisticsLogic::validateStatistics($date); } catch (\Exception $e) { Log::error("验证统计数据完整性失败", [ 'date' => $date, 'error' => $e->getMessage() ]); return [ 'valid' => false, 'error' => $e->getMessage(), 'details' => [] ]; } } }