info('开始生成每日价格趋势数据...'); // 检查配置是否启用自动生成功能 if (!$this->option('force') && !MexConfigService::isAutoGenerateDailyTrendsEnabled()) { $this->warn('自动生成每日价格趋势功能已被禁用'); $this->info('如需强制执行,请使用 --force 选项'); return 0; } try { $date = $this->option('date'); $startDate = $this->option('start-date'); $endDate = $this->option('end-date'); $itemId = $this->option('item-id'); if ($startDate && $endDate) { // 批量生成指定日期范围的趋势数据 $this->generateDateRangeTrends($startDate, $endDate, $itemId); } elseif ($date) { // 生成指定日期的趋势数据 $this->generateSingleDateTrends($date, $itemId); } else { // 默认生成昨天的趋势数据 $yesterday = Carbon::yesterday()->format('Y-m-d'); $this->generateSingleDateTrends($yesterday, $itemId); } $this->info('每日价格趋势数据生成完成!'); return 0; } catch (\Exception $e) { $this->error('生成每日价格趋势数据失败: ' . $e->getMessage()); $this->error('错误详情: ' . $e->getTraceAsString()); return 1; } } /** * 生成单个日期的趋势数据 */ private function generateSingleDateTrends(string $date, ?int $itemId = null): void { $this->info("正在生成 {$date} 的价格趋势数据..."); if ($itemId) { $this->info("指定商品ID: {$itemId}"); } $trends = MexDailyPriceTrendService::generateMultipleDaysTrends( $date, $date, $itemId ); $count = $trends->count(); $this->info("成功生成 {$count} 条价格趋势记录"); if ($count > 0) { $this->table( ['商品ID', '开盘价', '收盘价', '最高价', '最低价', '成交量', '成交额'], $trends->map(function ($trend) { return [ $trend->itemId, number_format($trend->openPrice, 5), number_format($trend->closePrice, 5), number_format($trend->highPrice, 5), number_format($trend->lowPrice, 5), $trend->totalVolume, number_format($trend->totalAmount, 5), ]; })->toArray() ); } } /** * 生成日期范围的趋势数据 */ private function generateDateRangeTrends(string $startDate, string $endDate, ?int $itemId = null): void { $this->info("正在生成 {$startDate} 到 {$endDate} 的价格趋势数据..."); if ($itemId) { $this->info("指定商品ID: {$itemId}"); } $start = Carbon::parse($startDate); $end = Carbon::parse($endDate); $totalDays = $start->diffInDays($end) + 1; $this->info("总共需要处理 {$totalDays} 天的数据"); $progressBar = $this->output->createProgressBar($totalDays); $progressBar->start(); $totalTrends = 0; $current = $start->copy(); while ($current <= $end) { $dateStr = $current->format('Y-m-d'); $trends = MexDailyPriceTrendService::generateMultipleDaysTrends( $dateStr, $dateStr, $itemId ); $totalTrends += $trends->count(); $progressBar->advance(); $current->addDay(); } $progressBar->finish(); $this->newLine(); $this->info("成功生成 {$totalTrends} 条价格趋势记录"); } }