This commit is contained in:
mzl
2025-12-12 09:47:34 +00:00
parent 909f169aae
commit f99b2a84ca
6 changed files with 404 additions and 120 deletions
+106 -14
View File
@@ -26,34 +26,112 @@ class FbLeadSyncService
return 0;
}
$now = time();
$count = 0;
$now = time();
Db::transaction(function () use ($leads, $enterUserId, $channelId, $now, &$count) {
$insert = [];
$ids = [];
// 转数组,后面循环更快
$leadArrList = [];
foreach ($leads as $lead) {
$leadArrList[] = (array)$lead;
}
foreach ($leads as $lead) {
$leadArr = (array)$lead;
$insert[] = $this->mapLeadToInquiry($leadArr, $now, $enterUserId, $channelId);
$ids[] = $leadArr['id'];
// ① 收集本批手机号(去重)
$phoneList = $this->collectPhonesFromLeads($leadArrList);
// ② 批量查出 inquiry_log 已存在手机号集合(索引加持)
$existingTelSet = $this->getExistingTelSet($phoneList);
$insert = [];
$idsToMarkProcessed = []; // 插入成功 or 重复跳过,统一标记已处理
$insertCount = 0;
Db::transaction(function () use (
$leadArrList, $existingTelSet, $enterUserId, $channelId, $now,
&$insert, &$idsToMarkProcessed, &$insertCount
) {
foreach ($leadArrList as $lead) {
$leadId = (int)($lead['id'] ?? 0);
$tel = trim((string)($lead['phone'] ?? ''));
// 如果手机号存在于 inquiry_log,跳过同步(但要标记 fb_leads 已处理)
if ($tel !== '' && isset($existingTelSet[$tel])) {
if ($leadId > 0) {
$idsToMarkProcessed[] = $leadId;
}
continue;
}
$insert[] = $this->mapLeadToInquiry($lead, $now, $enterUserId, $channelId);
if ($leadId > 0) {
$idsToMarkProcessed[] = $leadId;
}
}
if ($insert) {
if (!empty($insert)) {
Db::table('inquiry_log')->insert($insert);
$insertCount = count($insert);
}
if (!empty($idsToMarkProcessed)) {
Db::table('fb_leads')
->whereIn('id', $ids)
->whereIn('id', $idsToMarkProcessed)
->update([
'sync_status' => 1,
'updated_at' => $now,
]);
$count = count($insert);
}
});
return $count;
return $insertCount;
}
/**
* 从本批 leads 中收集手机号(tel),去重后返回
*/
protected function collectPhonesFromLeads(array $leadArrList): array
{
$phones = [];
foreach ($leadArrList as $lead) {
$tel = trim((string)($lead['phone'] ?? ''));
if ($tel !== '') {
// 先用原值精确匹配(你后续要做归一化再改这里即可)
$phones[$tel] = true;
}
}
return array_keys($phones);
}
/**
* 批量查出 inquiry_log 已存在手机号集合(返回 set:tel => true)
* - 自动分块 whereIn,避免 SQL 过长
* - 建议给 inquiry_log.tel 加索引 idx_tel
*/
protected function getExistingTelSet(array $phoneList, int $chunkSize = 800): array
{
$phoneList = array_values(array_unique(array_filter(array_map('trim', $phoneList))));
if (empty($phoneList)) {
return [];
}
$existingTelSet = [];
foreach (array_chunk($phoneList, $chunkSize) as $chunk) {
$rows = Db::table('inquiry_log')
->select('tel')
->where('tel', '<>', '')
->whereIn('tel', $chunk)
->get();
foreach ($rows as $r) {
$t = trim((string)($r->tel ?? ''));
if ($t !== '') {
$existingTelSet[$t] = true;
}
}
}
return $existingTelSet;
}
/**
@@ -124,6 +202,20 @@ class FbLeadSyncService
if (!empty($lead['seat_preference'])) {
$lines[] = '<p><strong>座位数偏好:</strong>' . $e($lead['seat_preference']) . '</p>';
}
if (!empty($lead['drive_type'])) {
$lines[] = '<p><strong>驱动形式偏好:</strong>' . $e($lead['drive_type']) . '</p>';
}
if (!empty($lead['extra_data'])) {
$extra = json_decode((string)$lead['extra_data'], true);
if (is_array($extra) && $extra) {
$lines[] = '<hr />';
$lines[] = '<p><strong>补充问答信息:</strong></p>';
foreach ($extra as $k => $v) {
$lines[] = '<p>' . $e($k) . ':' . $e($v) . '</p>';
}
}
}
$lines[] = '<hr />';
$lines[] = '<p><strong>广告与表单信息:</strong></p>';