This commit is contained in:
Your Name
2026-08-24 16:56:37 +08:00
parent d9bb94cd3f
commit 562fe0ea0e
374 changed files with 1223 additions and 785 deletions
@@ -472,6 +472,45 @@ SQL;
);
}
/**
* Filter an external_userid fact by the channel snapshot retained in
* qywx_external_contact.follow_users, including soft-deleted contacts.
*
* del_external_contact removes normalized tag relations, so historical
* deleted-fan attribution cannot use applyExternalUserChannelFilter(). The
* contact row itself retains follow_users and is the best available channel
* snapshot for this specific historical statistic.
*
* @param array<string, mixed>|null $channel
*/
public static function applyHistoricalExternalUserChannelFilter(Query $query, string $field, ?array $channel): void
{
if ($channel === null) {
return;
}
$patterns = self::buildLikePatterns($channel);
if ($patterns === []) {
$query->whereRaw('1 = 0');
return;
}
$segments = [];
$bindings = [];
foreach ($patterns as $pattern) {
$segments[] = 'historical_channel_contact.follow_users LIKE ?';
$bindings[] = $pattern;
}
$contactTable = self::tableWithPrefix('qywx_external_contact');
$query->whereRaw(
"EXISTS (SELECT 1 FROM {$contactTable} historical_channel_contact"
. " WHERE historical_channel_contact.external_userid = {$field}"
. ' AND (' . implode(' OR ', $segments) . '))',
$bindings
);
}
/**
* @return array{scanned_contacts: int, discovered_tags: int, inserted_or_updated: int}
*/