Files
fdesk/app/process/InquiryAutoProcess.php
T

107 lines
3.7 KiB
PHP
Raw Normal View History

2025-12-12 17:52:20 +08:00
<?php
namespace app\process;
2026-01-22 10:15:05 +08:00
use app\common\model\UserGroup;
use app\common\model\UserGroupMembers;
2025-12-12 17:52:20 +08:00
use app\inquiry\service\FbFileImportService;
use app\inquiry\service\FbLeadSyncService;
use app\inquiry\service\InquiryAssignService;
use Workerman\Timer;
use support\Log;
class InquiryAutoProcess
{
protected int $interval;
/**
* @param int $interval 间隔秒数,默认 30 秒
*/
public function __construct(int $interval = 30)
{
$this->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');
2026-01-22 10:15:05 +08:00
// $enterUserId = (int)($cfg['enter_user_id'] ?? 0);
2025-12-12 17:52:20 +08:00
$channelId = (int)($cfg['channel_id'] ?? 0);
2026-01-22 10:15:05 +08:00
// $salesUserIds = $cfg['sales_user_ids'] ?? [];
2025-12-12 17:52:20 +08:00
$roundRobinKey = $cfg['round_robin_key'] ?? $channelKey;
2026-01-22 10:15:05 +08:00
// 查分组
$group = UserGroup::where('scenario_code', $cfg['group_key'])->where('status', 1)->first();
// 查组长
$enterUserId = UserGroupMembers::where('group_id', $group->id)->where('is_leader', 1)->where('status', 1)->value('user_id');
// 查分组成员
$salesUserIds = UserGroupMembers::where('group_id', $group->id)->where('is_leader', 0)->where('status', 1)->pluck('user_id')->toArray();
2025-12-12 17:52:20 +08:00
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()
);
}
}
}