| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace App\Module\Mex\Commands;
- use App\Module\Mex\Models\MexOrder;
- use App\Module\Mex\Models\MexTransaction;
- use App\Module\Mex\Enums\OrderStatus;
- use App\Module\Mex\Enums\TransactionType;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- /**
- * 修复缺失的成交记录命令
- *
- * 用于修复已完成的订单但没有对应成交记录的数据不一致问题
- */
- class FixMissingTransactionRecordsCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'mex:fix-missing-transactions {--dry-run : 仅显示需要修复的数据,不执行修复}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '修复Mex模块中已完成订单但缺失成交记录的数据不一致问题';
- /**
- * 仓库账户ID
- */
- const WAREHOUSE_USER_ID = 15;
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->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('成交记录创建失败');
- }
- }
- }
|