FeeStatisticsCommand.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace App\Module\Transfer\Commands;
  3. use App\Module\Transfer\Services\FeeStatisticsService;
  4. use Illuminate\Console\Command;
  5. use Carbon\Carbon;
  6. /**
  7. * 手续费统计命令
  8. *
  9. * 用于执行每日手续费统计任务
  10. */
  11. class FeeStatisticsCommand extends Command
  12. {
  13. /**
  14. * 命令签名
  15. */
  16. protected $signature = 'transfer:fee-statistics
  17. {--date= : 统计日期,格式:Y-m-d,默认为昨天}
  18. {--app-id=0 : 应用ID,0表示所有应用}
  19. {--rerun : 重新统计(会删除已有数据)}
  20. {--validate : 验证统计数据完整性}
  21. {--cleanup : 清理过期统计数据}
  22. {--retention-days=365 : 数据保留天数}';
  23. /**
  24. * 命令描述
  25. */
  26. protected $description = '执行Transfer模块手续费每日统计';
  27. /**
  28. * 执行命令
  29. */
  30. public function handle()
  31. {
  32. $this->info('开始执行手续费统计任务...');
  33. $startTime = microtime(true);
  34. try {
  35. // 清理过期数据
  36. if ($this->option('cleanup')) {
  37. $this->handleCleanup();
  38. return 0;
  39. }
  40. // 验证数据完整性
  41. if ($this->option('validate')) {
  42. $this->handleValidation();
  43. return 0;
  44. }
  45. // 执行统计
  46. $this->handleStatistics();
  47. $endTime = microtime(true);
  48. $duration = round($endTime - $startTime, 2);
  49. $this->info("手续费统计任务完成,耗时:{$duration}秒");
  50. return 0;
  51. } catch (\Exception $e) {
  52. $this->error("手续费统计任务失败:" . $e->getMessage());
  53. $this->error("错误详情:" . $e->getTraceAsString());
  54. return 1;
  55. }
  56. }
  57. /**
  58. * 处理统计任务
  59. */
  60. protected function handleStatistics()
  61. {
  62. $date = $this->option('date') ?: Carbon::yesterday()->format('Y-m-d');
  63. $appId = (int) $this->option('app-id');
  64. $rerun = $this->option('rerun');
  65. $this->info("统计日期:{$date}");
  66. $this->info("应用ID:" . ($appId > 0 ? $appId : '所有应用'));
  67. if ($rerun) {
  68. $this->warn('重新统计模式:将删除已有统计数据');
  69. if (!$this->confirm('确认要重新统计吗?')) {
  70. $this->info('已取消操作');
  71. return;
  72. }
  73. $result = FeeStatisticsService::restatistics($date, $appId);
  74. $this->displayRestatisticsResult($result);
  75. } else {
  76. $result = FeeStatisticsService::runDailyStatistics($date);
  77. $this->displayStatisticsResult($result);
  78. }
  79. }
  80. /**
  81. * 处理数据验证
  82. */
  83. protected function handleValidation()
  84. {
  85. $date = $this->option('date') ?: Carbon::yesterday()->format('Y-m-d');
  86. $this->info("验证统计数据完整性,日期:{$date}");
  87. $result = FeeStatisticsService::validateStatistics($date);
  88. if ($result['valid']) {
  89. $this->info('✅ 统计数据验证通过');
  90. } else {
  91. $this->error('❌ 统计数据验证失败');
  92. }
  93. // 显示详细结果
  94. $headers = ['应用ID', '应用名称', '状态', '记录订单数', '实际订单数', '记录手续费', '实际手续费', '说明'];
  95. $rows = [];
  96. foreach ($result['details'] as $detail) {
  97. $rows[] = [
  98. $detail['app_id'],
  99. $detail['app_name'],
  100. $detail['valid'] ? '✅ 正常' : '❌ 异常',
  101. $detail['recorded_orders'] ?? '-',
  102. $detail['actual_orders'] ?? '-',
  103. $detail['recorded_fee'] ?? '-',
  104. $detail['actual_fee'] ?? '-',
  105. $detail['message']
  106. ];
  107. }
  108. $this->table($headers, $rows);
  109. }
  110. /**
  111. * 处理数据清理
  112. */
  113. protected function handleCleanup()
  114. {
  115. $retentionDays = (int) $this->option('retention-days');
  116. $this->info("清理过期统计数据,保留天数:{$retentionDays}");
  117. if (!$this->confirm('确认要清理过期数据吗?')) {
  118. $this->info('已取消操作');
  119. return;
  120. }
  121. $deletedCount = FeeStatisticsService::cleanupExpiredStats($retentionDays);
  122. $this->info("清理完成,删除了 {$deletedCount} 条过期记录");
  123. }
  124. /**
  125. * 显示统计结果
  126. */
  127. protected function displayStatisticsResult(array $result)
  128. {
  129. $this->info("统计完成!");
  130. $this->info("统计日期:{$result['date']}");
  131. $this->info("处理应用数:{$result['summary']['processed_apps']}");
  132. $this->info("总订单数:{$result['summary']['total_orders']}");
  133. $this->info("总手续费:{$result['summary']['total_fee']}");
  134. // 显示各应用统计详情
  135. if (!empty($result['apps'])) {
  136. $headers = ['应用ID', '应用名称', '订单数', '手续费金额', '状态'];
  137. $rows = [];
  138. foreach ($result['apps'] as $app) {
  139. $rows[] = [
  140. $app['app_id'],
  141. $app['app_name'],
  142. $app['total_orders'],
  143. $app['total_fee'],
  144. $app['message']
  145. ];
  146. }
  147. $this->table($headers, $rows);
  148. }
  149. }
  150. /**
  151. * 显示重新统计结果
  152. */
  153. protected function displayRestatisticsResult(array $result)
  154. {
  155. $this->info("重新统计完成!");
  156. if (isset($result['app_id'])) {
  157. // 单个应用重新统计
  158. $this->info("统计日期:{$result['date']}");
  159. $this->info("应用ID:{$result['app_id']}");
  160. $this->info("结果:{$result['result']['message']}");
  161. $this->info("订单数:{$result['result']['total_orders']}");
  162. $this->info("手续费:{$result['result']['total_fee']}");
  163. } else {
  164. // 所有应用重新统计
  165. $this->displayStatisticsResult($result);
  166. }
  167. }
  168. /**
  169. * 显示配置信息
  170. */
  171. protected function showConfig()
  172. {
  173. $config = FeeStatisticsService::getStatisticsConfig();
  174. $this->info('手续费统计配置信息:');
  175. $this->info("执行时间:{$config['schedule_time']}");
  176. $this->info("时区:{$config['timezone']}");
  177. $this->info("数据保留天数:{$config['retention_days']}");
  178. $this->info("批量处理大小:{$config['batch_size']}");
  179. $this->info("启用应用数:{$config['enabled_apps']}");
  180. $this->info("总应用数:{$config['total_apps']}");
  181. }
  182. }