MexMatchCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 MexMatchCommand extends Command
  12. {
  13. /**
  14. * 命令签名
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'mex: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::executeMatch($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('MexMatchCommand执行失败', [
  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::getMatchStats();
  79. $this->table([
  80. '待撮合订单数',
  81. '涉及商品数',
  82. '待撮合数量',
  83. '待撮合金额',
  84. '有库存商品数'
  85. ], [[
  86. $stats['pending_orders'],
  87. $stats['pending_items'],
  88. $stats['pending_quantity'],
  89. $stats['pending_amount'],
  90. $stats['available_items']
  91. ]]);
  92. if ($itemId) {
  93. $this->info("指定处理商品ID: {$itemId}");
  94. $conditionCheck = MexMatchService::checkMatchConditions($itemId);
  95. if ($conditionCheck['can_match']) {
  96. $this->info("✓ 商品 {$itemId} 满足撮合条件");
  97. $this->info(" - 待撮合订单: {$conditionCheck['pending_orders']} 个");
  98. } else {
  99. $this->warn("✗ 商品 {$itemId} 不满足撮合条件: {$conditionCheck['message']}");
  100. }
  101. } else {
  102. $this->info('将处理所有符合条件的商品');
  103. }
  104. $this->info("批处理大小: {$batchSize}");
  105. $this->info('试运行完成,未执行实际撮合操作');
  106. return Command::SUCCESS;
  107. }
  108. /**
  109. * 显示撮合前统计
  110. */
  111. private function showPreMatchStats(): void
  112. {
  113. $this->info('=== 撮合前统计 ===');
  114. $stats = MexMatchService::getMatchStats();
  115. $this->table([
  116. '待撮合订单数',
  117. '涉及商品数',
  118. '待撮合数量',
  119. '待撮合金额',
  120. '有库存商品数'
  121. ], [[
  122. $stats['pending_orders'],
  123. $stats['pending_items'],
  124. $stats['pending_quantity'],
  125. $stats['pending_amount'],
  126. $stats['available_items']
  127. ]]);
  128. }
  129. /**
  130. * 显示撮合结果
  131. *
  132. * @param array $result
  133. */
  134. private function showMatchResult(array $result): void
  135. {
  136. $this->info('=== 撮合结果 ===');
  137. if ($result['success']) {
  138. $this->info("✓ 撮合成功");
  139. $this->info(" - 处理商品: " . count($result['processed_items']) . " 个");
  140. $this->info(" - 撮合订单: {$result['total_matched']} 个");
  141. $this->info(" - 成交金额: {$result['total_amount']}");
  142. $this->info(" - 执行时间: {$result['execution_time_ms']}ms");
  143. if (!empty($result['processed_items'])) {
  144. $this->info(" - 处理的商品ID: " . implode(', ', $result['processed_items']));
  145. }
  146. if (!empty($result['errors'])) {
  147. $this->warn('部分商品处理出现错误:');
  148. foreach ($result['errors'] as $error) {
  149. $this->warn(" - {$error}");
  150. }
  151. }
  152. } else {
  153. $this->error("✗ 撮合失败: {$result['message']}");
  154. }
  155. }
  156. /**
  157. * 显示撮合后统计
  158. */
  159. private function showPostMatchStats(): void
  160. {
  161. $this->info('=== 撮合后统计 ===');
  162. $stats = MexMatchService::getMatchStats();
  163. $this->table([
  164. '剩余待撮合订单',
  165. '今日撮合订单',
  166. '今日成交数量',
  167. '今日成交金额'
  168. ], [[
  169. $stats['pending_orders'],
  170. $stats['today_matched'],
  171. $stats['today_quantity'],
  172. $stats['today_amount']
  173. ]]);
  174. }
  175. }