| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace App\Module\Mex\Commands;
- use App\Module\Mex\Services\MexMatchService;
- use Illuminate\Support\Facades\Log;
- /**
- * 用户买入物品撮合计划任务
- *
- * 定时执行用户买入物品撮合任务,处理待撮合的用户买入物品订单
- * php artisan mex:user-buy-item-match
- */
- class MexUserBuyItemMatchCommand extends \UCore\Command\Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'mex:user-buy-item-match
- {--item= : 指定商品ID,不指定则处理所有商品}
- {--batch=100 : 批处理大小,默认100}
- {--dry-run : 试运行模式,不执行实际撮合}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '执行用户买入物品撮合任务';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handleRun(): int
- {
- $startTime = microtime(true);
- $this->info('开始执行用户买入物品撮合任务...');
- // 获取命令参数
- $itemId = $this->option('item') ? (int)$this->option('item') : null;
- $batchSize = (int)$this->option('batch');
- $dryRun = $this->option('dry-run');
- if ($dryRun) {
- $this->warn('试运行模式:不会执行实际的撮合操作');
- return $this->handleDryRun($itemId, $batchSize);
- }
- try {
- // 显示撮合前统计
- $this->showPreMatchStats();
- // 执行用户买入物品撮合
- $result = MexMatchService::executeUserBuyItemMatch($itemId, $batchSize);
- // 显示撮合结果
- $this->showMatchResult($result);
- // 显示撮合后统计
- $this->showPostMatchStats();
- $endTime = microtime(true);
- $totalTime = round(($endTime - $startTime) * 1000, 2);
- $this->info("用户买入物品撮合任务执行完成,总耗时: {$totalTime}ms");
- return $result['success'] ? true : false;
- } catch (\Exception $e) {
- $this->error('用户买入物品撮合任务执行失败: ' . $e->getMessage());
- Log::error('MexUserBuyItemMatchCommand执行失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- return false;
- }
- }
- /**
- * 处理试运行模式
- *
- * @param int|null $itemId
- * @param int $batchSize
- * @return int
- */
- private function handleDryRun(?int $itemId, int $batchSize): int
- {
- $this->info('=== 用户买入物品撮合试运行模式 ===');
- // 显示当前统计
- $stats = MexMatchService::getUserBuyItemMatchStats();
- $this->table([
- '待撮合用户买入物品订单数',
- '涉及商品数',
- '待撮合数量',
- '待撮合金额',
- '有库存商品数'
- ], [[
- $stats['pending_orders'],
- $stats['pending_items'],
- $stats['pending_quantity'],
- $stats['pending_amount'],
- $stats['available_items']
- ]]);
- if ($itemId) {
- $this->info("指定处理商品ID: {$itemId}");
- $conditionCheck = MexMatchService::checkUserBuyItemMatchConditions($itemId);
- if ($conditionCheck['can_match']) {
- $this->info("✓ 商品 {$itemId} 满足用户买入物品撮合条件");
- $this->info(" - 待撮合订单: {$conditionCheck['pending_orders']} 个");
- } else {
- $this->warn("✗ 商品 {$itemId} 不满足用户买入物品撮合条件: {$conditionCheck['message']}");
- }
- } else {
- $this->info('将处理所有符合条件的商品');
- }
- $this->info("批处理大小: {$batchSize}");
- $this->info('试运行完成,未执行实际撮合操作');
- return true;
- }
- /**
- * 显示撮合前统计
- */
- private function showPreMatchStats(): void
- {
- $this->info('=== 用户买入物品撮合前统计 ===');
- $stats = MexMatchService::getUserBuyItemMatchStats();
- $this->table([
- '待撮合用户买入物品订单数',
- '涉及商品数',
- '待撮合数量',
- '待撮合金额',
- '有库存商品数'
- ], [[
- $stats['pending_orders'],
- $stats['pending_items'],
- $stats['pending_quantity'],
- $stats['pending_amount'],
- $stats['available_items']
- ]]);
- }
- /**
- * 显示撮合结果
- *
- * @param array $result
- */
- private function showMatchResult(array $result): void
- {
- $this->info('=== 用户买入物品撮合结果 ===');
- if ($result['success']) {
- $this->info("✓ 撮合成功");
- $this->info(" - 处理商品: " . count($result['processed_items']) . " 个");
- $this->info(" - 撮合订单: {$result['total_matched']} 个");
- $this->info(" - 成交金额: {$result['total_amount']}");
- $this->info(" - 执行时间: {$result['execution_time_ms']}ms");
- if (!empty($result['processed_items'])) {
- $this->info(" - 处理的商品ID: " . implode(', ', $result['processed_items']));
- }
- if (!empty($result['errors'])) {
- $this->warn('部分商品处理出现错误:');
- foreach ($result['errors'] as $error) {
- $this->warn(" - {$error}");
- }
- }
- } else {
- $this->error("✗ 撮合失败: {$result['message']}");
- }
- }
- /**
- * 显示撮合后统计
- */
- private function showPostMatchStats(): void
- {
- $this->info('=== 用户买入物品撮合后统计 ===');
- $stats = MexMatchService::getUserBuyItemMatchStats();
- $this->table([
- '剩余待撮合订单',
- '今日撮合订单',
- '今日成交数量',
- '今日成交金额'
- ], [[
- $stats['pending_orders'],
- $stats['today_matched'],
- $stats['today_quantity'],
- $stats['today_amount']
- ]]);
- }
- }
|