| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace App\Module\Transfer\Services;
- use App\Module\Transfer\Models\TransferApp;
- use App\Module\Transfer\Models\TransferOrder;
- use App\Module\Transfer\Models\TransferFeeDailyStats;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Enums\TransferType;
- use App\Module\Transfer\Logics\FeeStatisticsLogic;
- use Illuminate\Support\Facades\Log;
- use Carbon\Carbon;
- /**
- * 手续费统计服务类
- *
- * 负责处理Transfer模块的手续费统计功能
- */
- class FeeStatisticsService
- {
- /**
- * 执行每日手续费统计
- *
- * @param string|null $date 统计日期,默认为昨天
- * @return array 统计结果
- */
- public static function runDailyStatistics(?string $date = null): array
- {
- try {
- $date = $date ?: Carbon::yesterday()->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' => []
- ];
- }
- }
- }
|