info('开始检查Mex模块数据一致性...'); // 查找已完成但没有成交记录的订单 $missingTransactionOrders = $this->findOrdersWithMissingTransactions(); if ($missingTransactionOrders->isEmpty()) { $this->info('✅ 没有发现数据不一致问题'); return 0; } $this->warn("发现 {$missingTransactionOrders->count()} 个已完成订单缺失成交记录:"); // 显示问题订单 $this->displayProblematicOrders($missingTransactionOrders); if ($this->option('dry-run')) { $this->info('🔍 仅预览模式,未执行修复操作'); return 0; } // 确认是否执行修复 if (!$this->confirm('是否要修复这些数据不一致问题?')) { $this->info('❌ 用户取消修复操作'); return 0; } // 执行修复 $this->fixMissingTransactions($missingTransactionOrders); $this->info('✅ 修复完成'); return 0; } /** * 查找已完成但没有成交记录的订单 * * @return \Illuminate\Database\Eloquent\Collection */ private function findOrdersWithMissingTransactions() { return MexOrder::where('status', OrderStatus::COMPLETED) ->whereDoesntHave('buyTransactions') ->whereDoesntHave('sellTransactions') ->orderBy('completed_at') ->get(); } /** * 显示有问题的订单 * * @param \Illuminate\Database\Eloquent\Collection $orders */ private function displayProblematicOrders($orders) { $headers = ['订单ID', '用户ID', '商品ID', '订单类型', '数量', '价格', '总金额', '完成时间']; $rows = []; foreach ($orders as $order) { $rows[] = [ $order->id, $order->user_id, $order->item_id, $order->order_type->value, $order->quantity, $order->price, $order->total_amount, $order->completed_at ? $order->completed_at->format('Y-m-d H:i:s') : '未知', ]; } $this->table($headers, $rows); } /** * 修复缺失的成交记录 * * @param \Illuminate\Database\Eloquent\Collection $orders */ private function fixMissingTransactions($orders) { $fixedCount = 0; $errorCount = 0; foreach ($orders as $order) { try { DB::transaction(function () use ($order) { $this->createMissingTransaction($order); }); $fixedCount++; $this->info("✅ 订单 {$order->id} 的成交记录已修复"); } catch (\Exception $e) { $errorCount++; $this->error("❌ 订单 {$order->id} 修复失败: " . $e->getMessage()); } } $this->info("修复统计: 成功 {$fixedCount} 个,失败 {$errorCount} 个"); } /** * 为订单创建缺失的成交记录 * * @param MexOrder $order */ private function createMissingTransaction(MexOrder $order) { // 根据订单类型确定买方和卖方 if ($order->order_type->value === 'BUY') { $buyOrderId = $order->id; $sellOrderId = null; $buyerId = $order->user_id; $sellerId = self::WAREHOUSE_USER_ID; $transactionType = TransactionType::USER_BUY; } else { $buyOrderId = null; $sellOrderId = $order->id; $buyerId = self::WAREHOUSE_USER_ID; $sellerId = $order->user_id; $transactionType = TransactionType::USER_SELL; } // 创建成交记录 $transaction = MexTransaction::create([ 'buy_order_id' => $buyOrderId, 'sell_order_id' => $sellOrderId, 'buyer_id' => $buyerId, 'seller_id' => $sellerId, 'item_id' => $order->item_id, 'currency_type' => $order->currency_type->value, 'quantity' => $order->completed_quantity ?: $order->quantity, 'price' => $order->price, 'total_amount' => $order->completed_amount ?: $order->total_amount, 'transaction_type' => $transactionType, 'is_admin_operation' => false, 'created_at' => $order->completed_at ?: $order->updated_at, ]); if (!$transaction || !$transaction->id) { throw new \Exception('成交记录创建失败'); } } }