interval = $interval; } public function onWorkerStart() { // 每 interval 秒执行一次 Timer::add($this->interval, function () { $this->runOnceForAllChannels(); }); } /** * 一轮执行:扫描 config/inquiry_channel.php 中所有渠道,逐个跑一遍流水线 */ protected function runOnceForAllChannels() { $configAll = config('inquiry_channel'); if (empty($configAll) || !is_array($configAll)) { Log::error('InquiryAutoProcess: config("inquiry_channel") 为空或格式不正确'); return; } foreach ($configAll as $channelKey => $cfg) { if (!is_array($cfg)) { Log::error("InquiryAutoProcess: 渠道 {$channelKey} 配置不是数组,已跳过"); continue; } $this->runForChannel($channelKey, $cfg); } } /** * 对单个渠道执行:导入 Excel -> fb_leads -> inquiry_log -> 分配 + 推队列 */ protected function runForChannel(string $channelKey, array $cfg) { $watchDir = public_path($cfg['watch_dir'] ?? 'excel'); $enterUserId = (int)($cfg['enter_user_id'] ?? 0); $channelId = (int)($cfg['channel_id'] ?? 0); $salesUserIds = $cfg['sales_user_ids'] ?? []; $roundRobinKey = $cfg['round_robin_key'] ?? $channelKey; if ($enterUserId <= 0 || $channelId <= 0) { Log::error("InquiryAutoProcess: 渠道 {$channelKey} 配置缺少正确的 enter_user_id 或 channel_id,已跳过"); return; } $importService = new FbFileImportService(); $syncService = new FbLeadSyncService(); $assignService = new InquiryAssignService(); try { // 1) 导入该渠道目录下的 Excel 到 fb_leads(fb_leads.channel = 渠道ID) $filesImported = $importService->importDir($watchDir, $channelId); // 2) 只同步该渠道的 fb_leads -> inquiry_log(channel 过滤) $synced = $syncService->syncToInquiryLog($enterUserId, $channelId); // 3) 只分配该渠道的 inquiry_log(channel 过滤) // $assigned = $assignService->assignBatch($salesUserIds, $roundRobinKey, $channelId, 100); $assigned = $assignService->assignBatch( $salesUserIds, $roundRobinKey, $channelId, $cfg['assign_rules'] ?? [], 100 ); Log::info("InquiryAutoProcess[{$channelKey}] files={$filesImported}, synced={$synced}, assigned={$assigned}"); } catch (\Throwable $e) { Log::error( 'InquiryAutoProcess[' . $channelKey . '] error: ' . $e->getMessage() . "\n" . $e->getTraceAsString() ); } } }