info('开始执行用户买入物品撮合任务...'); // 获取命令参数 $itemId = $this->option('item') ? (int)$this->option('item') : null; $batchSize = (int)$this->option('batch'); $dryRun = $this->option('dry-run'); if ($dryRun) { $this->warn('试运行模式:不会执行实际的撮合操作'); return $this->handleDryRun($itemId, $batchSize); } try { // 显示撮合前统计 $this->showPreMatchStats(); // 执行用户买入物品撮合 $result = MexMatchService::executeUserBuyItemMatch($itemId, $batchSize); // 显示撮合结果 $this->showMatchResult($result); // 显示撮合后统计 $this->showPostMatchStats(); $endTime = microtime(true); $totalTime = round(($endTime - $startTime) * 1000, 2); $this->info("用户买入物品撮合任务执行完成,总耗时: {$totalTime}ms"); return $result['success'] ? true : false; } catch (\Exception $e) { $this->error('用户买入物品撮合任务执行失败: ' . $e->getMessage()); Log::error('MexUserBuyItemMatchCommand执行失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); return false; } } /** * 处理试运行模式 * * @param int|null $itemId * @param int $batchSize * @return int */ private function handleDryRun(?int $itemId, int $batchSize): int { $this->info('=== 用户买入物品撮合试运行模式 ==='); // 显示当前统计 $stats = MexMatchService::getUserBuyItemMatchStats(); $this->table([ '待撮合用户买入物品订单数', '涉及商品数', '待撮合数量', '待撮合金额', '有库存商品数' ], [[ $stats['pending_orders'], $stats['pending_items'], $stats['pending_quantity'], $stats['pending_amount'], $stats['available_items'] ]]); if ($itemId) { $this->info("指定处理商品ID: {$itemId}"); $conditionCheck = MexMatchService::checkUserBuyItemMatchConditions($itemId); if ($conditionCheck['can_match']) { $this->info("✓ 商品 {$itemId} 满足用户买入物品撮合条件"); $this->info(" - 待撮合订单: {$conditionCheck['pending_orders']} 个"); } else { $this->warn("✗ 商品 {$itemId} 不满足用户买入物品撮合条件: {$conditionCheck['message']}"); } } else { $this->info('将处理所有符合条件的商品'); } $this->info("批处理大小: {$batchSize}"); $this->info('试运行完成,未执行实际撮合操作'); return true; } /** * 显示撮合前统计 */ private function showPreMatchStats(): void { $this->info('=== 用户买入物品撮合前统计 ==='); $stats = MexMatchService::getUserBuyItemMatchStats(); $this->table([ '待撮合用户买入物品订单数', '涉及商品数', '待撮合数量', '待撮合金额', '有库存商品数' ], [[ $stats['pending_orders'], $stats['pending_items'], $stats['pending_quantity'], $stats['pending_amount'], $stats['available_items'] ]]); } /** * 显示撮合结果 * * @param array $result */ private function showMatchResult(array $result): void { $this->info('=== 用户买入物品撮合结果 ==='); if ($result['success']) { $this->info("✓ 撮合成功"); $this->info(" - 处理商品: " . count($result['processed_items']) . " 个"); $this->info(" - 撮合订单: {$result['total_matched']} 个"); $this->info(" - 成交金额: {$result['total_amount']}"); $this->info(" - 执行时间: {$result['execution_time_ms']}ms"); if (!empty($result['processed_items'])) { $this->info(" - 处理的商品ID: " . implode(', ', $result['processed_items'])); } if (!empty($result['errors'])) { $this->warn('部分商品处理出现错误:'); foreach ($result['errors'] as $error) { $this->warn(" - {$error}"); } } } else { $this->error("✗ 撮合失败: {$result['message']}"); } } /** * 显示撮合后统计 */ private function showPostMatchStats(): void { $this->info('=== 用户买入物品撮合后统计 ==='); $stats = MexMatchService::getUserBuyItemMatchStats(); $this->table([ '剩余待撮合订单', '今日撮合订单', '今日成交数量', '今日成交金额' ], [[ $stats['pending_orders'], $stats['today_matched'], $stats['today_quantity'], $stats['today_amount'] ]]); } }