MexUserBuyItemMatchCommand.php 6.2 KB

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