GenerateDailyPriceTrendsCommand.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Module\Mex\Commands;
  3. use App\Module\Mex\Service\MexDailyPriceTrendService;
  4. use Carbon\Carbon;
  5. use Illuminate\Console\Command;
  6. /**
  7. * 生成每日价格趋势数据命令
  8. */
  9. class GenerateDailyPriceTrendsCommand extends Command
  10. {
  11. /**
  12. * 命令签名
  13. */
  14. protected $signature = 'mex:generate-daily-trends
  15. {--date= : 指定日期 (Y-m-d 格式,默认为昨天)}
  16. {--start-date= : 开始日期 (Y-m-d 格式)}
  17. {--end-date= : 结束日期 (Y-m-d 格式)}
  18. {--item-id= : 指定商品ID}';
  19. /**
  20. * 命令描述
  21. */
  22. protected $description = '生成农贸市场每日价格趋势数据';
  23. /**
  24. * 执行命令
  25. */
  26. public function handle(): int
  27. {
  28. $this->info('开始生成每日价格趋势数据...');
  29. try {
  30. $date = $this->option('date');
  31. $startDate = $this->option('start-date');
  32. $endDate = $this->option('end-date');
  33. $itemId = $this->option('item-id');
  34. if ($startDate && $endDate) {
  35. // 批量生成指定日期范围的趋势数据
  36. $this->generateDateRangeTrends($startDate, $endDate, $itemId);
  37. } elseif ($date) {
  38. // 生成指定日期的趋势数据
  39. $this->generateSingleDateTrends($date, $itemId);
  40. } else {
  41. // 默认生成昨天的趋势数据
  42. $yesterday = Carbon::yesterday()->format('Y-m-d');
  43. $this->generateSingleDateTrends($yesterday, $itemId);
  44. }
  45. $this->info('每日价格趋势数据生成完成!');
  46. return 0;
  47. } catch (\Exception $e) {
  48. $this->error('生成每日价格趋势数据失败: ' . $e->getMessage());
  49. $this->error('错误详情: ' . $e->getTraceAsString());
  50. return 1;
  51. }
  52. }
  53. /**
  54. * 生成单个日期的趋势数据
  55. */
  56. private function generateSingleDateTrends(string $date, ?int $itemId = null): void
  57. {
  58. $this->info("正在生成 {$date} 的价格趋势数据...");
  59. if ($itemId) {
  60. $this->info("指定商品ID: {$itemId}");
  61. }
  62. $trends = MexDailyPriceTrendService::generateMultipleDaysTrends(
  63. $date,
  64. $date,
  65. $itemId
  66. );
  67. $count = $trends->count();
  68. $this->info("成功生成 {$count} 条价格趋势记录");
  69. if ($count > 0) {
  70. $this->table(
  71. ['商品ID', '开盘价', '收盘价', '最高价', '最低价', '成交量', '成交额'],
  72. $trends->map(function ($trend) {
  73. return [
  74. $trend->itemId,
  75. number_format($trend->openPrice, 5),
  76. number_format($trend->closePrice, 5),
  77. number_format($trend->highPrice, 5),
  78. number_format($trend->lowPrice, 5),
  79. $trend->totalVolume,
  80. number_format($trend->totalAmount, 5),
  81. ];
  82. })->toArray()
  83. );
  84. }
  85. }
  86. /**
  87. * 生成日期范围的趋势数据
  88. */
  89. private function generateDateRangeTrends(string $startDate, string $endDate, ?int $itemId = null): void
  90. {
  91. $this->info("正在生成 {$startDate} 到 {$endDate} 的价格趋势数据...");
  92. if ($itemId) {
  93. $this->info("指定商品ID: {$itemId}");
  94. }
  95. $start = Carbon::parse($startDate);
  96. $end = Carbon::parse($endDate);
  97. $totalDays = $start->diffInDays($end) + 1;
  98. $this->info("总共需要处理 {$totalDays} 天的数据");
  99. $progressBar = $this->output->createProgressBar($totalDays);
  100. $progressBar->start();
  101. $totalTrends = 0;
  102. $current = $start->copy();
  103. while ($current <= $end) {
  104. $dateStr = $current->format('Y-m-d');
  105. $trends = MexDailyPriceTrendService::generateMultipleDaysTrends(
  106. $dateStr,
  107. $dateStr,
  108. $itemId
  109. );
  110. $totalTrends += $trends->count();
  111. $progressBar->advance();
  112. $current->addDay();
  113. }
  114. $progressBar->finish();
  115. $this->newLine();
  116. $this->info("成功生成 {$totalTrends} 条价格趋势记录");
  117. }
  118. }