| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Module\Mex\Commands;
- use App\Module\Mex\Service\MexConfigService;
- use App\Module\Mex\Service\MexDailyPriceTrendService;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- /**
- * 生成每日价格趋势数据命令
- */
- class GenerateDailyPriceTrendsCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'mex:generate-daily-trends
- {--date= : 指定日期 (Y-m-d 格式,默认为昨天)}
- {--start-date= : 开始日期 (Y-m-d 格式)}
- {--end-date= : 结束日期 (Y-m-d 格式)}
- {--item-id= : 指定商品ID}
- {--force : 强制执行,忽略配置检查}';
- /**
- * 命令描述
- */
- protected $description = '生成农贸市场每日价格趋势数据';
- /**
- * 执行命令
- */
- public function handle(): int
- {
- $this->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} 条价格趋势记录");
- }
- }
|