| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace App\Module\Transfer\Commands;
- use App\Module\Transfer\Services\FeeStatisticsService;
- use Illuminate\Console\Command;
- use Carbon\Carbon;
- /**
- * 手续费统计命令
- *
- * 用于执行每日手续费统计任务
- */
- class FeeStatisticsCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'transfer:fee-statistics
- {--date= : 统计日期,格式:Y-m-d,默认为昨天}
- {--app-id=0 : 应用ID,0表示所有应用}
- {--rerun : 重新统计(会删除已有数据)}
- {--validate : 验证统计数据完整性}
- {--cleanup : 清理过期统计数据}
- {--retention-days=365 : 数据保留天数}';
- /**
- * 命令描述
- */
- protected $description = '执行Transfer模块手续费每日统计';
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->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', '应用名称', '订单数', '手续费金额', '最后处理订单ID', '状态'];
- $rows = [];
- foreach ($result['apps'] as $app) {
- $rows[] = [
- $app['app_id'],
- $app['app_name'],
- $app['total_orders'],
- $app['total_fee'],
- $app['last_processed_order_id'],
- $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']}");
- }
- }
|