MexUserSellItemMatchCommand.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Module\Mex\Commands;
  3. use App\Module\Mex\Services\MexMatchService;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 用户卖出物品撮合计划任务
  8. *
  9. * 定时执行用户卖出物品撮合任务,处理待撮合的用户卖出物品订单
  10. */
  11. class MexUserSellItemMatchCommand extends Command
  12. {
  13. /**
  14. * 命令签名
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'mex:user-sell-item-match
  19. {--item= : 指定商品ID,不指定则处理所有商品}
  20. {--batch=100 : 批处理大小,默认100}
  21. {--dry-run : 试运行模式,不执行实际撮合}';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = '执行用户卖出物品撮合任务';
  28. /**
  29. * 执行命令
  30. *
  31. * @return int
  32. */
  33. public function handle(): int
  34. {
  35. $startTime = microtime(true);
  36. $this->info('开始执行用户卖出物品撮合任务...');
  37. // 获取命令参数
  38. $itemId = $this->option('item') ? (int)$this->option('item') : null;
  39. $batchSize = (int)$this->option('batch');
  40. $dryRun = $this->option('dry-run');
  41. if ($dryRun) {
  42. $this->warn('试运行模式:不会执行实际的撮合操作');
  43. return $this->handleDryRun($itemId, $batchSize);
  44. }
  45. try {
  46. // 显示撮合前统计
  47. $this->showPreMatchStats();
  48. // 执行用户卖出物品撮合
  49. $result = MexMatchService::executeUserSellItemMatch($itemId, $batchSize);
  50. // 显示撮合结果
  51. $this->showMatchResult($result);
  52. // 显示撮合后统计
  53. $this->showPostMatchStats();
  54. $endTime = microtime(true);
  55. $totalTime = round(($endTime - $startTime) * 1000, 2);
  56. $this->info("用户卖出物品撮合任务执行完成,总耗时: {$totalTime}ms");
  57. return $result['success'] ? Command::SUCCESS : Command::FAILURE;
  58. } catch (\Exception $e) {
  59. $this->error('用户卖出物品撮合任务执行失败: ' . $e->getMessage());
  60. Log::error('MexUserSellItemMatchCommand执行失败', [
  61. 'error' => $e->getMessage(),
  62. 'trace' => $e->getTraceAsString(),
  63. ]);
  64. return Command::FAILURE;
  65. }
  66. }
  67. /**
  68. * 处理试运行模式
  69. *
  70. * @param int|null $itemId
  71. * @param int $batchSize
  72. * @return int
  73. */
  74. private function handleDryRun(?int $itemId, int $batchSize): int
  75. {
  76. $this->info('=== 用户卖出物品撮合试运行模式 ===');
  77. // 显示当前统计
  78. $stats = MexMatchService::getUserSellItemMatchStats();
  79. $this->table([
  80. '待撮合用户卖出物品订单数',
  81. '涉及商品数',
  82. '待撮合数量',
  83. '待撮合金额'
  84. ], [[
  85. $stats['pending_orders'],
  86. $stats['pending_items'],
  87. $stats['pending_quantity'],
  88. $stats['pending_amount']
  89. ]]);
  90. if ($itemId) {
  91. $this->info("指定处理商品ID: {$itemId}");
  92. $conditionCheck = MexMatchService::checkUserSellItemMatchConditions($itemId);
  93. if ($conditionCheck['can_match']) {
  94. $this->info("✓ 商品 {$itemId} 满足用户卖出物品撮合条件");
  95. $this->info(" - 待撮合订单: {$conditionCheck['pending_orders']} 个");
  96. } else {
  97. $this->warn("✗ 商品 {$itemId} 不满足用户卖出物品撮合条件: {$conditionCheck['message']}");
  98. }
  99. } else {
  100. $this->info('将处理所有符合条件的商品');
  101. }
  102. $this->info("批处理大小: {$batchSize}");
  103. $this->info('试运行完成,未执行实际撮合操作');
  104. return Command::SUCCESS;
  105. }
  106. /**
  107. * 显示撮合前统计
  108. */
  109. private function showPreMatchStats(): void
  110. {
  111. $this->info('=== 用户卖出物品撮合前统计 ===');
  112. $stats = MexMatchService::getUserSellItemMatchStats();
  113. $this->table([
  114. '待撮合用户卖出物品订单数',
  115. '涉及商品数',
  116. '待撮合数量',
  117. '待撮合金额'
  118. ], [[
  119. $stats['pending_orders'],
  120. $stats['pending_items'],
  121. $stats['pending_quantity'],
  122. $stats['pending_amount']
  123. ]]);
  124. }
  125. /**
  126. * 显示撮合结果
  127. *
  128. * @param array $result
  129. */
  130. private function showMatchResult(array $result): void
  131. {
  132. $this->info('=== 用户卖出物品撮合结果 ===');
  133. if ($result['success']) {
  134. $this->info("✓ 撮合成功");
  135. $this->info(" - 处理商品: " . count($result['processed_items']) . " 个");
  136. $this->info(" - 撮合订单: {$result['total_matched']} 个");
  137. $this->info(" - 成交金额: {$result['total_amount']}");
  138. $this->info(" - 执行时间: {$result['execution_time_ms']}ms");
  139. if (!empty($result['processed_items'])) {
  140. $this->info(" - 处理的商品ID: " . implode(', ', $result['processed_items']));
  141. }
  142. if (!empty($result['errors'])) {
  143. $this->warn('部分商品处理出现错误:');
  144. foreach ($result['errors'] as $error) {
  145. $this->warn(" - {$error}");
  146. }
  147. }
  148. } else {
  149. $this->error("✗ 撮合失败: {$result['message']}");
  150. }
  151. }
  152. /**
  153. * 显示撮合后统计
  154. */
  155. private function showPostMatchStats(): void
  156. {
  157. $this->info('=== 用户卖出物品撮合后统计 ===');
  158. $stats = MexMatchService::getUserSellItemMatchStats();
  159. $this->table([
  160. '剩余待撮合订单',
  161. '今日撮合订单',
  162. '今日成交数量',
  163. '今日成交金额'
  164. ], [[
  165. $stats['pending_orders'],
  166. $stats['today_matched'],
  167. $stats['today_quantity'],
  168. $stats['today_amount']
  169. ]]);
  170. }
  171. }